2013-10-14 40 views
-1

我有两个班是房间和客人,我需要计算房间内预订客房的人数以找到合适的房间。例如:客人5人预订,但我们有三个房间:A1001 2人,A1002 6人,A1003 8人。我们计算如下:| 5-2 | = 3; | 5-6 | = 1; | 5-8 | = 3 =>我们选择了房间A1002,因为它收到了最小的值。如何计算所有值并比较每个对象?

public class Room { 
    private String roomName; 
    private String roomType; 
    private int roomNumSeat; 
    private int status; 
    } 


public class Guest { 
    private String guestID; 
    private String guestName; 
    private double time; 
    private int guestNum; 
    private double bonus; 
    private ArrayList<String> guestServices= new ArrayList<String>(); 
    private ArrayList<String> guestRoomType= new ArrayList<String>(); 
    private Room room; 
    private Services services; 

} 

你能给我一个想法,可以解决上面的问题吗?我不知道如何计算所有的房间和结果。 类客户,我创建方法:

public String RoomForGuest(){ 
     return this.room.Room_Guest(); 
    } 

在教室:

public String Room_Guest(Guest g) {  
     Math.abs(g.getGuestNum()-this.roomNumSeat); 
//How can i calculate all roomNumSeat and compare the value received? 
     return this.roomName; 
    } 
+0

所以,如果你有三个人预约,你给他们的房间有两张床,因为| 3-2 | = 1& – luanjot

回答

1
import java.util.ArrayList; 
import java.util.LinkedHashMap; 

public class HotelRooms { 

    LinkedHashMap<String, Room> roomMap = new LinkedHashMap<String, Room>(); 

    public class Room { 
     private String roomName; 
     private String roomType; 
     private int roomNumSeat; 
     private int status; 

     public Room(String roomName, String roomType, int roomSeat, 
       int roomStatus) { 
      this.roomName = roomName; 
      this.roomType = roomType; 
      this.roomNumSeat = roomSeat; 
      this.status = roomStatus; 
     } 

    } 

    public class Guest { 
     private String guestID; 
     private String guestName; 
     private double time; 
     private int guestNum; 
     private double bonus; 
     private ArrayList<String> guestServices = new ArrayList<String>(); 
     private ArrayList<String> guestRoomType = new ArrayList<String>(); 
     private Room room; 
     private Services services; 

     public Guest(String guestID, String guestName, double time, 
       int guestNum, double bonus, ArrayList<String> guestServices, 
       ArrayList<String> guestRoomType) { 
      this.guestID = guestID; 
      this.guestName = guestName; 
      this.time = time; 
      this.guestNum = guestNum; 
      this.bonus = bonus; 
      this.guestServices = guestServices; 
      this.guestRoomType = guestRoomType; 
     } 

    } 

    public void createRooms(String roomName, String roomType, int roomSeat, 
      int roomStatus) { 
     Room room = new Room(roomName, roomType, roomSeat, roomStatus); 
     roomMap.put(roomName, room); 
    } 

    public String roomForGuest() { 
     Guest g = new Guest("1234", "taki", 12.00, 2, 0, null, null); 
     String roomName = this.getRoom(g); 
     System.out.println(roomName); 
     return roomName; 
    } 

    public String getRoom(Guest g) { 
     Room roomObj = null; 
     String roomName = ""; 
     int firstValue, previousValue = 0, count = 0; 
     for (String key : roomMap.keySet()) { 
      roomObj = roomMap.get(key); 
      firstValue = roomObj.roomNumSeat - g.guestNum; 
      if ((firstValue >= 0 && firstValue <= previousValue) || count == 0 
        || previousValue < 0) 
       roomName = roomObj.roomName; 
      previousValue = firstValue; 
      count++; 
     } 
     return roomName; 
    } 

    public static void main(String[] args) { 
     HotelRooms test = new HotelRooms(); 
     test.createRooms("A1001", "", 2, 0); 
     test.createRooms("A1002", "", 6, 0); 
     test.createRooms("A1003", "", 8, 0); 
     test.roomForGuest(); 
    } 

} 
+0

非常感谢!我最近学习了java,那么你能教我解决问题的简单方法吗?我从来没有学过LinkedHashMap。 @ _ @ – taki

+0

你可以在这里学习地图和泛型..在去地图之前学习泛型http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html http://docs.oracle.com/ JavaSE的/教程/收藏/接口/ map.html – AJJ