Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 5

package com;

import java.util.*;
import java.lang.*;
class Vehicle {
private String vName;
private int vNo;
private double totalFare;
private int availableNoOfSeats;
public Vehicle()
{
}
public Vehicle (String s, int a, double b,int c)
{
vName=s;
vNo=a;
totalFare=b;
availableNoOfSeats=c;
}
public double getTotalFare()
{
return totalFare;
}
public void setTotalFare(double a)
{totalFare=a;
}
public String getvName()
{
return vName;
}
public void setvName(String a)
{vName=a;
}
public int getvNo()
{
return vNo;
}
public void setvNo(int a)
{
vNo=a;
}
public int getAvailableNoOfSeats()
{
return availableNoOfSeats;
}
public void setAvailableNoOfSeats(int a)
{
availableNoOfSeats=a;
}
}
abstract class VehicleBooking
{
private int bookingId;
private int vehicleId;
private String custId;
private String dateOfJourney;
private int noOfSeatsToBook;
public int getVehicleId()
{

return vehicleId;
}
public void setVehicleId(int a)
{
vehicleId=a;
}
public void setBookingId(int a)
{
bookingId=a;
}
public int getBookingId()
{
return bookingId;
}
public int getNoOfSeatsToBook()
{
return noOfSeatsToBook;
}
public void setNoOfSeatsToBook(int a)
{
noOfSeatsToBook=a;
}
public String getCustId()
{
return custId;
}
public void setCustId(String a)
{
custId=a;
}
public String getDateOfJourney()
{
return dateOfJourney;
}
public void setDateOfJourney(String a)
{
dateOfJourney=a;
}
public abstract VehicleBooking makeBooking(String s,ArrayList<Vehicle>
al,int a,int b, String s1);
public abstract boolean cancelBooking(ArrayList<VehicleBooking> al, int
a);
}
class BusBooking extends VehicleBooking
{
@Override
public VehicleBooking makeBooking
a,

int b, String s1) {


int count=0;
Vehicle v;

(String s, ArrayList<Vehicle> al, int

try
{
for(int i=0;i<al.size();i++)
{v=(Vehicle)al.get(i);
if(v.getvNo()== b)
{
count++;
if(v.getAvailableNoOfSeats()<a)
{
throw new NoBookingAllowedException(a+"is greater
than avaliable no of seats");
}

the list");

}
if(count==0)
{
throw new NoBookingAllowedException(b+"is not present in
}
}
catch (Exception e)
{
e.printStackTrace();
}
for(int i=0;i<al.size();i++)
{v=(Vehicle)al.get(i);
if(v.getvNo()== b)
{int c=v.getAvailableNoOfSeats()-a;
v.setAvailableNoOfSeats(c);
setBookingId(i);
setCustId(s);
setDateOfJourney(s1);
setVehicleId(b);
setNoOfSeatsToBook(a);
new GreenTrip().bookingList.add(this);
}

}
return this;

@Override
public boolean cancelBooking(ArrayList<VehicleBooking> al, int a) {
for(int i=0;i<al.size();i++)
{VehicleBooking v=(VehicleBooking)al.get(i);
if(v.getBookingId()==a)
return true;
}
return false;
}
}
class NoBookingAllowedException extends Exception
{
NoBookingAllowedException(String a)
{
System.out.println(a);
}
}

public class GreenTrip {


static ArrayList<Vehicle> vList=new ArrayList<Vehicle>();
static ArrayList<VehicleBooking> bookingList=new
ArrayList<VehicleBooking>();
public static void main(String[] args) {
Vehicle v=new Vehicle("vbus",2345,100,42);
Vehicle v1=new Vehicle("cbus",2346,120,42);
Vehicle v2=new Vehicle("dbus",2347,200,42);
addVehicle(v);
addVehicle(v1);
addVehicle(v2);
BusBooking bb=new BusBooking();
bb.makeBooking("121", vList, 43, 2345, "02jun2016");
bb.cancelBooking(bookingList, 1003);
getUniqueCustId();
makeSeatOccupied(2345,20);
HashMap hs=bookingLookUpTable();
TreeSet ts= getVehicleSet();
}
public static void addVehicle(Vehicle v)
{
vList.add(v);
}
public static HashSet<Integer> getUniqueCustId()
{
HashSet<Integer> hs=new HashSet<Integer>();
for (int i=0;i<bookingList.size();i++)
{VehicleBooking bl=(VehicleBooking)bookingList.get(i);
Integer j=new Integer(0);
j.parseInt(bl.getCustId());
hs.add(j);
}
return hs;
}
public static void makeSeatOccupied(int a ,int b)
{for(int i=0;i<vList.size();i++)
{Vehicle v=(Vehicle)vList.get(i);
if(v.getvNo()== a)
{
v.setAvailableNoOfSeats(b);
break;
}
}
}
public static HashMap<Integer,Vehicle> bookingLookUpTable()
{
HashMap hm=new HashMap();
for(int i=0;i<bookingList.size();i++)
{VehicleBooking bl=(VehicleBooking)bookingList.get(i);
for(int j=0;j<vList.size();i++ )
{
Vehicle v=(Vehicle)vList.get(j);
if(v.getvNo()== bl.getVehicleId())
{
hm.put(bl.getBookingId(), v);
}
}
}

return hm;

}
public static TreeSet<Vehicle> getVehicleSet()
{
ArrayList<Vehicle> vl=new ArrayList<Vehicle>();
TreeSet<Vehicle> ts=new TreeSet<Vehicle>();
for(int i=0;i<vList.size();i++)
{Vehicle v=(Vehicle)vList.get(i);
try
{
Integer j=new Integer(0);
j.parseInt(v.getvName());
if(v.getvNo()!=j )
{
vl.add(v);
}
}
catch (Exception e)
{
vl.add(v);
}
}
int a=vl.size();
Integer a1[]=new Integer[a];
for(int i=0;i<vl.size();i++)
{
Vehicle v=(Vehicle)vl.get(i);
a1[i]=new Integer(v.getvNo());
}
for(int j=0;j<a1.length;j++)
{
for(int i=0;i<a1.length;j++)
{
if(a1[i]>a1[j])
{
int s=a1[i];
a1[i]=a1[j];
a1[j]=a1[i];
}
}
}
for(int i=0;i<vl.size();i++)
{for(int j=0;j<vl.size();j++)
{
Vehicle v=(Vehicle)vl.get(j);
if(a1[i]==v.getvNo())
{
ts.add(v);
}
}
}
TreeSet<Vehicle>ts1=new TreeSet<Vehicle>(ts.descendingSet());
return ts1;
}
}

You might also like