Sie sind auf Seite 1von 11

AIRLINE CLASS public class AirLine //AirLine class { private Flight flightA; //creating flight objects private Flight

flightB; private Flight flightC; private double flightFareFirst; //declaring variables to be used private double flightFareEcono; private double flightFare; public int numberSeats; String flights[] = {"AL101", "AL555", "AL503"}; //array for flight names String typeFlight[] = {"economy", "first class"}; //array for flight types public AirLine() //default constructor of AirLine { flightA = new Flight("AL101", 5000.00, 10000.00, 100, 20); //instantiation of flight objects flightB = new Flight("AL555", 6000.00, 12000.00, 20, 4); flightC = new Flight("AL503", 8888.00, 22500.00, 200, 30); } public void purchaseTicket(String flightName, String flightType, int qty) //method purchaseTicket { //condition to check if the value of flightName does exist if((flightName==flights[0]) || (flightName==flights[1]) || (flightName==flights[2])) { //condition to check if the value of flightType does exist if((flightType==typeFlight[0]) || (flightType==typeFlight[1])) { if(flightType==typeFlight[0]) //condition to check if flightType is economy { if(flightName==flights[0]) //condition to check if flightname is AL101 { //instantiate value of numberSeats as the return value of method // seatsAvailableEconomy of flightA numberSeats=flightA.seatsAvailableEconomy();

//pass the value of flightType and value of qty to method seatsPurchased of flightA flightA.seatsPurchased(flightType, qty); } else if(flightName==flights[1]) //condition to check if flightname is AL555 { //instantiate value of numberSeats as the return value of method //seatsAvailableEconomy of flightB numberSeats=flightB.seatsAvailableEconomy(); //pass the value of flightType and the value of qty to method seatsPurchased of flightB flightB.seatsPurchased(flightType, qty); } else if(flightName==flights[2]) //condition to check if flightname is AL503 { //instantiate value of numberSeats as the return value of method //seatsAvailableEconomy of flightC numberSeats=flightC.seatsAvailableEconomy(); //pass the value of flightType and the value of qty to method seatsPurchased of flightC flightC.seatsPurchased(flightType, qty); } //condition to check if the value of numberSeats is greater than or equal to the value of qty if(numberSeats>=qty) System.out.println("You've successfully bought " + qty + " ticket/s for a " + typeFlight[0] + " flight" + "\n"); else //executes when the value of qty is greater than the value of numberSeats System.out.println("The number of seats you are trying to buy in flight type: " + typeFlight[0] + ", exceeds the number of its available seats" + "\n"); } if(flightType==typeFlight[1]) //condition to check if flightType is first class { if(flightName==flights[0]) //condition to check if flightName is AL101 { //instantiate value of numberSeats as value of the return value of method //seatsAvailableFirstClass of flightA

numberSeats=flightA.seatsAvailableFirstClass(); //pass the value of flightType and qty to seatsPurchased of flightA flightA.seatsPurchased(flightType, qty); } else if(flightName==flights[1]) //condition to check if flightName is AL555 { //instantiate value of numberSeats as the return value of method //seatsAvailableFirstClass of flightB numberSeats=flightB.seatsAvailableFirstClass(); //pass the value of flightType and value of qty to method seatsPurchased of flightB flightB.seatsPurchased(flightType, qty); } else if(flightName==flights[2]) //condition to check if flightName is AL503 { //instantiate value of numberSeats as the return value of method //seatsAvailableFirstClass of flightC numberSeats=flightC.seatsAvailableFirstClass(); //pass the value of flightType and the value of qty to method seatsPurchased of flightC flightC.seatsPurchased(flightType, qty); } //condition to check if the value of numberSeats is greater than or equal the value of qty if(numberSeats>=qty) System.out.println("You've successfully bought " + qty + " ticket/s for a " + typeFlight[1] + " flight" + "\n"); else //executes when the value of qty is greater than the value of numberSeats System.out.println("The number of seats you are trying to buy in flight type: " + typeFlight[1] + ", exceeds the number of its available seats" + "\n"); } } else //executes when the value of flightType does not exist System.out.println("Please choose only between economy and first class flights " + "\n" + "Transaction unsuccessful" + "\n"); } else //executes when the value of flighName does not exist

System.out.println("Sorry, but no such flight exists, choose from either AL101, AL555 or AL503 " + "\n" + "Transaction unsuccessful" + "\n"); } //method getTicketPrice; this method gets the ticket price of the purchased ticket public double getTicketPrice(String flightName, String flightType) { Flight flightObject[] = {flightA, flightB, flightC}; //array for Flight objects if(flightType==typeFlight[0]) //condition to check if the type of flight is economy { for(int x=0;x<3;x++) { //condition to check if the flighName is in the array flights if(flightName==flights[x]) flightFareEcono=flightObject[x].flightFareEconomy(); //sets the value of //flightFareEcono as the return value of method flightFareEconomy of the flight object //set s the value of flightFare as the value of flightFareEcono flightFare=flightFareEcono; } } else if(flightType==typeFlight[1]) //condition to check if the type of flight is first class { for(int x=0;x<3;x++) { if(flightName==flights[x]) //condition to check if the flighName is in the array flights flightFareFirst=flightObject[x].flightFareFirstClass(); //sets the value of //flightFareFirst as the return value of method flightFareFirstClass of the flight object flightFare=flightFareFirst; value of flightFareFirst } } return flightFare; } public void printStats() //sets the value of flightFare as the

//return the value of flightFare

//method for printStats

{ flightA.printStats(); of the flight objects flightB.printStats(); flightC.printStats(); } } //invokes the method printStats

TICKETCOUNTER CLASS public class TicketCounter { private AirLine airLine; private int ticketCounter; private int numberTicketsSoldFirst; private int numberTicketsSoldEcono; private double salesFirstClass; private double salesEconomy; private double sales; //TicketCounter class //creates AirLine object //declaring variables to be used

//constructor method of TicketCounter with 2 parameters public TicketCounter(int ticketCounterNumber, AirLine airline) { //sets the value of ticketCounter as the value of ticketCounterNumber ticketCounter = ticketCounterNumber; airLine = airline; //sets the value of airLine as airline } public void purchaseTicket(String flightName, String flightType, int qty) //method purchaseTicket

{ //invokes the purchaseTicket method of airLine airLine.purchaseTicket(flightName, flightType, qty); //condition to check if the value of flightName does exist if((flightName=="AL101") || (flightName=="AL555") || (flightName=="AL503")) { if(flightType=="economy") //condition to check if the value of flightType is economy { //condition to check if the value of qty is less than or equal to value of numberSeats of airLine if(qty<=airLine.numberSeats) { //sets the value of numberTicketsSoldEcono as the value of qty numberTicketsSoldEcono=qty; //sets the value of salesEconomy as the return value of method getTicketPrice of airLine //multiply by the value of numbeTicketsSoldEcono salesEconomy=airLine.getTicketPrice(flightName, flightType)*numberTicketsSoldEcono; } } if(flightType=="first class") //condition to check if the value of flightType is first class { //condition to check if the the value of qty is less than or equal to value of numberSeats of airLine if(qty<=airLine.numberSeats) { //sets the value of numberTicketsSoldFirst as the value of qty numberTicketsSoldFirst=qty; //sets the value of salesFirstClass as the method return value of getTicketPrice of airLine //multiply by the value of numbeTicketsSoldFirst salesFirstClass=airLine.getTicketPrice(flightName, flightType)*numberTicketsSoldFirst; } } //sets the value of sales as the value of salesFirstClass plus the value of salesEconomy sales=salesFirstClass+salesEconomy; }

} //method printStats; this method prints the statistics of every ticketCounter public void printStats() { System.out.println("Statistics for ticketCounter " + ticketCounter); System.out.print("Total sales: "); System.out.println(String.format("%.2f",sales)); System.out.println("Economy tickets sold: " + numberTicketsSoldEcono); System.out.println("First Class tickets sold: " + numberTicketsSoldFirst + "\n"); } }

FLIGHT CLASS public class Flight { public String flightName; public double flightFareFirst; public double flightFareEcono; public int seatsAvailableFirst; public int seatsAvailableEcono; private int seatsPurchasedEcono; private int seatsPurchasedFirst; //Flight class //declaring variables to be used

//constructor method of Flight with 4 parameters public Flight(String flightName, double flightFareEcono, double flightFareFirst, int seatsEcono, int seatsFirst) {

//sets the value of flightName of Flight class as the value of flightName passed in this constructor this.flightName=flightName; //sets the value of flightFareEcono of Flight class as the value of flightFareEcono passed in this constructor this.flightFareEcono=flightFareEcono; //sets the value of flightFareFirst of Flight class as the value of flightFareFirst passed in this constructor this.flightFareFirst=flightFareFirst; //sets the value of seatsAvailableEcono as the value of seatsEcono seatsAvailableEcono=seatsEcono; //sets the value of seatsAvailableFirst as the value of seatsFirst seatsAvailableFirst=seatsFirst; } //method flightName; this method is for getting the value of flightName public String flightName() { return flightName; //returns the value of flightName } //method seatsAvailableFirstClass; this method is for getting the seats available for flight type first class public int seatsAvailableFirstClass() { return seatsAvailableFirst; //returns the value of seatsAvailableFirst } //method seatsAvailableEconomy; this method is for getting the seats available for flight type economy public int seatsAvailableEconomy() { return seatsAvailableEcono; //returns the value of seatsAvailableEcono } //method flightFareFirstClass; this method is for getting the fare for flight type first class public double flightFareFirstClass() { return flightFareFirst; //returns the value of flighFareFirst

} //method flightFareEconomy; this method is for getting the fare for flight type economy public double flightFareEconomy() { return flightFareEcono; //returns the value of flightFareEcono } public void seatsPurchased(String flightType, int qty) //method seatsPurchased { if(flightType=="economy") //condition to check if the value of flightType is economy { //condition to check if the value of qty is less than or equal to value of seatsAvailableEcono if(qty<=seatsAvailableEcono) { //sets the value of seatsAvailableEcono as the value of seatsAvailableEcono minus value of qty seatsAvailableEcono-=qty; //sets the value of seatsPurchasedEcono as the value qty seatsPurchasedEcono=qty; } }

else if(flightType=="first class") //condition to check if the value of flightType is first class { //condition to check if the value of qty is less than or equal to seatsAvailableFirst if(qty<=seatsAvailableFirst) { //sets the value of seatsAvailableFirst as seatsAvailableFirst minus qty seatsAvailableFirst-=qty;

seatsPurchasedFirst=qty; seatsPurchasedFirst as the value qty } } }

//sets the value of

public void printStats() //method printStats { System.out.println("Flight " + flightName() + " has " + seatsPurchasedEcono + " economy tickets sold, leaving " + seatsAvailableEcono + " seats remaining"); System.out.println("Flight " + flightName() + " has " + seatsPurchasedFirst + " first class tickets sold, leaving " + seatsAvailableFirst + " seats remaining" + "\n"); } }

New Era University College of Engineering and Technology Computer Science Department

CS 334 Lab Object Oriented Programming


M / 4:00pm 7:00pm

Joshua Miguel A. Arcebuche Roger C. Baltazar

Irysh Paulo Tipay Professor

Das könnte Ihnen auch gefallen