构造函数字段max number java

时间:2017-04-20 10:36:35

标签: java printing constructor max field

我制作了一个用java模拟酒店的程序,在这个程序中你将Rooms作为构造函数,我想打印最多2个构造函数字段(这是更昂贵的)。我知道如何制作返回2种价格差异的方法,但我不知道如何打印哪种价格最贵...这是我的代码。

主类

    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;
}

2 个答案:

答案 0 :(得分:0)

我鼓励你使用房间外的方法来计算价格差异,因为它是商业逻辑。 Room shuold只是一个数据持有者。将来扩展行动会更容易。

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中的变量命名约定,所有非静态变量都应以lowerCase开头,CamelCase仅保留给类名。

答案 1 :(得分: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;
}

将其放在主类中,并在调用方法

时添加所有房间的列表作为参数