Guest User

Untitled

a guest
Jun 15th, 2017
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.06 KB | None | 0 0
  1. import java.lang.reflect.InvocationTargetException;
  2. import java.lang.reflect.Method;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.*;
  6.  
  7. public class HotelSystem {
  8.  
  9. Map<String, Account> accounts = new HashMap<String, Account>();
  10. Map<Integer, Room> rooms = new HashMap<Integer, Room>();
  11.  
  12. Scanner input;
  13. Account current;
  14.  
  15. //done
  16. public HotelSystem(){
  17. current = null;
  18. input = new Scanner(System.in);
  19. accounts.put("1-Admin", new Account(1, "Admin", "Admin"));
  20. }
  21. //done
  22. public boolean callCommand(String command){
  23. try {
  24. Method method = getClass().getDeclaredMethod("command"+command);
  25. method.invoke(this);
  26. return true;
  27. } catch (Exception e) {
  28. System.out.println("- Unknown Command");
  29. return false;
  30. }
  31. }
  32. //done
  33. public boolean verifyCredentials(String test){
  34. return (current.rank.equals(test) ? true : false);
  35. }
  36. //done
  37. public boolean verifyCredentials(String test1, String test2){
  38. return (current.rank.equals(test1) || current.rank.equals(test2) ? true : false);
  39. }
  40. //done
  41. public int testIntParse(String string){
  42.  
  43. int test;
  44.  
  45. try{
  46. test = Integer.parseInt(string);
  47. return test;
  48. }
  49. catch (Exception e){
  50. return -1;
  51. }
  52. }
  53. //done
  54. public boolean testDate(String test, SimpleDateFormat format){
  55. try{
  56. Date date = format.parse(test);
  57. return true;
  58. } catch (Exception e) {
  59. return false;
  60. }
  61. }
  62. //done
  63. public void commandLI(){
  64.  
  65. if(current != null){
  66. System.out.println("- There is an active account");
  67. return;
  68. }
  69.  
  70. System.out.print("+ Employee Number : ");
  71. String user = input.nextLine();
  72.  
  73. System.out.print("+ password : ");
  74. String password = input.nextLine();
  75.  
  76. if(login(user, password)){
  77. System.out.println("+ Logged In Sucessfully");
  78. }
  79. else{
  80. System.out.println("- Login Failed!");
  81. }
  82. }
  83. //done
  84. public boolean commandSR(){
  85.  
  86. if(verifyCredentials("Gestor", "Balcao")){
  87. System.out.println("-------------------------------------------");
  88. System.out.println("[Showing All Rooms]\n");
  89. for(int i = 1; i <= rooms.size(); i++){
  90. System.out.println(rooms.get(i));
  91. }
  92. return true;
  93. }
  94. else{
  95. System.out.println("- Permission Denied");
  96. return false;
  97. }
  98. }
  99. //Create Account
  100. public boolean commandCA(){
  101.  
  102. if(verifyCredentials("Gestor", "Admin")){
  103.  
  104. System.out.print("+ Employers Number : ");
  105. int user = testIntParse(input.nextLine());
  106.  
  107. if(user < 1){
  108. System.out.println("- Invalid Number");
  109. return false;
  110. }
  111.  
  112. System.out.print("Password : ");
  113. String password = input.nextLine();
  114.  
  115. System.out.println("+ Available Ranks (Gestor, Limpeza, Balcao)");
  116. System.out.print("+ Rank : ");
  117.  
  118. String rank = input.nextLine();
  119. if(rank.equals("Gestor") || rank.equals("Limpeza") || rank.equals("Balcao")){
  120. createAccount(user, password, rank);
  121. System.out.println("+ Account created sucessfully");
  122. return true;
  123. }
  124. System.out.println("- Unknown Rank");
  125. return false;
  126.  
  127. }
  128. else{
  129. System.out.println("- Permission Denied");
  130. return false;
  131. }
  132. }
  133. //Update Room
  134. public void commandUR(){
  135.  
  136. if(verifyCredentials("Gestor")){
  137.  
  138. System.out.print("+ Room Number : ");
  139. int roomNum = testIntParse(input.nextLine());
  140.  
  141. if(roomNum < 1 || roomNum > rooms.size()){
  142. System.out.println("- Invalid Room Number :");
  143. return;
  144. }
  145.  
  146. System.out.println("+ Number of beds : ");
  147. int numBeds = testIntParse(input.nextLine());
  148.  
  149. if(numBeds < 1){
  150. System.out.println("- Invalid Beds Number");
  151. return;
  152. }
  153.  
  154. System.out.print("+ Single | Couple : ");
  155. String type = input.nextLine();
  156.  
  157. if(type.equals("Single") || type.equals("Couple")){
  158. rooms.get(roomNum).beds = numBeds;
  159. rooms.get(roomNum).isSingle = (type == "Single" ? true : false);
  160. System.out.println("+ [Room Updated]");
  161. }
  162. else{
  163. System.out.println("- Invalid Room Type");
  164. }
  165.  
  166. }
  167. else{
  168. System.out.println("- Permission Denied");
  169. }
  170. }
  171. //Show Cleanings
  172. public void commandSC(){
  173. if(verifyCredentials("Gestor", "Limpeza")){
  174. for(int i = 1; i <= rooms.size(); i++){
  175. System.out.println("+ Room " + i + ": " + (rooms.get(i).lastCleanDate == null ? "Never Cleaned!" : rooms.get(i).lastCleanDate));
  176. }
  177. }
  178. else{
  179. System.out.println("- Permission Denied");
  180. }
  181. }
  182. //Register Cleaning
  183. public boolean commandRC(){
  184.  
  185. if(verifyCredentials("Gestor", "Limpeza")){
  186.  
  187. System.out.print("+ Room number:");
  188. int roomNum = testIntParse(input.nextLine());
  189.  
  190. if(roomNum < 1 || roomNum > rooms.size()){
  191. System.out.println("- Invalid Room Number");
  192. return false;
  193. }
  194.  
  195. System.out.println("Date in format dd/MM/yyyy HH:mm");
  196. String testDate = input.nextLine();
  197.  
  198. if(!testDate(testDate, new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"))){
  199. System.out.println("- Invalid Date Format");
  200. return false;
  201. }
  202.  
  203. rooms.get(roomNum).lastCleanDate = testDate;
  204. return true;
  205. }
  206. else{
  207. System.out.println("- Permission Denied");
  208. return false;
  209. }
  210. }
  211. //List Reservations
  212. public boolean commandLR(){
  213.  
  214. if(verifyCredentials("Gestor", "Balcao")){
  215. for(int i = 1; i <= rooms.size(); i++){
  216. if(rooms.get(i).res != null){
  217. System.out.println("+ --------------------------------------- +");
  218. System.out.println("+ Room " + i + ":");
  219. System.out.println( "+ Entry Date :" + rooms.get(i).res.entryDate);
  220. System.out.println( "+ Leave Date :" + rooms.get(i).res.leaveDate);
  221. System.out.println( "+ Client Info :" + rooms.get(i).res.clientName + " " + rooms.get(i).res.clientNif + "\n");
  222. }
  223. }
  224. return true;
  225. }
  226.  
  227. System.out.println("- Permission Denied");
  228. return false;
  229. }
  230. //Delete Reservation
  231. public boolean commandDR(){
  232.  
  233. if(verifyCredentials("Gestor", "Balcao")){
  234.  
  235. System.out.println("- Room number:");
  236. int roomNum = testIntParse(input.nextLine());
  237.  
  238. if(roomNum > 0 && rooms.get(roomNum) != null){
  239. rooms.get(roomNum).removeReservation();
  240. System.out.println("+ Reserve Removed Sucessfully");
  241. return true;
  242. }
  243.  
  244. System.out.println("- Invalid Room Number");
  245. return false;
  246. }
  247.  
  248. System.out.println("- Permission Denied");
  249. return false;
  250. }
  251.  
  252. public void commandMR() throws ParseException{
  253. if(current.rank.equals("Gestor") || current.rank.equals("Balcao")){
  254. input = new Scanner(System.in);
  255. System.out.println("Room number:");
  256. int roomNum = Integer.parseInt(input.nextLine());
  257. if(rooms.get(roomNum) != null){
  258. System.out.println("Entry Date (dd/MM/yyyy HH:mm):");
  259. String entryDate = input.nextLine();
  260. System.out.println("Number of nights:");
  261. int nights = Integer.parseInt(input.nextLine());
  262. System.out.println("Nome do cliente:");
  263. String clientName = input.nextLine();
  264. System.out.println("Nif do cliente:");
  265. int clientNif = Integer.parseInt(input.nextLine());
  266.  
  267. rooms.get(roomNum).res = new Reservation(roomNum, entryDate, nights, clientName, clientNif);
  268. rooms.get(roomNum).isFree = false;
  269. }
  270. else{
  271. System.out.println("Invalid room number");
  272. }
  273. }
  274. else{
  275. System.out.println("Permission Denied!");
  276. }
  277. }
  278. //Available Rooms
  279. public void commandAR(){
  280.  
  281. if(verifyCredentials("Gestor", "Balcao")){
  282.  
  283. System.out.println("\nAvailable rooms: \n");
  284.  
  285. for(int i = 1; i <= rooms.size(); i++){
  286. if(rooms.get(i).isFree){
  287. System.out.println(rooms.get(i));
  288. }
  289. }
  290. }
  291. else{
  292. System.out.println("Permission Denied!");
  293. }
  294. }
  295. //Logout
  296. public boolean commandLO(){
  297. if(current != null){
  298. current = null;
  299. System.out.println("+ Logged Out Sucessfully");
  300. return true;
  301. }
  302. return false;
  303. }
  304.  
  305. public boolean createAccount(int user, String pwd, String rank){
  306. return (accounts.put("" + user + "-" + pwd, new Account(user, pwd, rank)) == null ? true : false);
  307. }
  308.  
  309. public boolean login(String user, String pwd){
  310. if(accounts.get(user + "-" + pwd) == null){
  311. return false;
  312. }
  313. current = accounts.get(user + "-" + pwd);
  314. return true;
  315. }
  316. }
Add Comment
Please, Sign In to add comment