Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- public class HotelSystem {
- Map<String, Account> accounts = new HashMap<String, Account>();
- Map<Integer, Room> rooms = new HashMap<Integer, Room>();
- Scanner input;
- Account current;
- //done
- public HotelSystem(){
- current = null;
- input = new Scanner(System.in);
- accounts.put("1-Admin", new Account(1, "Admin", "Admin"));
- }
- //done
- public boolean callCommand(String command){
- try {
- Method method = getClass().getDeclaredMethod("command"+command);
- method.invoke(this);
- return true;
- } catch (Exception e) {
- System.out.println("- Unknown Command");
- return false;
- }
- }
- //Login
- public void commandLI(){
- if(current != null){
- System.out.println("- There is an active account");
- return;
- }
- System.out.print("+ Employee Number : ");
- String user = input.nextLine();
- System.out.print("+ password : ");
- String password = input.nextLine();
- if(login(user, password)){
- System.out.println("+ Logged In Sucessfully");
- }
- else{
- System.out.println("- Login Failed!");
- }
- }
- //done
- public boolean commandSR(){
- if(verifyCredentials("Gestor", "Balcao")){
- System.out.println("-------------------------------------------");
- System.out.println("[Showing All Rooms]\n");
- for(int i = 1; i <= rooms.size(); i++){
- System.out.println(rooms.get(i));
- }
- return true;
- }
- else{
- System.out.println("- Permission Denied");
- return false;
- }
- }
- //Create Account
- public boolean commandCA(){
- if(verifyCredentials("Gestor", "Admin")){
- System.out.print("+ Employers Number : ");
- int user = testIntParse(input.nextLine());
- if(user < 1){
- System.out.println("- Invalid Number");
- return false;
- }
- System.out.print("Password : ");
- String password = input.nextLine();
- System.out.println("+ Available Ranks (Gestor, Limpeza, Balcao)");
- System.out.print("+ Rank : ");
- String rank = input.nextLine();
- if(rank.equals("Gestor") || rank.equals("Limpeza") || rank.equals("Balcao")){
- createAccount(user, password, rank);
- System.out.println("+ Account created sucessfully");
- return true;
- }
- System.out.println("- Unknown Rank");
- return false;
- }
- else{
- System.out.println("- Permission Denied");
- return false;
- }
- }
- //Update Room
- public void commandUR(){
- if(verifyCredentials("Gestor")){
- System.out.print("+ Room Number : ");
- int roomNum = testIntParse(input.nextLine());
- if(roomNum < 1 || roomNum > rooms.size()){
- System.out.println("- Invalid Room Number :");
- return;
- }
- System.out.println("+ Number of beds : ");
- int numBeds = testIntParse(input.nextLine());
- if(numBeds < 1){
- System.out.println("- Invalid Beds Number");
- return;
- }
- System.out.print("+ Single | Couple : ");
- String type = input.nextLine();
- if(type.equals("Single") || type.equals("Couple")){
- rooms.get(roomNum).beds = numBeds;
- rooms.get(roomNum).isSingle = (type == "Single" ? true : false);
- System.out.println("+ [Room Updated]");
- }
- else{
- System.out.println("- Invalid Room Type");
- }
- }
- else{
- System.out.println("- Permission Denied");
- }
- }
- //Show Cleanings
- public boolean commandSC(){
- if(verifyCredentials("Gestor", "Limpeza")){
- for(int i = 1; i <= rooms.size(); i++){
- System.out.println("+ Room " + i + ": " + (rooms.get(i).lastCleanDate == null ? "Never Cleaned!" : rooms.get(i).lastCleanDate));
- }
- return true;
- }
- System.out.println("- Permission Denied");
- return false;
- }
- //Register Cleaning
- public boolean commandRC(){
- if(verifyCredentials("Gestor", "Limpeza")){
- System.out.print("+ Room number:");
- int roomNum = testIntParse(input.nextLine());
- if(roomNum < 1 || roomNum > rooms.size()){
- System.out.println("- Invalid Room Number");
- return false;
- }
- System.out.println("Date in format dd/MM/yyyy HH:mm");
- String testDate = input.nextLine();
- if(!testDate(testDate, new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"))){
- System.out.println("- Invalid Date Format");
- return false;
- }
- rooms.get(roomNum).lastCleanDate = testDate;
- return true;
- }
- else{
- System.out.println("- Permission Denied");
- return false;
- }
- }
- //List Reservations
- public boolean commandLR(){
- if(verifyCredentials("Gestor", "Balcao")){
- for(int i = 1; i <= rooms.size(); i++){
- if(rooms.get(i).res != null){
- System.out.println("+ --------------------------------------- +");
- System.out.println("+ Room " + i + ":");
- System.out.println( "+ Entry Date :" + rooms.get(i).res.entryDate);
- System.out.println( "+ Leave Date :" + rooms.get(i).res.leaveDate);
- System.out.println( "+ Client Info :" + rooms.get(i).res.clientName + " " + rooms.get(i).res.clientNif + "\n");
- }
- }
- return true;
- }
- System.out.println("- Permission Denied");
- return false;
- }
- //Delete Reservation
- public boolean commandDR(){
- if(verifyCredentials("Gestor", "Balcao")){
- System.out.println("- Room number:");
- int roomNum = testIntParse(input.nextLine());
- if(roomNum > 0 && rooms.get(roomNum) != null){
- rooms.get(roomNum).removeReservation();
- System.out.println("+ Reserve Removed Sucessfully");
- return true;
- }
- System.out.println("- Invalid Room Number");
- return false;
- }
- System.out.println("- Permission Denied");
- return false;
- }
- //Make Reservation
- public boolean commandMR() throws ParseException{
- if(verifyCredentials("Gestor", "Balcao")){
- System.out.print("+ Room Number: ");
- int roomNum = testIntParse(input.nextLine());
- if(roomNum > 0 && roomNum >= rooms.size()){
- System.out.println("Entry Date (dd/MM/yyyy HH:mm):");
- String entryDate = input.nextLine();
- if(!testDate(entryDate, new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"))){
- System.out.println("- Invalid Date");
- return false;
- }
- System.out.println("Number of nights:");
- int nights = testIntParse(input.nextLine());
- if(nights < 1 || nights > 31){
- System.out.println("- Invalid Number");
- return false;
- }
- System.out.println("Nome do cliente:");
- String clientName = input.nextLine();
- System.out.println("Nif do cliente:");
- int clientNif = testIntParse(input.nextLine());
- if(clientNif < 1){
- System.out.println("- Invalid Number");
- return false;
- }
- rooms.get(roomNum).res = new Reservation(roomNum, entryDate, nights, clientName, clientNif);
- rooms.get(roomNum).isFree = false;
- System.out.println("+ Reservation Created");
- return true;
- }
- System.out.println("- Invalid Room Number");
- return false;
- }
- System.out.println("- Permission Denied");
- return false;
- }
- //Available Rooms
- public void commandAR(){
- if(verifyCredentials("Gestor", "Balcao")){
- System.out.println("\nAvailable rooms: \n");
- for(int i = 1; i <= rooms.size(); i++){
- if(rooms.get(i).isFree){
- System.out.println(rooms.get(i));
- }
- }
- }
- else{
- System.out.println("Permission Denied!");
- }
- }
- //Logout
- public boolean commandLO(){
- if(current != null){
- current = null;
- System.out.println("+ Logged Out Sucessfully");
- return true;
- }
- return false;
- }
- //Create Room
- public boolean commandCR(){
- if(verifyCredentials("Gestor", "Admin")){
- System.out.print("+ Number of Beds");
- int beds = testIntParse(input.nextLine());
- if(beds < 1){
- System.out.println("- Invalid Number");
- return false;
- }
- System.out.print("+ Single | Couple : ");
- String type = input.nextLine();
- if(type.equals("Single") || type.equals("Couple")){
- rooms.put(rooms.size() + 1, new Room(rooms.size() + 1, beds, type));
- System.out.println("+ Room " + rooms.size() + " created");
- return true;
- }
- System.out.println("- Invalid Room Type");
- return false;
- }
- System.out.println("- Permission Denied");
- return false;
- }
- //done
- public boolean verifyCredentials(String test){
- return (current.rank.equals(test) ? true : false);
- }
- //done
- public boolean verifyCredentials(String test1, String test2){
- return (current.rank.equals(test1) || current.rank.equals(test2) ? true : false);
- }
- //done
- public int testIntParse(String string){
- int test;
- try{
- test = Integer.parseInt(string);
- return test;
- }
- catch (Exception e){
- return -1;
- }
- }
- //done
- public boolean testDate(String test, SimpleDateFormat format){
- Date date;
- try{
- date = format.parse(test);
- return true;
- } catch (Exception e) {
- return false;
- }
- }
- public boolean createAccount(int user, String pwd, String rank){
- return (accounts.put("" + user + "-" + pwd, new Account(user, pwd, rank)) == null ? true : false);
- }
- public boolean login(String user, String pwd){
- if(accounts.get(user + "-" + pwd) == null){
- return false;
- }
- current = accounts.get(user + "-" + pwd);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement