diff --git a/Accounts.java b/Accounts.java new file mode 100644 index 0000000..c57d0f2 --- /dev/null +++ b/Accounts.java @@ -0,0 +1,63 @@ +package railways; +import java.util.*; + +public class Accounts { + LinkedListuserList=new LinkedList<>(); + Scanner sc=new Scanner(System.in); + + void createAccount() { + int flag=0; + do{ + flag=0; + System.out.print("\n\tEnter the username you want to use for your account: "); + String userName = sc.nextLine(); + for(User u : userList) { + if(u.userName.equals(userName)) { + System.out.println("\tAlready exists. Try a new username."); + flag=1; + break; + } + } + if(flag==0){ + System.out.print("\n\tEnter the password to secure your account: "); + String password = sc.nextLine(); + User u1 = new User(userName,password); + userList.add(u1); + System.out.println("\n\tAccount created successfully!!"); + u1.userMenu(); + } + + }while(flag==1); + + } + + public void login() { + int flag=0, trying=0; + do{ + System.out.print("\n\tEnter the userName: "); + String userName=sc.nextLine(); + System.out.print("\n\tEnter the password: "); + String password=sc.nextLine(); + User u1 = null; + for(User u:userList) { + if(u.userName.equals(userName) && u.password.equals(password) ){ + flag=1; + u1=u; + break; + } + } + if(flag==1) { + System.out.println("\n\tLogin Successful!!"); + u1.userMenu(); + } + else { + System.out.println("\tPlease enter proper credentials.\n"); + System.out.println("\tDo you want to keep trying?\n\t1.Yes\n\t2.Exit"); + System.out.print("\n\tEnter your choice: "); + trying = sc.nextInt(); + sc.nextLine(); + } + }while(flag==0 && trying==1); + } +} + diff --git a/Admin.java b/Admin.java new file mode 100644 index 0000000..0c2a7e5 --- /dev/null +++ b/Admin.java @@ -0,0 +1,197 @@ +package railways; +import java.util.*; +public class Admin { + public static LinkedList trainList = new LinkedList<>(); + public static LinkedList removedTrains = new LinkedList<>(); + String securityKey = "admin123"; + + void displayAdminMenu() { + int choice = 0; + do { + System.out.println("\n\t***Admin Menu***\n"); + System.out.println("\t1. Add trains "); + System.out.println("\t2. Remove trains."); + System.out.println("\t3. Display all trains."); + //System.out.println("\t4. Business analysis");//te functions nantar decide karu + System.out.println("\t4. Exit"); + Scanner s = new Scanner(System.in); + int status = 0; + do { + status = 0; + try { + System.out.print("\tEnter your choice: "); + choice = s.nextInt(); //s.nextLine(); + }catch(InputMismatchException e) { + System.out.println("\tThis choice is invalid.Enter a number between 1 to 4"); + status = 1; + s.nextLine(); + } + if(status==0) s.nextLine(); + }while(status==1); + switch(choice) { + case 1: this.addTrains(); + break; + case 2: this.removeTrains(); + break; + case 3: this.displayTrains(); + break; + case 4: System.out.println("\n\tClosing admin window...."); + break; + default: System.out.println("\n\tInvalid choice!"); + break; + } + }while(choice!=4); + } + + public void addTrains() { + Scanner sc = new Scanner(System.in); + String choice; + Validation v = new Validation(); + do { + System.out.print("\n\t\tEnter the train number: "); + int trainNo=sc.nextInt(); + sc.nextLine(); + System.out.print("\n\t\tEnter the name of the train: "); + String trainName=sc.nextLine(); + boolean isSrc=false; boolean isDest=false; + String source,destination; + do { + do { + System.out.print("\n\t\tEnter the source place of the train: "); + source=sc.nextLine(); + isSrc=v.placeValidation(source); + if(!isSrc) { + System.out.println("\t\tThis place is invalid."); + } + }while(!isSrc); + do { + System.out.print("\n\t\tEnter the destination place of the train: "); + destination=sc.nextLine(); + isDest=v.placeValidation(destination); + if(!isDest) { + System.out.println("\t\tThis place is invalid."); + } + }while(!isDest); + if(source.equalsIgnoreCase(destination)) { + System.out.println("\t\tSource and Destination cannot be same.Please enter correct source and destination"); + } + }while(source.equalsIgnoreCase(destination)); + boolean isValid=false; String date;//int status=0; + do { + System.out.print("\n\t\tEnter date of journey in dd/mm/yyyy format only: "); + date = sc.nextLine(); + isValid=v.dateValidation(date); + if(!isValid) { + System.out.println("\t\tThe date format is invalid."); + } + + }while(!isValid); + + boolean deptTime=false; String departure; + do { + System.out.print("\n\t\tEnter departure time in 24 hour format as hh:mm only: "); + departure = sc.nextLine(); + deptTime=v.timeValidation(departure); + if(!deptTime) { + System.out.println("\t\tThe time format is invalid."); + } + }while(!deptTime); + + boolean arrTime=false; String arrival; + do { + System.out.print("\n\t\tEnter arrival time in 24 hour format as hh:mm only: "); + arrival = sc.nextLine(); + arrTime=v.timeValidation(arrival); + if(!arrTime) { + System.out.println("\t\tThe time format is invalid."); + } + }while(!arrTime); + + + System.out.print("\n\t\tEnter the maximum capacity of the train: "); + int maxSeat=sc.nextInt(); sc.nextLine(); + System.out.print("\n\t\tEnter the maximum waiting list capacity of the train: "); + int maxWait=sc.nextInt(); sc.nextLine(); + String slp; + do { + System.out.print("\n\t\tIs the train sleepercoach?(y/n): "); + slp = sc.nextLine(); + if(!slp.equalsIgnoreCase("Y")&&!slp.equalsIgnoreCase("N")) { + System.out.println("\t\tInvalid Choice!"); + } + }while(!slp.equalsIgnoreCase("Y")&&!slp.equalsIgnoreCase("N")); + boolean sl = slp.equals("y")||slp.equals("Y") ? true:false; + String ac; + do { + System.out.print("\n\t\tIs the train A/C?(y/n): "); + ac = sc.nextLine(); + if(!ac.equalsIgnoreCase("Y")&&!ac.equalsIgnoreCase("N")) { + System.out.println("\t\tInvalid Choice!"); + } + }while(!ac.equalsIgnoreCase("Y")&&!ac.equalsIgnoreCase("N")); + boolean a = ac.equals("y")||ac.equals("Y") ? true:false; + System.out.print("\n\t\tEnter price of ticket: "); + int price = sc.nextInt(); sc.nextLine(); + Train t=new Train(trainNo,trainName,a,sl,date,source,destination,maxSeat,maxWait, departure, arrival,price); + trainList.add(t); + System.out.println("\n\t\tTrain "+trainNo+" "+trainName+" added successfully"); + do { + System.out.print("\n\t\tDo you want to continue adding trains? (y/n): "); + choice=sc.nextLine(); + if(!choice.equalsIgnoreCase("Y")&&!choice.equalsIgnoreCase("N")) { + System.out.println("\t\tInvalid Choice!"); + } + }while(!choice.equalsIgnoreCase("Y")&&!choice.equalsIgnoreCase("N")); + + }while(choice.equalsIgnoreCase("Y")); + } + + public void removeTrains() { + Scanner scan = new Scanner(System.in); + int trainNo; + int flag=0; + String ch = "y"; + do { + flag = 0; + System.out.print("\n\t\tEnter the train number which you want to remove: "); + trainNo = scan.nextInt(); scan.nextLine(); + for(Train t1 : trainList) { + if(t1.trainNo==trainNo) { + removedTrains.add(t1); + trainList.remove(t1); + System.out.println("\n\t\tTrain no. "+trainNo+" successfully removed."); + flag=1; + break; + } + } + if(flag!=1) { + System.out.println("\t\tTrain not found."); + } + do { + System.out.print("\n\t\tDo you want to continue adding trains? (y/n): "); + ch=scan.nextLine(); + if(!ch.equalsIgnoreCase("Y")&&!ch.equalsIgnoreCase("N")) { + System.out.println("\t\tInvalid Choice!"); + } + }while(!ch.equalsIgnoreCase("Y")&&!ch.equalsIgnoreCase("N")); + }while(ch.equals("y")||ch.equals("Y")); + } + void displayTrains() { + if(trainList.size()==0) { + System.out.println("\n\t\tNo trains are added yet. Please add the trains to check the availability."); + } + else { + System.out.println("\n\t\tFollowing are the details of all available trains:"); + System.out.println("\t\t_________________________________________________________________________________________________________________________");//85 + System.out.println("\t\t|NO.| NAME | DATE | SOURCE | DESTINATION |DEPARTURE|ARRIVAL|PRICE| AC |SLEEPER|SEATS|WAITING|"); + System.out.println("\t\t|___|____________________|__________|_______________|_______________|_________|_______|_____|______|_______|_____|_______|"); + for(Train t : trainList) { + String a = t.ac ? "Yes":"No"; + String sleep = t.sleeperCoach ? "Yes":"No"; + System.out.format("\t\t|%3d|%20s|%10s|%15s|%15s|%9s|%7s|%5d|%6s|%7s|%5d|%7d|\n",t.trainNo,t.trainName,t.date,t.Source,t.Destination,t.departureTime,t.arrivalTime,t.price,a,sleep,t.maxSeat,t.maxWait); + } + System.out.println("\t\t|___|____________________|__________|_______________|_______________|_________|_______|_____|______|_______|_____|_______|"); + } + } +} + diff --git a/Booking.java b/Booking.java new file mode 100644 index 0000000..1b786c6 --- /dev/null +++ b/Booking.java @@ -0,0 +1,11 @@ +package railways; + +public class Booking { + int trainNo; + int bookingID; + Booking(int trainNo, int bookingID){ + this.trainNo = trainNo; + this.bookingID = bookingID; + } +} + diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..07a1ea1 --- /dev/null +++ b/Main.java @@ -0,0 +1,91 @@ +package railways; +import java.util.*; + +public class Main { + public static void main(String args[]) { + Scanner sc=new Scanner(System.in); + System.out.println("Welcome to Indian Railways!"); + Admin a=new Admin(); + Accounts acc=new Accounts(); + String securityKey; + + int choice=0; + int ch=0; + int trying=0; + + do { + System.out.println("\nPlease select your role\n"); + System.out.println("1. Admin"); + System.out.println("2. User"); + System.out.println("3. Exit"); + int status=0; + do { + status=0; + try { + System.out.print("Enter your choice: "); + choice = sc.nextInt(); //sc.nextLine(); + }catch(InputMismatchException e) { + System.out.println("This choice is invalid. Enter a number between 1 to 3"); + status=1; + sc.nextLine(); + } + if(status==0) sc.nextLine(); + }while(status==1); + switch(choice) { + + case 1: do { + System.out.print("\nEnter the security key: "); + //sc.nextLine(); + securityKey=sc.nextLine(); + if(securityKey.equals(a.securityKey)) { + System.out.println("\n\tWelcome, Admin!"); + a.displayAdminMenu(); + }else { + System.out.println("\nThis key is invalid."); + System.out.println("\nDo you want to keep trying?\n1.Yes\n2.Exit"); + System.out.print("\nEnter your choice: "); + trying = sc.nextInt(); + sc.nextLine(); + } + }while(!securityKey.equals(a.securityKey) && trying==1); + + break; + + case 2: System.out.println("\n\tDo you want to create new account or login into your existing account?\n"); + System.out.println("\t1. Create Account"); + System.out.println("\t2. Login"); + System.out.println("\t3. Exit"); + int flag=0; + do { + flag=0; + try { + System.out.print("\tEnter your choice: "); + ch = sc.nextInt(); //sc.nextLine(); + }catch(InputMismatchException e) { + System.out.println("\tThis choice is invalid. Enter a number between 1 to 3"); + flag=1; + sc.nextLine(); + } + if(flag==0) sc.nextLine(); + }while(flag==1); + switch(ch) { + case 1: acc.createAccount(); + break; + case 2: acc.login(); + break; + case 3: System.out.println("\n\tThank you.Visit again!"); + break; + default: System.out.println("\tInvalid choice"); + } + + break; + case 3: System.out.println("\nThank you!"); + break; + default: System.out.println("Invalid choice!"); + } + + }while(choice!=3); + } +} + + diff --git a/Passenger.java b/Passenger.java new file mode 100644 index 0000000..d103a86 --- /dev/null +++ b/Passenger.java @@ -0,0 +1,16 @@ +package railways; + +public class Passenger { + String passengerName; + int age; + String gender; + int bookingID; + //constructor of passenger class + Passenger(String passengerName, int age, String gender, int bookingID){ + this.passengerName=passengerName; + this.age=age; + this.gender=gender; + this.bookingID = bookingID; + } +} + diff --git a/README.md b/README.md index e674b94..06f29ed 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,41 @@ -# Buffer_2.0 -Buffer_2.0 repo to submit your projects to.Repository containing folder-wise code for all submissions to Buffer : A Project Series, conducted between April, 2021 - June, 2020. - -Each folder should contain a separate README to describe the files and project objective - +# CodeWays: Our trains run smoothly; and so does our code!!! +## TEAM MEMBERS: +#### SY Comp Div A +1. NISHA DESHMUKH +2. VAISHNAVI BHAVSAR +3. YAMINI DONGAONKAR +## PROBLEM STATEMENT: +Building a console-based programme to automate the manual RAILWAY RESERVATION SYSTEM. +## FEATURES: +1. This is a console-based programme which simulates Railway Reservation System. +2. It enables the admin to securely login into the admin’s window to add and remove trains. +3. It allows users to create their accounts on the platform to book or cancel tickets or check status of their tickets. +4. Users can enter their choices and details to get desired outcomes. +5. Appropriate output is generated according to the user’s input. +## DATA STRUCTURES USED: + ### 1. SINGLY LINKED LIST: + List of all trains + List of removed trains + List of passengers with confirmed tickets for a particular train + List of all bookings made by a user + List of all user accounts and their credential +#### Size of these lists is dynamic, hence singly linked list is used for efficient insertion and deletion. +### 2. QUEUE + Waiting list of passengers for a particular train +#### Queue is used to implement First in First Out mechanism for waiting passengers. +### 3. ARRAY + List of seats in a particular train +#### Array of boolean values is used to check whether a particular seat in a train is vacant or not. Its size is static and provides efficient searching. +## LEARNINGS AND OUTCOMES: +1. Importance and application of Abstract Data Types (ADTs) and Flowcharts +2. Application and implementation of various linear data structures +3. Importance of static keyword (used a static variable to keep track of booking ID) +4. Learned a new concept of Regular Expressions +5. Learned a powerful tool - GitHub +## WHAT'S NEXT: +1. User interface for better user experience. +2. Database for storing train and station data. + + + + diff --git a/Seat.java b/Seat.java new file mode 100644 index 0000000..9719feb --- /dev/null +++ b/Seat.java @@ -0,0 +1,11 @@ +package railways; + +public class Seat extends Passenger{ + int seatNo; + Boolean window; + Seat(String passengerName, int age, String gender, int bookingID, int seatNo, boolean window){ + super(passengerName, age, gender, bookingID); + this.seatNo=seatNo; + this.window=window; + } +} diff --git a/Train.java b/Train.java new file mode 100644 index 0000000..4ccba5d --- /dev/null +++ b/Train.java @@ -0,0 +1,151 @@ +package railways; +import java.util.*; +public class Train { + int trainNo; + String trainName; + boolean ac; + boolean sleeperCoach; + String date;//validation + String Source; + String Destination; + int maxSeat; + int maxWait; + String departureTime; + String arrivalTime; + int price; + boolean[] seatList = new boolean[maxSeat]; // by default initialized to false, so if seatList[i]==false=>seat no. i+1 is vacant (not occupied). + static int bookingCount=0; + LinkedList seatedPassengers = new LinkedList<>(); + Queue waiting = new LinkedList<>(); + //LocalDate d = LocalDate.parse(date); + + Train(int trainNo,String trainName, boolean ac, boolean sleeperCoach,String date,String Source, String Destination,int maxSeat,int maxWait, String departure, String arrival, int price){ + this.trainNo=trainNo; + this.trainName=trainName; + this.ac=ac; + this.sleeperCoach=sleeperCoach; + this.date=date; + this.Source=Source; + this.Destination=Destination; + this.maxSeat=maxSeat; + this.maxWait=maxWait; + this.departureTime = departure; + this.arrivalTime = arrival; + this.price = price; + this.seatList = new boolean[maxSeat]; + } + + void bookTicket(String name, String gender, int age) { + int currBookingID=0; + if(seatedPassengers.size()==maxSeat) { + currBookingID = ++bookingCount; + Passenger p1=new Passenger(name,age,gender.toUpperCase(),currBookingID); + waiting.add(p1); + System.out.println("\n\t\tYou have been added to waiting list."); + displayTicket(currBookingID); + } + else { + currBookingID = ++bookingCount; + int seatNo; + int i=0; + for(i=0;i bookingList = new LinkedList(); + User(String userName, String password){ + this.userName = userName; + this.password = password; + } + Scanner sc = new Scanner(System.in); + public void displayAvailableTrains(String source, String destination, String date) { + int flag=0; + int ch; + //Train t1 = null; + ArrayList availableTrains = new ArrayList(); + for(Train t: Admin.trainList) { + if((t.Source).equalsIgnoreCase(source)&&(t.Destination).equalsIgnoreCase(destination)&&(t.date).equalsIgnoreCase(date)) { + if(flag==0) { + System.out.println("\n\t\tFollowing are the details of all available trains:"); + System.out.println("\t\t_________________________________________________________________________________________________________________________");//85 + System.out.println("\t\t|NO.| NAME | DATE | SOURCE | DESTINATION |DEPARTURE|ARRIVAL|PRICE| AC |SLEEPER|SEATS|WAITING|"); + System.out.println("\t\t|___|____________________|__________|_______________|_______________|_________|_______|_____|______|_______|_____|_______|"); + } + flag=1; + String a = t.ac ? "Yes":"No"; + String sleep = t.sleeperCoach ? "Yes":"No"; + availableTrains.add(t.trainNo); + System.out.format("\t\t|%3d|%20s|%10s|%15s|%15s|%9s|%7s|%5d|%6s|%7s|%5d|%7d|\n",t.trainNo,t.trainName,t.date,t.Source,t.Destination,t.departureTime,t.arrivalTime,t.price,a,sleep,t.maxSeat-(t.seatedPassengers).size(),t.maxWait-(t.waiting).size()); + } + } + if(flag==1) { + System.out.println("\t\t|___|____________________|__________|_______________|_______________|_________|_______|_____|______|_______|_____|_______|"); + System.out.println("\n\t\tDo you want to continue booking?\n\t\t1.Yes\n\t\t2.Exit "); + System.out.print("\t\tEnter your choice: "); + ch = sc.nextInt(); sc.nextLine(); + if(ch==1){ + System.out.print("\n\t\tEnter train number which you want to book: "); + int n = sc.nextInt(); sc.nextLine(); + if(!availableTrains.contains(n)) { + System.out.println("\n\t\tInvalid train number. Try again."); + return; + } + int i=0; + for(i=0;i<(Admin.trainList).size();i++) { + if(Admin.trainList.get(i).trainNo==n) { + break; + } + } + if(Admin.trainList.get(i).maxSeat-(Admin.trainList.get(i).seatedPassengers).size()==0 && Admin.trainList.get(i).maxWait-(Admin.trainList.get(i).waiting).size()==0) { + System.out.println("\n\t\tThis train does not have any seats available."); + System.out.println("\t\tSorry for the incovenience."); + return; + } + System.out.print("\n\t\tEnter Number of tickets you want to book: "); + int tickets = sc.nextInt();sc.nextLine(); + boolean toBook = false; + int seats = Admin.trainList.get(i).maxSeat-(Admin.trainList.get(i).seatedPassengers).size(); + int wait = Admin.trainList.get(i).maxWait-(Admin.trainList.get(i).waiting).size(); + String choice =null; + if(tickets<=seats) { + System.out.println("\n\t\tTickets for "+tickets+" passengers are available."); + toBook=true; + } + else if(tickets<=wait+seats) { + if(seats==0) { + System.out.println("\n\t\tNo confirm tickets in this train are available.All your "+tickets+" tickets will be booked in waiting."); + System.out.print("\n\t\tDo you want to continue? (Y/N): "); + choice=sc.nextLine(); + } + else { + System.out.println("\n\t\tYou will get "+seats+" tickets as confirmed and remaining "+(tickets-seats)+" tickets will be added to waiting."); + System.out.print("\n\t\tDo you want to continue? (Y/N): "); + choice=sc.nextLine(); + } + toBook = choice.equals("Y")||choice.equals("y") ? true:false; + } + else { //tickets>wait+seats; + System.out.println("\n\t\tThis train does not have "+tickets+" tickets available."); + if(seats==0) { + System.out.println("\t\tYou will get only "+wait+" tickets in waiting."); + System.out.print("\n\t\tDo you want to continue? (Y/N): "); + choice = sc.nextLine(); + } + else { + System.out.print("\t\tYou will get "+seats+" tickets as confirmed and "+wait+" tickets in waiting."); + System.out.println("\n\t\tDo you want to continue? (Y/N): "); + choice = sc.nextLine(); + } + toBook = choice.equals("Y")||choice.equals("y") ? true:false; + tickets=wait+seats; + } + if(toBook) { + List yourList = new LinkedList<>(); + System.out.println("\n\t\tPlease enter passenger details respectively:"); + for(int t=0;t