Advertisement
Guest User

Untitled

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