2 Replies - 542 Views - Last Post: 03 August 2013 - 09:12 PM Rate Topic: -----

#1 atamimi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-August 13

Recursive error, need help fixing my code

Posted 03 August 2013 - 04:11 PM

So i have a homework assignment that is kinda hard, heres what it requires me to do:
Airline Reservation
You must use Object Oriented Programming to design this project.

Write a reservation system for an airline flight. Assume the airplane has 10 rows with 4 seats in each row. Use a two dimensional array of strings to maintain a seating chart. In addition create a array to be used as a waiting list in case the plane is full. The waiting list should be "first come first serve" that is people who are added early in the list get the priority over those who are added later. Allow the user to add three options

1. Add a passenger to the list.
a. Request the passenger's name.
b. Display a chart of the seats in the airplane in a tabular form
c. If seats are available let the passenger choose a seat. Add the passenger to the seating chart.
d. If no seats are available, place the passenger on the waiting list.

2. Remove a passenger from the list
a. Request the passengers name
b. Search the seating chart for the passenger's name and delete it.
c. If the waiting list is empty, update the array so that the seat is available,
d. If the waiting list is not empty remove the first person from the list, and give him or her the newly vacated seat.

3. Quit




While I feel like I have most of the objects correct, I am coming across errors, here is my current code, following this format:Flight
SeatingChart sc;
WaitingList wl;
AddPassenger()
RemovePassenger()

SeatingChart
aPassenger [] pass = new Passenger[40];
aprintSeatingChart()
agetSeatChoicefromUser() //prompts the user to enter which seat they want to occupy.
asetPassenger()
getPassenger() //given the location in Seating Chart.
asearch()
aremovePassenger()
//delete passenger from seating chart
areconcileWithWaitingList()
//If the waiting list is not empty remove the first person from the list, and give him or her the newly vacated seat.
aaddPassengerToWaitingList()


WaitingList
Passenger [] waitlist = new WaitingList[10];
printWaitingList()
setPassenger() //FIFO
getPassenger()

Passenger
Instance Variables
FirstName
LastName
getName()
setFirstName()
setLastName()


import java.util.StringTokenizer;


public class Flight extends SeatingChart{
	SeatingChart sc;
	WaitingList wl;

	Flight(){
		sc= new SeatingChart();
		wl=new WaitingList();}
	void AddPassenger(Flight sw101){
		sw101.sc.printSeatingChart();
		String seat=sw101.sc.getSeatChoicefromUser();
		StringTokenizer strToken=new StringTokenizer(seat);
		int row= Integer.parseInt(strToken.nextToken());
		int col= Integer.parseInt(strToken.nextToken());
		sw101.sc.addPassengerToWaitingList(row,col);
			}
	void RemovePassenger(){}
	public static void main(String[] args)
	{
		Flight sw101= new Flight();
		sw101.sc.printSeatingChart();
		sw101.AddPassenger(sw101);
	}
	}
	



import java.io.Console;
    import java.util.Scanner;
    
    public class SeatingChart extends WaitingList {
    	Passenger[][] pass = new Passenger[10][4];
    
    	SeatingChart() {
    		for (int row = 0; row < 10; row++) {
    			for (int col = 0; col < 4; col++) {
    				pass[row][col] = new Passenger();
    			}
    		}
    	}
    
    	void printSeatingChart() {
    		for (int row = 0; row < 10; row++) {
    			for (int col = 0; col < 4; col++) {
    				System.out.print(pass[row][col].getFirst() + "\t");
    				System.out.print(pass[row][col].getLast() + "\n");
    			}
    		}
    	}
    
    	String getSeatChoicefromUser() {
    		Scanner scanner = new Scanner(System.in);
    		System.out.print("Enter preferred seat (row, column): ");
    		String seat = scanner.next();
    		return seat;
    	}
    
    	void setPassenger(int row, int col, Passenger temppass) {
    		pass[row][col] = temppass;
    	}
    
    	Passenger getPassenger(int row, int col) {
    		return pass[row][col];
    	}
    
    	Passenger search(String first, String last) {
    		Passenger search = new Passenger();
    		for (int row = 0; row < 10; row++) {
    			for (int col = 0; col < 4; col++) {
    				String tempfirst = pass[row][col].getFirst();
    				String templast = pass[row][col].getLast();
    				if (tempfirst.equals(first)) {
    					if (templast.equals(last)) {
    						search = pass[row][col];
    						removePassenger(row, col);
    					}
    				} else {
    					search = null;
    					System.out.print("\nPassenger not found\n");
    				}
    			}
    		}
    		return search;
    	}
    
    	void removePassenger(int row, int col) {
    		pass[row][col].setFirstName(null);
    		pass[row][col].setLastName(null);
    	}
    
    	Passenger reconcileWithWaitingList() {
    		int i = 0;
    		Passenger pass = new Passenger();
    		String tempfirst = waitlist[i].getFirst();
    		String templast = waitlist[i].getLast();
    		if (tempfirst.equals(null)) {
    			if (templast.equals(null))
    				System.out.print("\nWaiting list is empty\n");
    		} else {
    			pass = waitlist[i];
    			for (i = 0; i < 10; i++)
    				waitlist[i] = waitlist[i + 1];
    		}
    		return pass;
    	}
    
    	public void addPassengerToWaitingList(int wrow, int wcol) {
    		Passenger addPass = new Passenger();
    		Scanner sc = new Scanner(System.in);
    		System.out.print("Enter first name: ");
    		String tempfirst = sc.next();
    		System.out.print("Enter last name: ");
    		String templast = sc.next();
    		addPass = search(tempfirst, templast);
    		if (addPass == null) {
    			String tempfirst1 = pass[wrow][wcol].getFirst();
    			String templast1 = pass[wrow][wcol].getLast();
    			if (tempfirst1.equals(null)) {
    				if (templast1.equals(null)) {
    					pass[wrow][wcol].setFirstName(tempfirst1);
    					pass[wrow][wcol].setLastName(templast1);
    				}
    			}
    		} else
    			System.out.print("Passenger already present!");
    	}
    
    	public static void main(String[] args) {
    	}
    }


  public class WaitingList extends Passenger {
    	Passenger[] waitlist = new WaitingList[10];
    
    	WaitingList(){
    		for(int i = 0; i<10;i++){
    			waitlist[i]=new WaitingList();
    			waitlist[i].setFirstName("");
    			waitlist[i].setLastName("");
    		}}
    
    	void printWaitingList() {
    		for (int j = 0; j < 10; j++)
    			waitlist[j].printNames();
    	}
    
    	void setPassenger(int i, Passenger tempPass) {
    		waitlist[i] = tempPass;
    	}
    
    	Passenger getPassenger(int i) {
    		return waitlist[i];
    	}
    
    	public static void main(String[] args) {
    	}
    
    }

public class Passenger {
    	String FirstName;
    	String LastName;
    
    	void getName() {
    		FirstName = " ";
    		LastName = " ";
    	}
    
    	void getName(String first, String last) {
    		FirstName = first;
    		LastName = last;
    	}
    
    	String getFirst() {
    		return FirstName;
    	}
    
    	String getLast() {
    		return LastName;
    	}
    
    	void setFirstName(String tempfirst) {
    		FirstName = tempfirst;
    	}
    
    	void setLastName(String templast) {
    		LastName = templast;
    	}
    
    	void printNames() {
    		System.out.printf("%s %s\n", this.getFirst(), this.getLast());
    	}
    
    	public static void main(String[] args) {
    	}
    }




Is there something wrong with my inheritence? should i follow his outline? any help or tips would be appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Recursive error, need help fixing my code

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Recursive error, need help fixing my code

Posted 03 August 2013 - 05:26 PM

Firstly, please format your code.

The inheritance tree is incorrect. A waiting list is not a type of passenger, and a seating chart is not type of waiting list. It seems that are you're forcing the inheritance, creating more than is necessary. Model this as it would normally be in real life. Many times locating the nouns in a specification, clues you into the classes needed.

Quote

Write a reservation system for an airline flight. Assume the airplane has 10 rows with 4 seats in each row. Use a two dimensional array of strings to maintain a seating chart. In addition create a array to be used as a waiting list in case the plane is full. The waiting list should be "first come first serve" that is people who are added early in the list get the priority over those who are added later. Allow the user to add three options

1. Add a passenger to the list.
a. Request the passenger's name.
b. Display a chart of the seats in the airplane in a tabular form
c. If seats are available let the passenger choose a seat. Add the passenger to the seating chart.
d. If no seats are available, place the passenger on the waiting list.

2. Remove a passenger from the list
a. Request the passengers name
b. Search the seating chart for the passenger's name and delete it.
c. If the waiting list is empty, update the array so that the seat is available,
d. If the waiting list is not empty remove the first person from the list, and give him or her the newly vacated seat.


class ReservationSystem{ Flight[] flights; }
class Flight { Passenger seats[]; Passenger waiting[]; }
class Passenger { String name; }

This post has been edited by blackcompe: 03 August 2013 - 05:27 PM

Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Recursive error, need help fixing my code

Posted 03 August 2013 - 09:12 PM

and really don't see any place where recursivity would be required
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1