Question
JAVA HELP URGGENT: In a bad way getting down to the wire and I have no Idea how to get my main to work correctly
JAVA HELP URGGENT: In a bad way getting down to the wire and I have no Idea how to get my main to work correctly with the file IO file in:
My code:
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileNotFoundException; import java.io.IOException; import java.util.*;
public class ExpanseTester {
static void displayMenu() { System.out.println("1: Add traveler to a Spaceship from the AvailableTravelers object"); System.out.println("2: Search for Traveler on StarShip"); System.out.println("3: Move SpaceShip to new Location"); System.out.println("4: Displays a list of Available Travelers"); System.out.println("5: Display a list of Travelers on a specific SpaceShip (Id by name)"); System.out.println("6: Exit"); System.out.println("Enter choice (1-6) "); }
public static void main(String args[]) throws IOException {
int numberOfShips = 2; int numberOfTravelers = 4; Traveler travelers[] = new Traveler[numberOfTravelers]; Spaceship spaceShips[] = new Spaceship[numberOfShips]; Scanner scanner= new Scanner(System.in); String name, seatCat; String currLocation,destLocation; Location current,destination; int capacity,tripLength, rewardsPoints; double cost; SeatCategory seat;
for(int i = 1;i <= numberOfTravelers; i++) { System.out.println("Data for Traveler "+i+" "); System.out.print("Name: "); name = scanner.next(); System.out.print("Seat Category(ALL CAPS): "); seatCat = scanner.next(); seat = SeatCategory.valueOf(seatCat); System.out.print("Current(ALL CAPS): "); currLocation = scanner.next(); current = Location.valueOf(currLocation); travelers[i-1] = new Passenger(); System.out.println("Traveler " + i + " "+travelers[i-1]); }
for(int i = 1;i <= numberOfShips; i++) { System.out.println("Data for SpaceShip " + i + " "); System.out.print("Name : "); name = scanner.next(); System.out.print("Current : "); currLocation = scanner.next(); current = Location.valueOf(currLocation); System.out.print("Destination : "); destLocation = scanner.next(); destination = Location.valueOf(destLocation); System.out.print("Capacity : "); capacity = scanner.nextInt(); spaceShips[i - 1] = new Spaceship(name,current,capacity); System.out.println("Spaceship " + i + " " + spaceShips[i - 1]);
}
int inputChoice; String travelerName, shipName; int indexOfShip = -1, indexOfTraveler = -1; boolean doExit = false; while(true) {
displayMenu(); inputChoice = scanner.nextInt();
switch (inputChoice) { case 1: indexOfShip = -1; indexOfTraveler = -1; System.out.print("Boarding Traveler "); travelerName = scanner.next(); System.out.print("Boarding Ship "); shipName = scanner.next(); for (int i = 0; i < numberOfShips; i++) { if (spaceShips[i].getName().compareTo(shipName) == 0) { indexOfShip = i; } } for (int i = 0; i < numberOfTravelers; i++) { if (travelers[i].getName().compareTo(travelerName) == 0) { indexOfTraveler = i; } } if (indexOfShip != -1 && indexOfTraveler != -1) { if (spaceShips[indexOfShip].board(travelers[indexOfTraveler])) { System.out.println("Successful!"); System.out.println(travelers[indexOfTraveler]); System.out.println(spaceShips[indexOfShip]); } else { System.out.println("Unsuccessful!"); } } else { System.out.println("Unsuccessful!"); } break; case 2: indexOfShip = -1; indexOfTraveler = -1; System.out.print("Searching for Traveler "); travelerName = scanner.next(); System.out.print("Searching in Ship "); shipName = scanner.next(); for (int i = 0; i < numberOfShips; i++) { if (spaceShips[i].getName().compareTo(shipName) == 0) { indexOfShip = i; } } for (int i = 0; i < numberOfTravelers; i++) { if (travelers[i].getName().compareTo(travelerName) == 0) { indexOfTraveler = i; } } if (indexOfShip != -1 && indexOfTraveler != -1) { if (spaceShips[indexOfShip].isOnBoard(travelers[indexOfTraveler])) { System.out.println("true!"); System.out.println(travelers[indexOfTraveler]); System.out.println(spaceShips[indexOfShip]); } else { System.out.println("false!"); } } else { System.out.println("false!"); } break; case 3: indexOfShip = -1; indexOfTraveler = -1; System.out.print("Moving Ship "); shipName = scanner.next(); for (int i = 0; i < numberOfShips; i++) { if (spaceShips[i].getName().compareTo(shipName) == 0) { indexOfShip = i; } } System.out.println(spaceShips[indexOfShip]); spaceShips[indexOfShip].move(true); System.out.println(spaceShips[indexOfShip]); break; case 4: break; case 5: break; case 6: doExit = true; break; default: System.out.println("Invalid inputChoice");
} if(doExit) { break; } } } }
public class AvailableTravelers { private Traveler travelers[]; private int numOfTravelers; public AvailableTravelers() { travelers = new Traveler[20]; this.numOfTravelers = 0; } public AvailableTravelers(int capacity) { travelers = new Traveler[capacity]; this.numOfTravelers = 0; } public boolean addTraveler(Traveler t) { if (numOfTravelers >= travelers.length) return false; for (int i=0; i public class Crew extends Traveler { private String position; private int flightHours; public Crew() { super(); this.position = null; this.flightHours = 0; } public Crew(String name, Location location, int flightHours, String position) { super(name, location); this.flightHours = flightHours; this.position = position; } public void setPosition(String position) { this.position = position; } public String getPosition() { return this.position; } public void setFlightHours(int flightHours) { this.flightHours = flightHours; } public int getFlightHours() { return this.flightHours; } @Override public void addFlightToHistory(int flightDuration) { flightHours = flightHours + flightDuration; } @Override public String toString() { return "Crew [name = " + getName() + ", id = " + getId() + ", current = " + getLocation() + ", position = " + getPosition() + ", flight hours = " + getFlightHours() + "]"; } } public class Passenger extends Traveler { private double cost; SeatCategory seat; private int rewardsPoints; public Passenger() { super(); cost = 0.0; rewardsPoints = 0; } public Passenger(String name, Location location, double cost, SeatCategory seat, int rewardsPoints) { super(name, location); this.seat = seat; this.cost = cost; this.rewardsPoints = rewardsPoints; if(seat == SeatCategory.FIRST) { cost = 1499.99; }else if(seat == SeatCategory.BUSINESS) { cost = 998.00; }else if (seat == SeatCategory.COACH) { cost = 449.00; }else { cost = 0.00; } } public void setSeat(SeatCategory seat) { this.seat = seat; if(seat == SeatCategory.FIRST) { cost = 1499.99; }else if(seat == SeatCategory.BUSINESS) { cost = 998.00; }else if (seat == SeatCategory.COACH) { cost = 449.00; }else { cost = 0.00; } } public void setRewardsPoints(int rewardsPoints) { this.rewardsPoints = rewardsPoints; } public double getCost() { return this.cost; } public SeatCategory getSeat() { return this.seat; } public int getRewardsPoints() { return this.rewardsPoints; } private void rewardUpgrade() { if(getSeat() != SeatCategory.FIRST && this.rewardsPoints >= 10000) { seat.equals(SeatCategory.FIRST); rewardsPoints = rewardsPoints - 10000; }else if(getSeat() == SeatCategory.FIRST) { cost = cost - (0.2 * cost); } } @Override public void addFlightToHistory(int flightDuration) { rewardsPoints = rewardsPoints + (10 * flightDuration); } @Override public String toString() { return "Passenger [name = "+ getName() +", id = " + getId() + ", current = " + getLocation() + ", seat = " + getSeat() + ", cost = " + getCost() + ", rewardsPoints = " + getRewardsPoints() +"]"; } } abstract class Traveler { public static int nextIDNum = 0; private String name; private static int paxCreated = 0; private int id; private Location location; public Traveler () { this.name = null; this.location = null; nextIDNum = 1; this.id = nextIDNum; nextIDNum ++; } public Traveler (String name, Location location) { this.name = name; this.location = location; this.id = ++Traveler.paxCreated; nextIDNum = 1; id = nextIDNum; nextIDNum++; } public void setLocation (Location l) { this.location = l; } public Location getLocation () { return this.location; } public String getName () { return this.name; } public void setId() { id = nextIDNum; nextIDNum++; } public int getId () { return this.id; } public String toString () { return "[name = "+ this.name +", id = " + this.id + ", current = " + this.location + "]"; } public abstract void addFlightToHistory(int flightDuration) ; } import java.util.*; public class Spaceship { private String name; private int capacity; private int numTraverlers; private ArrayList public Spaceship() { this.name = null; this.current = null; this.destination = null; this.tripLength = 0; this.capacity = 10; this.travelers = new ArrayList<>(capacity); } public Spaceship(String name) { this.name = name; this.current = null; this.destination = null; this.tripLength = 0; this.capacity = 10; this.travelers = new ArrayList<>(capacity); } public Spaceship(String name, Location current, int capacity) { this.name = name; this.current = null; this.destination = null; this.tripLength = 0; this.capacity = 10; this.travelers = new ArrayList<>(capacity); } public Spaceship(String name, int capacity, Location current, Location destination, int tripLength) { this.name = name; this.capacity = capacity; this.current = current; this.destination = destination; this.tripLength = tripLength; travelers = new ArrayList<>(capacity); } public String getName() { return name; } public int getCapacity() { return capacity; } public Location getCurrent() { return current; } public Location getDestination() { return destination; } public int getTripLength() { return tripLength; } public void setTripLength(int tripLength) { this.tripLength = tripLength; } public void setName(String name) { this.name = name; } public void setCapacity(int capacity) { this.capacity = capacity; } public void setCurrent(Location current) { this.current = current; } public void setDestination(Location destination) { this.destination = destination; } public boolean board(Traveler t) { if(numTraverlers public boolean leave(Traveler t) { if(isOnBoard(t)) { travelers.remove(t); numTraverlers--; return true; } return false; } public boolean isOnBoard(Traveler t) { if(travelers.contains(t)) { return true; } return false; } public boolean move(boolean isMovedAllowed) { current = destination; destination = null; for(Traveler t:travelers) { t.setLocation(current); //Calling addFlightToHistory() method for traveler on board t.addFlightToHistory(getTripLength()); } travelers.clear(); //If isMovedAllowed is true if(isMovedAllowed) return true; //returns false if isMovedAllowed is false else return false; } public String toString() { //String to demonstrate Spaceship class String string_to_return="Spaceship [name = " + getName() + ", current = " + getCurrent() + ", destination = " + getDestination() + ", capacity = " + getCapacity() + ",travelers = " + numTraverlers + ", tripLength = " + getTripLength() + "]"; for(int i = 0; i <= numTraverlers; i++) { //This would display all the details of passengers on board System.out.println(travelers.get(i).toString()); } //System.out.printf() should not be used. Instead return a String return string_to_return; } } public enum SeatCategory { NOSEAT, FIRST, BUSINESS, COACH } public enum Location { EARTH, BELT, MARS, RING } ExpanceTester - with a main method that completely tests the Travelers and Spaceship classes. 1. Initially the program creates an AvailableTravelers object and populates it with 1. 10 Travelers of which 4 are Passenger and 6 are Crew members (2 Captains, 2 Co-Captains and 2 crew members) 2. Two Space Ship with no travelers. 2. Develop a text-based menu system similar to Project 2 . 1. Executes the text-menu with the following options (Parts 2-4 below) 1. Add traveler to a Spaceship from the AvailableTravelers object 2. Search for traveler on a Spaceship 3. Move a spaceship to a new location 4. Displays a list of Available Travelers 5. Display of list of Travelers on a specific SpaceShip (Id by name) 6. Exit the program Sample program run Passenger (p) or Crew (c)? p Name: Lupe Fiasco Location: MARS Seat class (COACH, BUSINESS, or FIRST): FIRST Rewards points: 12345 Passenger (p) or Crew (c)? p Name: Drake Location: EARTH Seat class (COACH, BUSINESS, or FIRST): COACH Rewards points: 10000 Passenger (p) or Crew (c)? p Name: Dave Matthews Location: MARS Seat class (COACH, BUSINESS, or FIRST): BUSINESS Rewards points: 1345 Passenger (p) or Crew (c)? p Name: Peter Cetera Location: EARTH Seat class (COACH, BUSINESS, or FIRST): COACH Rewards points: 41235 Passenger (p) or Crew (c)? p Name: Roy Orbison Location: BELT Seat class (COACH, BUSINESS, or FIRST): BUSINESS Rewards points: 24318 Passenger (p) or Crew (c)? p Name: Dirks Bentley Location: RING Seat class (COACH, BUSINESS, or FIRST): COACH Rewards points: 345 Passenger (p) or Crew (c)? c Name: Notorious B.I.G Location: BELT Position: Captain Flight hours: 9724 Passenger (p) or Crew (c)? c Name: George Strait Location: RING Position: Co-pilot Flight hours: 1398 Passenger (p) or Crew (c)? c Name: Ringo Starr Location: RING Position: Captain Flight hours: 8348 Passenger (p) or Crew (c)? c Name: Eddie Vedder Location: MARS Position: Co-pilot Flight hours: 1832 Passenger (p) or Crew (c)? - Last passenger has been entered Name of ship: USS Chicago Current location: MARS Destination: EARTH Capacity: 2 Trip Duration: 328 Name of ship: USS Louisville Current location: BELT Destination: RING Capacity: 3 Trip Duration: 127 Name of ship: ----- Last ship has been entered ------------------------------------------- 1) Add traveler to a Spaceship from the AvailableTravelers object 2) Search for traveler on a Spaceship 3) Move a spaceship to a new location 4) Displays a list of Available Travelers 5) Display of list of Travelers on a specific SpaceShip (Id by name) 6) Exit the program Choose an option from above (1-6): 4 List all travelers [name=Lupe Fiasco, id=1, current=MARS, seat=FIRST, cost=$1199.99, rewardsPoints=2345] [name=Drake, id=2, current=EARTH, seat=FIRST, cost=$1499.99, rewardsPoints=0] [name=Dave Matthews, id=3, current=MARS, seat=BUSINESS, cost=$998.00, rewardsPoints=1345] [name=Peter Cetera, id=4, current=EARTH, seat=FIRST, cost=$1499.99, rewardsPoints=31235] [name=Roy Orbison, id=5, current=BELT, seat=FIRST, cost=$1499.99, rewardsPoints=14318] [name=Dirks Bentley, id=6, current=RING, seat=COACH, cost=$449.00, rewardsPoints=345] [name=Notorious B.I.G, id=7, current=BELT, position=Captain, flightHours=9724] [name=George Strait, id=8, current=RING, position=Co-pilot, flightHours=1398] [name=Ringo Starr, id=9, current=RING, position=Captain, flightHours=8348] [name=Eddie Vedder, id=10, current=MARS, position=Co-pilot, flightHours=1832] ------------------------------------------- 1) Add traveler to a Spaceship from the AvailableTravelers object 2) Search for traveler on a Spaceship 3) Move a spaceship to a new location 4) Displays a list of Available Travelers 5) Display of list of Travelers on a specific SpaceShip (Id by name) 6) Exit the program Choose an option from above (1-6): 1 Board a traveler on a ship Name of ship: USS Chicago Name of Traveler: Lupe Fiasco Boarded successfully: true ------------------------------------------- 1) Add traveler to a Spaceship from the AvailableTravelers object 2) Search for traveler on a Spaceship 3) Move a spaceship to a new location 4) Displays a list of Available Travelers 5) Display of list of Travelers on a specific SpaceShip (Id by name) 6) Exit the program Choose an option from above (1-6): 1 Board a traveler on a ship Name of ship: USS Chicago Name of Traveler: Peter Cetera Boarded successfully: false ------------------------------------------- 1) Add traveler to a Spaceship from the AvailableTravelers object 2) Search for traveler on a Spaceship 3) Move a spaceship to a new location 4) Displays a list of Available Travelers 5) Display of list of Travelers on a specific SpaceShip (Id by name) 6) Exit the program Choose an option from above (1-6): 1 Board a traveler on a ship Name of ship: USS Chicago Name of Traveler: Eddie Vedder Boarded successfully: true ------------------------------------------- 1) Add traveler to a Spaceship from the AvailableTravelers object 2) Search for traveler on a Spaceship 3) Move a spaceship to a new location 4) Displays a list of Available Travelers 5) Display of list of Travelers on a specific SpaceShip (Id by name) 6) Exit the program Choose an option from above (1-6): 2 Search for traveler on ship Name of ship: USS Chicago Enter passenger to search for: Peter Cetera Passenger on board: false ------------------------------------------- 1) Add traveler to a Spaceship from the AvailableTravelers object 2) Search for traveler on a Spaceship 3) Move a spaceship to a new location 4) Displays a list of Available Travelers 5) Display of list of Travelers on a specific SpaceShip (Id by name) 6) Exit the program Choose an option from above (1-6): 2 Search for traveler on ship Name of ship: USS Chicago Enter passenger to search for: Lupe Fiasco Passenger on board: true ------------------------------------------- 1) Add traveler to a Spaceship from the AvailableTravelers object 2) Search for traveler on a Spaceship 3) Move a spaceship to a new location 4) Displays a list of Available Travelers 5) Display of list of Travelers on a specific SpaceShip (Id by name) 6) Exit the program Choose an option from above (1-6): 5 List all pax on ship Name of ship: USS Chicago [name=USS Chicago, current=MARS, destination=EARTH, capacity=2, crew=2] [name=Lupe Fiasco, id=1, current=MARS, seat=FIRST, cost=$1199.99, rewardsPoints=2345] [name=Eddie Vedder, id=10, current=MARS, position=Co-pilot, flightHours=1832] ------------------------------------------- 1) Add traveler to a Spaceship from the AvailableTravelers object 2) Search for traveler on a Spaceship 3) Move a spaceship to a new location 4) Displays a list of Available Travelers 5) Display of list of Travelers on a specific SpaceShip (Id by name) 6) Exit the program Choose an option from above (1-6): 3 Move Name of ship: USS Chicago ------------------------------------------- 1) Add traveler to a Spaceship from the AvailableTravelers object 2) Search for traveler on a Spaceship 3) Move a spaceship to a new location 4) Displays a list of Available Travelers 5) Display of list of Travelers on a specific SpaceShip (Id by name) 6) Exit the program Choose an option from above (1-6): 5 List all pax on ship Name of ship: USS Chicago [name=USS Chicago, current=EARTH, destination=MARS, capacity=2, crew=0] ------------------------------------------- 1) Add traveler to a Spaceship from the AvailableTravelers object 2) Search for traveler on a Spaceship 3) Move a spaceship to a new location 4) Displays a list of Available Travelers 5) Display of list of Travelers on a specific SpaceShip (Id by name) 6) Exit the program Choose an option from above (1-6): 4 List all travelers [name=Lupe Fiasco, id=1, current=EARTH, seat=FIRST, cost=$1199.99, rewardsPoints=35145] [name=Drake, id=2, current=EARTH, seat=FIRST, cost=$1499.99, rewardsPoints=0] [name=Dave Matthews, id=3, current=MARS, seat=BUSINESS, cost=$998.00, rewardsPoints=1345] [name=Peter Cetera, id=4, current=EARTH, seat=FIRST, cost=$1499.99, rewardsPoints=31235] [name=Roy Orbison, id=5, current=BELT, seat=FIRST, cost=$1499.99, rewardsPoints=14318] [name=Dirks Bentley, id=6, current=RING, seat=COACH, cost=$449.00, rewardsPoints=345] [name=Notorious B.I.G, id=7, current=BELT, position=Captain, flightHours=9724] [name=George Strait, id=8, current=RING, position=Co-pilot, flightHours=1398] [name=Ringo Starr, id=9, current=RING, position=Captain, flightHours=8348] [name=Eddie Vedder, id=10, current=EARTH, position=Co-pilot, flightHours=2160] ------------------------------------------- 1) Add traveler to a Spaceship from the AvailableTravelers object 2) Search for traveler on a Spaceship 3) Move a spaceship to a new location 4) Displays a list of Available Travelers 5) Display of list of Travelers on a specific SpaceShip (Id by name) 6) Exit the program Choose an option from above (1-6): 6 Well, that was fun. Thanks for playing! The above was created by using this file as input (included in starter code as travelersAndFleet.txt) p Lupe Fiasco MARS FIRST 12345 p Drake EARTH COACH 10000 p Dave Matthews MARS BUSINESS 1345 p Peter Cetera EARTH COACH 41235 p Roy Orbison BELT BUSINESS 24318 p Dirks Bentley RING COACH 345 c Notorious B.I.G BELT Captain 9724 c George Strait RING Co-pilot 1398 c Ringo Starr RING Captain 8348 c Eddie Vedder MARS Co-pilot 1832 ----- USS Chicago MARS EARTH 2 328 USS Louisville BELT RING 3 127 ----- 4 1 USS Chicago Lupe Fiasco 1 USS Chicago Peter Cetera 1 USS Chicago Eddie Vedder 2 USS Chicago Peter Cetera 2 USS Chicago Lupe Fiasco 5 USS Chicago 3 USS Chicago 5 USS Chicago 4 6
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started