2017-04-20 16 views
0

私はjavaを持つホテルのシミュレーションであるプログラムを作成しました。このプログラムでは、ルームをコンストラクタにして、最大2コンストラクタフィールド(これは高価です)を出力します。私は2つの価格の差を返す方法を作る方法を知っていますが、最も高価な印刷方法はわかりません...ここに私のコードです。コンストラクタfields max number java

メインクラス

String RoomNumber, Category, View; 
    int NumberOfBeds; 
    double Price; 

    Room RoomA = new Room("C101",2,"Standard","Sea",95.89); 



    Scanner sc = new Scanner(System.in); 
    System.out.println("Give room number \n"); 
    RoomNumber=sc.next(); 
    System.out.println("Give number of beds \n"); 
    NumberOfBeds=sc.nextInt(); 
    System.out.println("Give category \n"); 
    Category=sc.next(); 
    System.out.println("Give view \n"); 
    View=sc.next(); 
    System.out.println("Give price \n"); 
    Price=sc.nextDouble(); 

    Room RoomB = new Room(RoomNumber, NumberOfBeds, Category, View, Price); 
    System.out.println(RoomA.toString()); 
    System.out.println(RoomB.toString()); 
    System.out.println(""); //this is the part I am struggling 

、これは私はそれがビジネスロジックであるとして、価格差を計算するために、部屋の外のメソッドを使用することをお勧めします

public String RoomNumber; 
public int NumberOfBeds; 
private String Category; 
private String View; 
private double Price; 

    public Room(String RoomNumber, int NumberOfBeds, String Category, String View, double Price){ 
    RoomNumber = this.RoomNumber; 
    NumberOfBeds = this.NumberOfBeds; 
    Category = this.Category; 
    View = this.View; 
    Price = this.Price; 
} 


    public void setRoomNumber(String roomnumber){ 
    this.RoomNumber = roomnumber; 
} 
public String getRoomNumber(){ 
    return this.RoomNumber; 
} 

public void setNumberOfBeds(int numberofbeds){ 
    this.NumberOfBeds = numberofbeds; 
} 
public int getNumberOfBeds(){ 
    return this.NumberOfBeds; 
} 

public void setCategory(String category){ 
    this.Category = category; 
} 
public String getCategory(){ 
    return this.Category; 
} 

public void setView(String view){ 
    this.View = view; 
} 
public String getView(){ 
    return this.View; 
} 

public void setPrice(double price){ 
    this.Price = price; 
} 
public double getPrice(){ 
    return this.Price; 
} 


public double getPriceDifference(double double1, double double2){ 
    if (double1 > double2){ 

     return double1-double2; //and i know that here is the part i must add something 
    }else{ 
     return double2-double1; 
    } 
} 

@Override 
public String toString() { 
    return "Room number:" + this.RoomNumber + ",\n " 
      + "Number of beds:" + this.NumberOfBeds + ",\n " + "Category:" 
      + this.Category + ",\n " + "View:" + this.View + ",\n " + "Price:" + this.Price; 
} 

答えて

0

私の部屋クラスです。部屋shuoldはデータホルダーだけです。将来は操作を拡張するのが簡単になります。あなたは、あなたがComparatorを作成するとルームリストをソートするために使用する必要があるよりも、二つ以上の部屋を比較したい場合は

private static double priceDifference(Room room1, Room room2){ 
    return Math.abs(room1.getPrice()- room2.getPrice()); 
} 

:あなたはMath.abs()メソッドを使用することができる価格差をチェックするための

private static Room moreExpensiveRoom(Room room1, Room room2){ 
    return room1.getPrice() > room2.getPrice()? room1 : room2; 
} 

最大の価格でそれを取る。またはmax()

そして最後にでストリームを使用します。javaでの変数の命名規則について読んでください、すべての非静的変数は、小文字で始まる必要があり、キャメルケースがクラス名だけのために予約されています。

+0

メソッドを呼び出すときにパラメータとして、すべての部屋のリストを追加クラスなど、[lowerCamelCase](https://en.wikipedia.org/wiki/Camel_case)メソッド、変数など – Kayaman

0
public Room getMostExpensive(ArrayList<Room> rooms) { 
    double max = 0d; 
    Room room = null; 
    for (Room room1 : rooms) { 
     if (max < room1.getPrice()) { 
      max = room1.getPrice(); 
      room = room1; 
     } 
    } 
    return room; 
} 

あなたのメインクラスに入れ、これをしては[PascalCase](https://en.wikipedia.org/wiki/PascalCase)