QUESTION: Develop a Railway Reservation Application
contains
1.AC coach
2.Non AC coach
3. Seater
each should contain 60 seats and 10 waiting list max allowed rest request should be cancelled. If a confirmed ticket is cancelled then waiting list should moved to confirm ticket.
you should have
1.Ticket Booking
2.Ticket Cancellation
3.Status Checking
ANSWER:
//here i had 4 class 1-main class 2-manager class in which all the operations takes place 3&4-passenger and booking declaration class
//MAIN CLASS BookingSystem.java
package RailwayReservation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
class BookingSystem {
public static BookingManager bookingManager=new BookingManager();
static Scanner scanner=new Scanner(System.in);
public static void main(String[] args) {
int choice;
do
{
System.out.println("Enter you choice \n1.Ticket Booking \n2. Ticket Cancellation\n3. Status checking \n4.exit");
choice=scanner.nextInt();
while(choice<=4)
{
switch(choice) {
case 1: getBookingDetails();
break;
case 2: cancelBooking();
break;
case 3: showStatus();
break;
case 4: System.out.println("Thank u!! Visit again");
break;
}
break;
}
}while(choice<=4);
}
private static void getBookingDetails() {
int count,select=1;
int coach,noOfTickets,age;
char from,to,gender;
String name,preferedBerth;
System.out.println("enter the 1.AC, 2.Non-Ac, 3.Seater");
do
{
coach=scanner.nextInt();
}while(coach>3);
count=bookingManager.checkAvailability(coach);
if(count<=70)
{
if(count<=60) {
System.out.println("from");
from=scanner.next().charAt(0);
System.out.println("To");
to=scanner.next().charAt(0);
System.out.println("enter how many tickets required ");
noOfTickets=scanner.nextInt();
scanner.nextLine();
if(count+noOfTickets>4)
{
System.out.println("Cannot book"+(count+noOfTickets-70)+"/n want to continue enter 1 ");
select=scanner.nextInt();
noOfTickets=count+noOfTickets-70;
}
if(select==1)
{
HashMap<Integer,Booking> bookedTickets=new HashMap<Integer,Booking>();
for(int i=0;i<noOfTickets;i++) {
//scanner.nextLine();
System.out.println("enter name");
name=scanner.nextLine();
System.out.println("enter age");
age=scanner.nextInt();
System.out.println("enter gender");
gender=scanner.next().charAt(0);
scanner.nextLine();
System.out.println("enter preferedBerth");
preferedBerth=scanner.nextLine();
ArrayList<Booking> temp=bookingManager.bookTicket(from,to,coach,noOfTickets,name,age,gender,preferedBerth);
bookedTickets.put(i+1, temp.get(0));
}
for(Entry<Integer, Booking> temp:bookedTickets.entrySet()) {
System.out.println("Customer Id : "+temp.getValue().getCustId());
System.out.println("Status "+temp.getValue().getStatus());
}
}
}
}
}
private static void showStatus() {
int custId;
String status;
System.out.println("enter customer id");
custId=scanner.nextInt();
status=bookingManager.checkStatus(custId);
System.out.println("your ticket is"+status);
}
private static void cancelBooking() {
int custId,select;
boolean check=true;
int amount=0;
System.out.println("enter customer id");
custId=scanner.nextInt();
check=showDetails(custId);
if(check) {
System.out.println("Kindly confirm to proceed cancel by enter 1");
select=scanner.nextInt();
if(select==1)
{
bookingManager.cancelTicket(custId);
System.out.println("your ticket canncelled and 5% "+(amount*5/100)+" of the ticket price "+amount+" non refunded");
}
else
{
System.out.println("Thank you! your ticket is not cancelled");
}
}
}
private static boolean showDetails(int custId) {
boolean s=true;
Passenger passenger=bookingManager.getCustomerDetails(custId);
if(passenger!=null)
{
System.out.println("Name "+passenger.getName());
System.out.println("From "+passenger.getFrom());
System.out.println("To "+passenger.getTo());
System.out.println("Coach "+passenger.getCoachReq());
if(passenger.getStatus().equals("Cancelled"))
{
System.out.println("your ticket is already cancelled");
s=false;
}
else
{
System.out.println("status "+passenger.getStatus());
System.out.println("Kindly note that once you cancel 5% of your ticket charges are not returnable");
}
}
return s;
}
void showTotalBookedTickets()
{
HashMap<Integer,Passenger> passList=bookingManager.getPassengerDetails();
}
}
//Booking class Booking.java
package RailwayReservation;
class Booking {
private int bookingId;
private int custId;
private String berth;
private int charges;
private int coachType;
public int getCharges() {
return charges;
}
public void setCharges(int charges) {
this.charges = charges;
}
public Booking(int bookingId, int custId, int coachType,String berth, String status,int charges) {
super();
this.bookingId = bookingId;
this.custId = custId;
this.berth = berth;
this.status = status;
this.charges=charges;
this.coachType=coachType;
}
private String status;
public int getBookingId() {
return bookingId;
}
public int getCoachType() {
return coachType;
}
public void setCoachType(int coachType) {
this.coachType = coachType;
}
public void setBookingId(int bookingId) {
this.bookingId = bookingId;
}
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
public String getBerth() {
return berth;
}
public void setBerth(String berth) {
this.berth = berth;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
//Passenger class
package RailwayReservation;
class Passenger {
int custId;
String name;
int age;
char gender;
String preferedBerth;
int noOfTicketsReq;
int coachReq;
String status;
int ticketId;
char from;
char to;
public Passenger(int custId, String name,int age, char gender, String preferedBerth, int noOfTicketsReq, int coachReq,
String status, int ticketId, char from, char to) {
super();
this.custId = custId;
this.age = age;
this.gender = gender;
this.preferedBerth = preferedBerth;
this.noOfTicketsReq = noOfTicketsReq;
this.coachReq = coachReq;
this.status = status;
this.ticketId = ticketId;
this.from = from;
this.to = to;
this.name=name;
}
public char getFrom() {
return from;
}
public void setFrom(char from) {
this.from = from;
}
public char getTo() {
return to;
}
public void setTo(char to) {
this.to = to;
}
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getPreferedBerth() {
return preferedBerth;
}
public void setPreferedBerth(String preferedBerth) {
this.preferedBerth = preferedBerth;
}
public int getNoOfTicketsReq() {
return noOfTicketsReq;
}
public void setNoOfTicketsReq(int noOfTicketsReq) {
this.noOfTicketsReq = noOfTicketsReq;
}
public int getCoachReq() {
return coachReq;
}
public void setCoachReq(int coachReq) {
this.coachReq = coachReq;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getTicketId() {
return ticketId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setTicketId(int ticketId) {
this.ticketId = ticketId;
}
}
//Booking manager class
package RailwayReservation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
class BookingManager {
static int passId=1,bookIdBAC=1,bookIdBNAC=1,bookIdBS=1,bookIdWAC=1,bookIdWNAC=1,bookIdWS=1;
HashMap<Integer,Booking> bookedTicketsAC=new HashMap<Integer,Booking>();
HashMap<Integer,Booking> waitingTicketsAC=new HashMap<Integer,Booking>();
HashMap<Integer,Booking> bookedTicketsNonAC=new HashMap<Integer,Booking>();
HashMap<Integer,Booking> waitingTicketsNonAC=new HashMap<Integer,Booking>();
HashMap<Integer,Booking> bookedTicketsSeater=new HashMap<Integer,Booking>();
HashMap<Integer,Booking> waitingTicketsSeater=new HashMap<Integer,Booking>();
HashMap<Integer,Passenger> passengerDetails=new HashMap<Integer,Passenger>();
public HashMap<Integer, Passenger> getPassengerDetails() {
return passengerDetails;
}
public void setPassengerDetails(HashMap<Integer, Passenger> passengerDetails) {
this.passengerDetails = passengerDetails;
}
public HashMap<Integer, Booking> getBookedTicketsAC() {
return bookedTicketsAC;
}
public void setBookedTicketsAC(HashMap<Integer, Booking> bookedTickets) {
this.bookedTicketsAC = bookedTickets;
}
public int checkAvailability(int coachType) {
int val=0;
switch(coachType)
{
case 1: val=bookedTicketsAC.size()+waitingTicketsAC.size();
break;
case 2: val=bookedTicketsNonAC.size()+waitingTicketsNonAC.size();
break;
case 3: val=bookedTicketsSeater.size()+waitingTicketsSeater.size();
break;
}
return val;
}
public ArrayList<Booking> bookTicket(char from,char to,int coach, int noOfTickets, String name, int age, char gender, String preferedBerth) {
ArrayList<Booking> result=new ArrayList<Booking>();
char berth='l';
switch(coach)
{
case 1:
if(bookedTicketsAC.size()<2)
{
if(age>=60)
{
berth='l';
}
Booking book=new Booking(bookIdBAC,passId,coach,"L","Confirmed",1000);
Passenger pass=new Passenger(passId,name,age,gender,preferedBerth,noOfTickets,coach,"confirmed",bookIdBAC,from,to);
result.add(book);
bookedTicketsAC.put(bookIdBAC, book);
passengerDetails.put(passId, pass);
bookIdBAC++;
//passId--;
}
else
{
Booking book=new Booking(bookIdWAC,passId,coach,"L","Waiting",1000);
result.add(book);
waitingTicketsAC.put(bookIdWAC, book);
Passenger pass=new Passenger(passId,name,age,gender,preferedBerth,noOfTickets,coach,"Waiting",bookIdWAC,from,to);
passengerDetails.put(passId, pass);
bookIdWAC++;
}
passId++;
break;
case 2:
if(bookedTicketsNonAC.size()<=2)
{
Booking book=new Booking(bookIdBNAC,passId,coach,"L","Confirmed",500);
Passenger pass=new Passenger(passId,name,age,gender,preferedBerth,noOfTickets,coach,"Confirmed",bookIdBNAC,from,to);
result.add(book);
bookedTicketsNonAC.put(bookIdBNAC, book);
passengerDetails.put(passId, pass);
bookIdBNAC++;
}
else
{
Booking book=new Booking(bookIdWNAC,passId,coach,"L","Waiting",500);
result.add(book);
waitingTicketsNonAC.put(bookIdWNAC++, book);
Passenger pass=new Passenger(passId,name,age,gender,preferedBerth,noOfTickets,coach,"Waiting",bookIdWNAC,from,to);
passengerDetails.put(passId, pass);
bookIdWNAC++;
}
passId++;
break;
case 3:
if(bookedTicketsSeater.size()<=60)
{
Booking book=new Booking(bookIdBS,passId,coach,"L","Confirmed",200);
Passenger pass=new Passenger(passId,name,age,gender,preferedBerth,noOfTickets,coach,"confirmed",bookIdBS,from,to);
result.add(book);
bookedTicketsNonAC.put(bookIdBS, book);
passengerDetails.put(passId, pass);
bookIdBS++;
}
else
{
Booking book=new Booking(bookIdWS,passId,coach,"L","Waiting",200);
result.add(book);
waitingTicketsNonAC.put(bookIdWS, book);
Passenger pass=new Passenger(passId,name,age,gender,preferedBerth,noOfTickets,coach,"Waiting",bookIdWS,from,to);
passengerDetails.put(passId, pass);
//passId++;
bookIdWS++;
}
passId++;
break;
}
return result;
// TODO Auto-generated method stub
}
public String checkStatus(int custId) {
String status="invalid id";
Passenger pass=passengerDetails.get(custId);
if(pass!=null)
{
status=pass.getStatus();
}
return status;
}
public Passenger getCustomerDetails(int custId) {
Passenger pass=passengerDetails.get(custId);
System.out.println(pass.getName());
return pass;
}
public int cancelTicket(int custId) {
Passenger pass=passengerDetails.get(custId);
int ticketId=pass.getTicketId();
String status=pass.getStatus();
int coach=pass.getCoachReq();
int amount=0;
if(status.equals("Waiting"))
{
switch(coach)
{
case 1: waitingTicketsAC.remove(ticketId);
amount=1000;
break;
case 2: waitingTicketsNonAC.remove(ticketId);
amount=500;
break;
case 3: waitingTicketsSeater.remove(ticketId);
amount=200;
break;
}
}
else
{
Booking book=null;
int key=0;
switch(coach)
{
case 1: bookedTicketsAC.remove(ticketId);
for(Entry<Integer, Booking> temp:waitingTicketsAC.entrySet())
{
book=temp.getValue();
book.setStatus("Confirmed");
int k=book.getCustId();
Passenger p=passengerDetails.get(k);
p.setStatus("confirmed");
key=temp.getKey();
break;
}
bookedTicketsAC.put(bookIdBAC,book);
bookIdBAC++;
waitingTicketsAC.remove(key);
amount=1000;
break;
case 2: bookedTicketsNonAC.remove(ticketId);
for(Entry<Integer, Booking> temp:waitingTicketsNonAC.entrySet())
{
book=temp.getValue();
book.setStatus("Confirmed");
int k=book.getCustId();
Passenger p=passengerDetails.get(k);
p.setStatus("confirmed");
key=temp.getKey();
break;
}
bookedTicketsAC.put(bookIdBNAC,book);
bookIdBNAC++;
waitingTicketsNonAC.remove(key);
amount=500;
break;
case 3:bookedTicketsSeater.remove(ticketId);
for(Entry<Integer, Booking> temp:waitingTicketsSeater.entrySet())
{
book=temp.getValue();
book.setStatus("Confirmed");
int k=book.getCustId();
Passenger p=passengerDetails.get(k);
p.setStatus("confirmed");
key=temp.getKey();
break;
}
bookedTicketsSeater.put(bookIdBS,book);
bookIdBS++;
amount=200;
waitingTicketsNonAC.remove(key);
break;
}
}
pass.setStatus("Cancelled");
return amount;
}
}
Note: for me they didn't asked about the seat arrangement so i didnt concentrate on it