Room.java (2069B)
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.Map; 4 5 public class Room { 6 public String type; 7 public String desc; 8 public int price; 9 public int avail; 10 private HashMap<String, Integer> guests = new HashMap<String, Integer>(); 11 private ArrayList<String> listsubs = new ArrayList<String>(); 12 13 public Room(String type, String desc, int price, int avail) { 14 this.type = type; 15 this.desc = desc; 16 this.price = price; 17 this.avail = avail; 18 } 19 20 public String addGuest(String name, int num) { 21 if (num <= 0) 22 return "fail: cannot book <= 0 rooms"; 23 else if (avail - num >= 0) { 24 guests.put(name, guests.getOrDefault(name, 0) + num); 25 avail -= num; 26 return "success: " + num * price + " euros"; 27 } else if (avail > 0) 28 return "fail: can only book " + avail + " rooms"; 29 else 30 return "fail: no rooms available"; 31 } 32 33 public String removeReserv(String name, int num) { 34 String str = ""; 35 int n; 36 37 if (!guests.containsKey(name)) 38 return "guest '" + name + "' doesn't exist"; 39 40 /* make sure he doesn't cancel more than he has booked */ 41 if ((n = guests.get(name)) >= num) { 42 guests.put(name, n - num); 43 avail += num; 44 n -= num; 45 /* remove guest if he has 0 reservations */ 46 if (n == 0) { 47 guests.remove(name); 48 str += "success"; 49 } else 50 str += "success: " + n + " reservations left"; 51 str += notifySubs(name); 52 return str; 53 } else 54 return "fail: cannot remove more (" + num + 55 ") than booked (" + n + ")"; 56 } 57 58 public String listGuests() { 59 String str = ""; 60 61 for (Map.Entry<String, Integer> g : guests.entrySet()) 62 str += "\t" + g.getKey() + " (" + g.getValue() + ")\n"; 63 return str; 64 } 65 66 public int totalGuests() { 67 return guests.size(); 68 } 69 70 public void addSub(String name) { 71 listsubs.add(name); 72 } 73 74 public String notifySubs(String name) { 75 if (listsubs.contains(name)) 76 return "\nnew " + avail + " rooms available!"; 77 else 78 return ""; 79 } 80 81 public String toString() { 82 return avail + " " + type + " (" + desc + 83 ") rooms available - " + price + " euros per night"; 84 } 85 }