2017-04-20 35 views
0

我製作了一個程序,該程序是用java編寫的一個酒店模擬程序,在此程序中,您將Rooms作爲構造函數,並且我想打印最多2個構造函數字段(這更昂貴)。我知道如何使返回2價格的差異的方法,但我不知道如何打印哪些是最昂貴的...這是我的代碼。構造函數字段最大數量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

我會鼓勵你使用的房間外的方法來計算差價,因爲它是商業邏輯。房間只能是數據持有者。將來它將擴大運營範圍。

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

對於檢查的價格差異,你可以使用Math.abs()方法:

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

當你想比較兩間客房以上,比你需要創建一個Comparator並使用排序房間列表,然後拿最大的價格。或使用流與max()

而最後:請閱讀有關Java中的變量命名約定,所有的非靜態變量應該與小寫開始,駝峯僅保留類名。

+0

[PascalCase(https://en.wikipedia.org/wiki/PascalCase)爲類,等等,[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; 
} 

將此放在你的主類和調用方法時添加所有的房間作爲參數列表