Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.Random;
- public class HotelProject {
- ArrayList<Guest> guestList = new ArrayList();
- ArrayList<Room> roomList = new ArrayList();
- Room selectedRoom;
- boolean loggedIn = false;
- Guest g;
- public static void main(String[] args) {
- System.out.println("\n========================== WELCOME TO OUR RESERVATION SYSTEM ============================");
- HotelProject hp = new HotelProject();
- hp.readInGuests();
- System.out.println(" Our hotel currently has " + hp.guestList.size() + " members");
- hp.readInRooms();
- System.out.println(" We offer " + hp.roomList.size() + " types of room");
- System.out.println("=========================================================================================");
- hp.processHotelProject();
- }
- public void processHotelProject() {
- while (true) {
- System.out.println("=========================================================================================");
- System.out.println("Please Log In To Begin");
- do {
- System.out.println("Username:");
- Scanner scanner = new Scanner(System.in);
- String username = scanner.next();
- System.out.println("Password:");
- String password = scanner.next();
- g = verifyLogin(username, password);
- } while (g == null);
- System.out.println("Successfully Logged in. Thank you.");
- System.out.println("To make sure you are not a robot please re-enter number below");
- Random rand = new Random();
- int randomNum = rand.nextInt((10000 - 1) + 1) + 1;
- System.out.println(randomNum);
- Scanner scanner = new Scanner(System.in);
- String random = scanner.next();
- if (random.equals(randomNum + "")) {
- System.out.println("Welcome " + g.getPrefix() + ". " + g.getLastName() + " Your balance is: $" + g.getBalance());
- do{
- int selection = processSelection();
- if(selection == 1){
- processBooking();
- } else {
- System.out.println("Thank you " + g.getPrefix() + ". " + g.getLastName() + " for using our system and Good Bye.");
- g = null;
- selectedRoom = null;
- break;
- }
- } while(true);
- } else {
- System.out.println("You are a robot! Bye Bye.");
- break;
- }
- }
- }
- public void processBooking(){
- String selection = printAvailableOccupancies();
- if(selection.equals("x")){
- System.out.println("Thank you " + g.getPrefix() + ". " + g.getLastName() + " for using our system and Good Bye.");
- } else {
- selection = printAvailableRoomTypes(selection);
- if(selection.equals("x")){
- System.out.println("Thank you " + g.getPrefix() + ". " + g.getLastName() + " for using our system and Good Bye.");
- } else {
- // System.out.println("proceeding");
- boolean flag = confirmSelection();
- if(flag){
- g.setBalance(g.getBalance() - selectedRoom.getPrice());
- deductFromAvailable();
- System.out.println("Thank you for your booking. Your balance is now: $" + g.getBalance());
- do{
- System.out.println("Would you like to add Breakfast to your reservation at $25 per person?");
- System.out.println("This would add total of $" + (25*selectedRoom.getMaxAllowed()));
- System.out.println("select 1 to purchase");
- System.out.println("x to exit");
- Scanner scanner = new Scanner(System.in);
- selection = scanner.next();
- if (!selection.equals("x") && ! selection.equals("1")) {
- System.out.println("INVALID SELECTION PLEASE REENTER.");
- } else {
- if( selection.equals("1") ){
- g.setBalance(g.getBalance() - ((25*selectedRoom.getMaxAllowed())));
- }
- System.out.println("Thank you for your purchase. Your balance is now: $" + g.getBalance());
- break;
- }
- }while(true);
- } else {
- System.out.println("No reservation has been made. We are looking forward for your next reservations. Have a Great Day!");
- }
- }
- }
- }
- public void deductFromAvailable(){
- for(int x = 0; x < roomList.size(); x++){
- Room availableRoom = roomList.get(x);
- if(availableRoom == selectedRoom){
- // System.out.println("found:" + availableRoom.getNumberOfRooms());
- availableRoom.setNumberOfRooms(availableRoom.getNumberOfRooms()-1);
- }
- }
- }
- public boolean confirmSelection(){
- String selection;
- do{
- if(selectedRoom != null){
- System.out.println("The room you selected is $" + selectedRoom.getPrice() + " per night.");
- System.out.println("choose 1 to book it");
- System.out.println("choose x to exit");
- }
- Scanner scanner = new Scanner(System.in);
- selection = scanner.next();
- if (!selection.equals("x") && ! selection.equals("1")) {
- System.out.println("INVALID SELECTION PLEASE REENTER.");
- } else{
- break;
- }
- } while (true);
- return selection.equals("1");
- }
- public String printAvailableRoomTypes(String str){
- int numOfOccupants = new Integer(str).intValue();
- String selection;
- do {
- HashMap hm = new HashMap();
- System.out.println("Choose type of room below:");
- for(int x = 0; x < roomList.size(); x++){
- Room room = roomList.get(x);
- if(room.getNumberOfRooms() > 0 && room.getMaxAllowed()==numOfOccupants){
- System.out.println(x+1 + " for " + room.getViewType() + " view( " + room.getNumberOfRooms() + " currently available)");
- hm.put(new Integer(x+1), room);
- }
- }
- System.out.println("x to exit");
- Scanner scanner = new Scanner(System.in);
- selection = scanner.next();
- if (!selection.equals("x") && ! hm.containsKey(new Integer(selection))) {
- System.out.println("INVALID SELECTION PLEASE REENTER.");
- } else{
- if(!selection.equals("x")){
- selectedRoom = (Room)hm.get(new Integer(selection));
- }
- break;
- }
- } while( true);
- return selection;
- }
- public String printAvailableOccupancies(){
- String selection;
- Map<Integer, Integer> map = new HashMap<Integer, Integer>();
- for(int x = 0; x < roomList.size(); x++){
- if(roomList.get(x).getNumberOfRooms() > 0){
- map.put(new Integer((roomList.get(x)).getMaxAllowed()).intValue(), new Integer((roomList.get(x)).getMaxAllowed()).intValue());
- }
- }
- Collection<Integer> unsorted = map.values();
- Object[] intsArr = unsorted.toArray();
- do {
- System.out.print("Currently we offer rooms for ");
- for (int x = 0; x < intsArr.length; x++) {
- System.out.print(((Integer) intsArr[x]).intValue());
- if (x < intsArr.length - 2) {
- System.out.print(", ");
- } else if (x < intsArr.length - 1) {
- System.out.print(" or ");
- }
- }
- System.out.println(" people. Please type");
- for (int x = 0; x < intsArr.length; x++) {
- System.out.println(((Integer) intsArr[x]).intValue() + " to choose room for " + ((Integer) intsArr[x]).intValue());
- }
- System.out.println("x to exit");
- Scanner scanner = new Scanner(System.in);
- selection = scanner.next();
- if (!selection.equals("x") && !checkArrayForSelection(selection, intsArr)) {
- System.out.println("INVALID SELECTION PLEASE REENTER.");
- } else{
- break;
- }
- } while (true);
- return selection;
- }
- public boolean checkArrayForSelection(String str, Object[] intsArr ){
- for(int x = 0; x < intsArr.length; x++){
- int fromArr = ((Integer)intsArr[x]).intValue();
- if(str.equals(fromArr + "")){
- return true;
- }
- }
- return false;
- }
- public int processSelection(){
- while ( true ) {
- printOptions();
- if( getNumberOfRoomsAvailable() > 0){
- Scanner scanner = new Scanner(System.in);
- String selection = scanner.next();
- if (selection.equals("1") || selection.equals("2")) {
- return new Integer(selection).intValue();
- } else {
- System.out.println("We do not recognize this selection please try again");
- }
- } else {
- break;
- }
- }
- return 2;
- }
- public void printOptions(){
- if( getNumberOfRoomsAvailable() > 0){
- System.out.println("-----------------------------------------------------------------------------------------");
- //System.out.println("=========================================================================================");
- System.out.println("Please choose from the options below:");
- System.out.println("Type 1 to book a room");
- System.out.println("Type 2 to exit");
- } else {
- System.out.println("We are currently completely booked. Please check back soon! Good Bye.");
- }
- }
- public int getNumberOfRoomsAvailable(){
- int available = 0;
- for(int x = 0; x < roomList.size(); x++){
- available += roomList.get(x).getNumberOfRooms();
- }
- return available;
- }
- public void readInGuests() {
- try {
- FileReader fr = new FileReader("./guests.txt");
- BufferedReader br = new BufferedReader(fr);
- String line;
- while ( ( line = br.readLine()) != null) {
- List<String> fields = Arrays.asList(line.split(","));
- Guest g = new Guest();
- g.setFirstName(fields.get(0));
- g.setLastName(fields.get(1));
- g.setUsername(fields.get(2));
- g.setPassword(fields.get(3));
- g.setPrefix(fields.get(4));
- g.setBalance(new Double(fields.get(5)).doubleValue());
- guestList.add(g);
- }
- } catch(Exception e){
- System.out.println("Exception:" + e.toString());
- }
- }
- public void readInRooms() {
- try {
- FileReader fr = new FileReader("./rooms.txt");
- BufferedReader br = new BufferedReader(fr);
- String line;
- while ( ( line = br.readLine()) != null) {
- List<String> fields = Arrays.asList(line.split(","));
- Room r = new Room();
- r.setMaxAllowed(new Integer(fields.get(0)).intValue());
- r.setNumberOfRooms(new Integer(fields.get(1)).intValue());
- r.setViewType(fields.get(2));
- r.setPrice(new Double(fields.get(3)).doubleValue());
- roomList.add(r);
- }
- } catch(Exception e){
- System.out.println("Exception:" + e.toString());
- }
- }
- public Guest verifyLogin(String username, String password) {
- for(int x = 0; x < guestList.size(); x++){
- Guest guest = guestList.get(x);
- if (username.equals(guest.getUsername()) && password.equals(guest.getPassword())) {
- return guest;
- }
- }
- System.out.println("Username/Password incorrect please try again");
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement