From 5b01991917bf5ec4e4ec11c90d6623886be06303 Mon Sep 17 00:00:00 2001 From: Soullloud <97365214+Soullloud@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:01:03 +0300 Subject: [PATCH 1/2] add syntax and fix bug when multiple numbers break flow --- Main.java | 794 ++++++++++++++++++++++++++---------------------------- 1 file changed, 384 insertions(+), 410 deletions(-) diff --git a/Main.java b/Main.java index 2255c0a..c86fa06 100755 --- a/Main.java +++ b/Main.java @@ -7,216 +7,203 @@ import java.util.ArrayList; import java.util.Scanner; -class Food implements Serializable -{ +class Food implements Serializable { int itemno; - int quantity; + int quantity; float price; - - Food(int itemno,int quantity) - { - this.itemno=itemno; - this.quantity=quantity; - switch(itemno) - { - case 1:price=quantity*50; + + Food(int itemno, int quantity) { + this.itemno = itemno; + this.quantity = quantity; + switch (itemno) { + case 1: + price = quantity * 50; break; - case 2:price=quantity*60; + case 2: + price = quantity * 60; break; - case 3:price=quantity*70; + case 3: + price = quantity * 70; break; - case 4:price=quantity*30; + case 4: + price = quantity * 30; break; } } } -class Singleroom implements Serializable -{ + +class Singleroom implements Serializable { String name; String contact; - String gender; - ArrayList food =new ArrayList<>(); + String gender; + ArrayList food = new ArrayList<>(); - - Singleroom() - { - this.name=""; + + Singleroom() { + this.name = ""; } - Singleroom(String name,String contact,String gender) - { - this.name=name; - this.contact=contact; - this.gender=gender; + + Singleroom(String name, String contact, String gender) { + this.name = name; + this.contact = contact; + this.gender = gender; } } -class Doubleroom extends Singleroom implements Serializable -{ + +class doubleRoom extends Singleroom implements Serializable { String name2; String contact2; - String gender2; - - Doubleroom() - { - this.name=""; - this.name2=""; + String gender2; + + doubleRoom() { + this.name = ""; + this.name2 = ""; } - Doubleroom(String name,String contact,String gender,String name2,String contact2,String gender2) - { - this.name=name; - this.contact=contact; - this.gender=gender; - this.name2=name2; - this.contact2=contact2; - this.gender2=gender2; + + doubleRoom(String name, String contact, String gender, String name2, String contact2, String gender2) { + this.name = name; + this.contact = contact; + this.gender = gender; + this.name2 = name2; + this.contact2 = contact2; + this.gender2 = gender2; } } -class NotAvailable extends Exception -{ + +class NotAvailable extends Exception { @Override - public String toString() - { + public String toString() { return "Not Available !"; } } -class holder implements Serializable -{ - Doubleroom luxury_doublerrom[]=new Doubleroom[10]; //Luxury - Doubleroom deluxe_doublerrom[]=new Doubleroom[20]; //Deluxe - Singleroom luxury_singleerrom[]=new Singleroom[10]; //Luxury - Singleroom deluxe_singleerrom[]=new Singleroom[20]; //Deluxe +class holder implements Serializable { + doubleRoom[] luxury_doublerrom = new doubleRoom[10]; //Luxury + doubleRoom[] deluxe_doublerrom = new doubleRoom[20]; //Deluxe + Singleroom[] luxury_singleerrom = new Singleroom[10]; //Luxury + Singleroom[] deluxe_singleerrom = new Singleroom[20]; //Deluxe } -class Hotel -{ - static holder hotel_ob=new holder(); +class Hotel { + static holder hotel_ob = new holder(); static Scanner sc = new Scanner(System.in); - static void CustDetails(int i,int rn) - { + + static void CustDetails(int i, int rn) { String name, contact, gender; - String name2 = null, contact2 = null; - String gender2=""; - System.out.print("\nEnter customer name: "); + String name2 = null, contact2 = null; + String gender2 = ""; + System.out.println("Enter customer name: "); name = sc.next(); System.out.print("Enter contact number: "); - contact=sc.next(); + contact = sc.next(); System.out.print("Enter gender: "); gender = sc.next(); - if(i<3) - { - System.out.print("Enter second customer name: "); - name2 = sc.next(); - System.out.print("Enter contact number: "); - contact2=sc.next(); - System.out.print("Enter gender: "); - gender2 = sc.next(); - } - - switch (i) { - case 1:hotel_ob.luxury_doublerrom[rn]=new Doubleroom(name,contact,gender,name2,contact2,gender2); + if (i < 3) { + System.out.print("Enter second customer name: "); + name2 = sc.next(); + System.out.print("Enter contact number: "); + contact2 = sc.next(); + System.out.print("Enter gender: "); + gender2 = sc.next(); + } + + switch (i) { + case 1: + hotel_ob.luxury_doublerrom[rn] = new doubleRoom(name, contact, gender, name2, contact2, gender2); break; - case 2:hotel_ob.deluxe_doublerrom[rn]=new Doubleroom(name,contact,gender,name2,contact2,gender2); + case 2: + hotel_ob.deluxe_doublerrom[rn] = new doubleRoom(name, contact, gender, name2, contact2, gender2); break; - case 3:hotel_ob.luxury_singleerrom[rn]=new Singleroom(name,contact,gender); + case 3: + hotel_ob.luxury_singleerrom[rn] = new Singleroom(name, contact, gender); break; - case 4:hotel_ob.deluxe_singleerrom[rn]=new Singleroom(name,contact,gender); + case 4: + hotel_ob.deluxe_singleerrom[rn] = new Singleroom(name, contact, gender); break; - default:System.out.println("Wrong option"); + default: + System.out.println("Wrong option"); break; } } - - static void bookroom(int i) - { + + static void bookRoom(int i) { int j; int rn; - System.out.println("\nChoose room number from : "); + System.out.println("Choose room number from : "); switch (i) { case 1: - for(j=0;j60) - System.out.println("Room doesn't exist"); - else if(ch2>40) - Hotel.order(ch2-41,4); - else if(ch2>30) - Hotel.order(ch2-31,3); - else if(ch2>10) - Hotel.order(ch2-11,2); - else if(ch2>0) - Hotel.order(ch2-1,1); - else - System.out.println("Room doesn't exist"); - break; - case 5: - System.out.print("Room Number -"); - ch2 = sc.nextInt(); - if(ch2>60) - System.out.println("Room doesn't exist"); - else if(ch2>40) - Hotel.deallocate(ch2-41,4); - else if(ch2>30) - Hotel.deallocate(ch2-31,3); - else if(ch2>10) - Hotel.deallocate(ch2-11,2); - else if(ch2>0) - Hotel.deallocate(ch2-1,1); - else - System.out.println("Room doesn't exist"); - break; - case 6:break x; - - } - - System.out.println("\nContinue : (y/n)"); - wish=sc.next().charAt(0); - if(!(wish=='y'||wish=='Y'||wish=='n'||wish=='N')) - { - System.out.println("Invalid Option"); - System.out.println("\nContinue : (y/n)"); - wish=sc.next().charAt(0); - } - - }while(wish=='y'||wish=='Y'); - - Thread t=new Thread(new write(Hotel.hotel_ob)); - t.start(); - } - catch(Exception e) - { - System.out.println("Not a valid input"); + try { + File f = new File("backup"); + if (f.exists()) { + FileInputStream fin = new FileInputStream(f); + ObjectInputStream ois = new ObjectInputStream(fin); + Hotel.hotel_ob = (holder) ois.readObject(); } + Scanner sc = new Scanner(System.in); + int ch, ch2; + char wish; + x: + do { + + System.out.println("\nEnter your choice :\n1.Display room details\n2.Display room availability \n3.Book\n4.Order food\n5.Checkout\n6.Exit\n"); + ch = sc.nextInt(); + switch (ch) { + case 1: + System.out.println("\nChoose room type :\n1.Luxury Double Room \n2.Deluxe Double Room \n3.Luxury Single Room \n4.Deluxe Single Room\n"); + ch2 = sc.nextInt(); + Hotel.features(ch2); + break; + case 2: + System.out.println("\nChoose room type :\n1.Luxury Double Room \n2.Deluxe Double Room \n3.Luxury Single Room\n4.Deluxe Single Room\n"); + ch2 = sc.nextInt(); + Hotel.availability(ch2); + break; + case 3: + System.out.println("\nChoose room type :\n1.Luxury Double Room \n2.Deluxe Double Room \n3.Luxury Single Room\n4.Deluxe Single Room\n"); + ch2 = sc.nextInt(); + Hotel.bookRoom(ch2); + break; + case 4: + System.out.print("Room Number -"); + ch2 = sc.nextInt(); + if (ch2 > 60) + System.out.println("Room doesn't exist"); + else if (ch2 > 40) + Hotel.order(ch2 - 41, 4); + else if (ch2 > 30) + Hotel.order(ch2 - 31, 3); + else if (ch2 > 10) + Hotel.order(ch2 - 11, 2); + else if (ch2 > 0) + Hotel.order(ch2 - 1, 1); + else + System.out.println("Room doesn't exist"); + break; + case 5: + System.out.print("Room Number -"); + ch2 = sc.nextInt(); + if (ch2 > 60) + System.out.println("Room doesn't exist"); + else if (ch2 > 40) + Hotel.deallocate(ch2 - 41, 4); + else if (ch2 > 30) + Hotel.deallocate(ch2 - 31, 3); + else if (ch2 > 10) + Hotel.deallocate(ch2 - 11, 2); + else if (ch2 > 0) + Hotel.deallocate(ch2 - 1, 1); + else + System.out.println("Room doesn't exist"); + break; + case 6: + break x; + + } + + System.out.println("\nContinue : (y/n)"); + wish = sc.next().charAt(0); + if (!(wish == 'y' || wish == 'Y' || wish == 'n' || wish == 'N')) { + System.out.println("Invalid Option"); + System.out.println("\nContinue : (y/n)"); + wish = sc.next().charAt(0); + } + + } while (wish == 'y' || wish == 'Y'); + + Thread t = new Thread(new write(Hotel.hotel_ob)); + t.start(); + } catch (Exception e) { + System.out.println("Not a valid input"); + } } } From 69ad68575f8d68256d5025a0e348a57563e8d341 Mon Sep 17 00:00:00 2001 From: Soullloud <97365214+Soullloud@users.noreply.github.com> Date: Wed, 27 Sep 2023 17:30:12 +0300 Subject: [PATCH 2/2] fix bug Input Validation, Thread Safety, Error Handling --- Main.java | 62 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/Main.java b/Main.java index c86fa06..3adc344 100755 --- a/Main.java +++ b/Main.java @@ -87,7 +87,7 @@ class holder implements Serializable { class Hotel { static holder hotel_ob = new holder(); static Scanner sc = new Scanner(System.in); - + static ArrayList users = new ArrayList<>(); static void CustDetails(int i, int rn) { String name, contact, gender; String name2 = null, contact2 = null; @@ -126,6 +126,8 @@ static void CustDetails(int i, int rn) { } } + + static void bookRoom(int i) { int j; int rn; @@ -140,19 +142,30 @@ static void bookRoom(int i) { System.out.println("Enter room number: "); try { String roomNumber = sc.nextLine(); - if(!roomNumber.matches("[0-9]+")) { + if (!roomNumber.matches("[0-9]+")) { throw new IllegalAccessException("incorrect room number"); } rn = Integer.parseInt(roomNumber); rn--; + if (rn < 0 || rn >= hotel_ob.luxury_doublerrom.length) { + throw new IllegalArgumentException("Invalid room number"); + } if (hotel_ob.luxury_doublerrom[rn] != null) throw new NotAvailable(); CustDetails(i, rn); + } catch (NumberFormatException e) { + System.out.println("Invalid room number format"); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + }catch (NotAvailable e) { + System.out.println("Room not available"); } catch (Exception e) { + System.out.println("Invalid Option"); return; } break; + case 2: for (j = 0; j < hotel_ob.deluxe_doublerrom.length; j++) { if (hotel_ob.deluxe_doublerrom[j] == null) { @@ -449,7 +462,23 @@ static void order(int rn, int rtype) { } } } +class User { + private String username; + private String password; + public User(String username, String password) { + this.username = username; + this.password = password; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } +} class write implements Runnable { holder hotel_ob; @@ -460,14 +489,16 @@ class write implements Runnable { @Override public void run() { - try { - FileOutputStream fout = new FileOutputStream("backup"); - ObjectOutputStream oos = new ObjectOutputStream(fout); - oos.writeObject(hotel_ob); - } catch (Exception e) { - System.out.println("Error in writing " + e); - } + synchronized (hotel_ob) { + try { + FileOutputStream fout = new FileOutputStream("backup"); + ObjectOutputStream oos = new ObjectOutputStream(fout); + oos.writeObject(hotel_ob); + } catch (Exception e) { + System.out.println("Error in writing " + e); + } + } } } @@ -475,12 +506,17 @@ public void run() { public class Main { public static void main(String[] args) { + try { File f = new File("backup"); if (f.exists()) { FileInputStream fin = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(fin); - Hotel.hotel_ob = (holder) ois.readObject(); + + synchronized (Hotel.hotel_ob) { + Hotel.hotel_ob = (holder) ois.readObject(); + + } } Scanner sc = new Scanner(System.in); int ch, ch2; @@ -543,11 +579,11 @@ else if (ch2 > 0) } - System.out.println("\nContinue : (y/n)"); + System.out.println("Continue : (y/n)"); wish = sc.next().charAt(0); if (!(wish == 'y' || wish == 'Y' || wish == 'n' || wish == 'N')) { System.out.println("Invalid Option"); - System.out.println("\nContinue : (y/n)"); + System.out.println("Continue : (y/n)"); wish = sc.next().charAt(0); } @@ -556,7 +592,7 @@ else if (ch2 > 0) Thread t = new Thread(new write(Hotel.hotel_ob)); t.start(); } catch (Exception e) { - System.out.println("Not a valid input"); + System.out.println("Not a valid input" + e.toString()); } } }