Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I have a Java project that I have been working on for a week. I am trying to get the program to: a. Includes

Hello, I have a Java project that I have been working on for a week. I am trying to get the program to:

a. Includes a loop that allows the user to interact with the menu until they signal that they want to exit the system

b. Maps user input to the specified functionality based on the menu options.

The Java program requires 4 class files as followed:

Cruise.java

Passenger.java

Ship.java

Driver.java

Driver.java

package luxury;

import java.util.ArrayList;

import java.util.Scanner;

import static java.lang.Integer.parseInt;

public class Driver {

// instance variables (add more as needed)

private static ArrayList shipList = new ArrayList();

private static ArrayList cruiseList = new ArrayList();

private static ArrayList passengerList = new ArrayList();

public static void main(String[] args) {

Scanner scnr = new Scanner (System.in);

initializeShipList();// initial ships

initializeCruiseList();// initial cruises

initializePassengerList();// initial passengers

displayMenu();

String selection = scnr.next().toUpperCase();

scnr.hasNextLine();

while (!selection.equalsIgnoreCase("X")) {

{

switch (selection) {

case "1":

addShip();

break;

case "2":

editShip();

break;

case "3":

if (addCruise()) {

addCruise();

}

else {

System.out.println("There are no available ships to add a cruise to at this time.");

}

break;

case "4":

editCruise();

break;

case "5":

addPassenger();

break;

case "6":

editPassenger();

break;

case "A":

printShipList("name");

break;

case "B":

printShipList("active");

break;

case "C":

printShipList("full");

break;

case "D":

printCruiseList("list");

break;

case "E":

printCruiseList("details");

break;

case "F":

printPassengerList();

break;

case "X":

break;

default:

throw new Exception("Invalid Entry. Please try again");

}

}

}

displayMenu();

selection = scnr.next().toUpperCase();

scnr.nextLine();

}

System.out.println("Goodbye.Thanks for choosing Luxury Ocean Cruise Outings!");

System.out.println("We hope to see you again soon!");

scnr.close();// close the Scanner object to prevent memory leak

return;// return even when return type is void to clear the stack frame

}

// add loop and code here that accepts and validates user input

// and takes the appropriate action. include appropriate

// user feedback and redisplay the menu as needed

// hardcoded ship data for testing

// Initialize ship list

public static void initializeShipList() {

add("Candy Cane", 20, 40, 10, 60, true);

add("Peppermint Stick", 10, 20, 5, 40, true);

add("Bon Bon", 12, 18, 2, 24, false);

add("Candy Corn", 12, 18, 2, 24, false);

}

// hardcoded cruise data for testing

// Initialize cruise list

public static void initializeCruiseList() {

Cruise newCruise = new Cruise("Southern Swirl", "Candy Cane", "Miami", "Cuba", "Miami");

cruiseList.add(newCruise);

}

// hardcoded cruise data for testing

// Initialize passenger list

public static void initializePassengerList() {

Passenger newPassenger1 = new Passenger("Neo Anderson", "Southern Swirl", "STE");

passengerList.add(newPassenger1);

Passenger newPassenger2 = new Passenger("Trinity", "Southern Swirl", "STE");

passengerList.add(newPassenger2);

Passenger newPassenger3 = new Passenger("Morpheus", "Southern Swirl", "BAL");

passengerList.add(newPassenger3);

}

// custom method to add ships to the shipList ArrayList

public static void add(String tName, int tBalcony, int tOceanView,

int tSuite, int tInterior, boolean tInService) {

Ship newShip = new Ship(tName, tBalcony, tOceanView, tSuite, tInterior, tInService);

shipList.add(newShip);

}

public static void printShipList(String listType) {

//printShipList() //method prints list of ships from the

// shipList ArrayList. There are three different outputs

// based on the listType String parameter:

// name - prints a list of ship names only

// active - prints a list of ship names that are "in service"

// full - prints tabbed data on all ships

if (shipList.size() < 1) {

System.out.println(" There are no ships to print.");

return;

}

if (listType == "name") {

System.out.println(" SHIP LIST - Name");

for (int i = 0; i < shipList.size(); i++) {

System.out.println(shipList.get(i));

}

} else if (listType == "active") {

int activeShipCount = 0;

System.out.println(" SHIP LIST - Active");

} else if (listType == "full") {

System.out.println(" SHIP LIST - Full");

System.out.println("-----------------------------------------------");

System.out.println("Number of RoomsIn");

System.out.print("SHIP NAMEBal OVSte IntService");

System.out.println(" -----------------------------------------------");

for (Ship eachShip: shipList)

eachShip.printShipData();

} else

System.out.println(" Error: List type not defined.");

}

public static void printCruiseList(String listType) {

if (cruiseList.size() < 1) {

System.out.println(" There are no cruises to print.");

return;

}

if (listType == "list") {

System.out.println(" CRUISE LIST");

for (int i=0; i < cruiseList.size(); i++) {

System.out.println(cruiseList.get(i));

}

} else if (listType == "details") {

System.out.println(" CRUISE LIST - Details");

System.out.println("------------------------------------------------------------------------------------------");

System.out.println("|----------------------PORTS-----------------------|");

System.out.print("CRUISE NAMESHIP NAMEDEPARTUREDESTINATIONRETURN");

System.out.println(" -----------------------------------------------------------------------------------------");

for (Cruise eachCruise: cruiseList)

eachCruise.printCruiseDetails();

} else

System.out.println(" Error: List type not defined.");

}

public static void printPassengerList() {

if (passengerList.size() < 1) {

System.out.println(" There are no passengers to print.");

return;

}

System.out.println(" PASSENGER LIST");

System.out.println("-----------------------------------------------------");

System.out.print("PASSENGER NAMECRUISEROOM TYPE");

System.out.println(" -----------------------------------------------------");

for (Passenger eachPassenger: passengerList)

eachPassenger.printPassenger();

}

// display text-based menu

public static void displayMenu() {

System.out.println(" ");

System.out.println("\t\t\tLuxury Ocean Cruise Outings");

System.out.println("\t\t\t\t\tSystem Menu ");

System.out.println("[1] Add Ship[A] Print Ship Names");

System.out.println("[2] Edit Ship[B] Print Ship In Service List");

System.out.println("[3] Add Cruise[C] Print Ship Full List");

System.out.println("[4] Edit Cruise[D] Print Cruise List");

System.out.println("[5] Add Passenger[E] Print Cruise Details");

System.out.println("[6] Edit Passenger[F] Print Passenger List");

System.out.println("[x] Exit System");

System.out.println(" Enter a menu selection: ");

}

// Add a New Ship

public static void addShip() {

Scanner scan = new Scanner(System.in);

System.out.print("Enter Ship Name: ");

String shipName = scan.nextLine();

System.out.print("Balcony: ");

int balcony = scan.nextInt();

System.out.print("Ocean View:");

int oceanView = scan.nextInt();

System.out.print("Room Suite: ");

int roomSuite = scan.nextInt();

System.out.print("Room Interior: ");

int roomInterior = scan.nextInt();

System.out.print("In Service: ");

boolean inService = scan.nextBoolean();

scan.nextLine();

shipList.add(new Ship(shipName, balcony, oceanView, roomSuite, roomInterior, inService));

}

// Edit an existing ship

public static void editShip() {

// This method does not need to be completed

System.out.println("The \"Edit Ship\" feature is not yet implemented.");

}

// Add a New Cruise

public static boolean addCruise() {

Scanner scan = new Scanner(System.in);

System.out.print("Enter Cruise Name: ");

String cruiseName = scan.nextLine();

int flag = 0;

for (Cruise cruise : cruiseList) {

if (cruise.getCruiseName().equalsIgnoreCase(cruiseName)) {

flag = 1;

}

}

if (flag == 1) {

System.out.println("That Cruise is already in the system. Exiting to menu...");

return;

}

System.out.print("Enter Cruise Ship Name: ");

String cruiseShip = scan.nextLine();

System.out.print("Departure Port: ");

String departurePort = scan.nextLine();

System.out.print("Destination: ");

String destination = scan.nextLine();

System.out.print("Return Port: ");

String returnPort = scan.nextLine();

cruiseList.add(new Cruise(cruiseName, cruiseShip, departurePort, destination, returnPort));

}

// Edit an existing cruise

public static void editCruise() {

// This method does not need to be completed

System.out.println("The \"Edit Cruise\" feature is not yet implemented.");

}

// Add a New Passenger

public static void addPassenger() {

Scanner newPassengerInput = new Scanner(System.in);

System.out.println("Enter the new passenger's name: ");

String newPassengerName = newPassengerInput.nextLine();

// ensure new passenger name does not already exist

for (Passenger eachPassenger: passengerList) {

if (eachPassenger.getPassengerName().equalsIgnoreCase(newPassengerName)) {

System.out.println("That passenger is already in the system. Exiting to menu...");

return; // quits addPassenger() method processing

}

}

// get cruise name for passenger

System.out.println("Enter cruise name: ");

String newCruiseName = newPassengerInput.nextLine();

// ensure cruise exists

for (Cruise eachCruise: cruiseList) {

if (eachCruise.getCruiseName().equalsIgnoreCase(newCruiseName)) {

// cruise does exist

} else {

System.out.println("That cruise does not exist in the system. Exiting to menu...");

return; // quits addPassenger() method processing

}

}

// get room type

System.out.println("Enter Room Type (BAL, OV, STE, or INT: ");

String room = newPassengerInput.nextLine();

// validate room type

if ((room.equalsIgnoreCase("BAL")) || (room.equalsIgnoreCase("OV")) ||

(room.equalsIgnoreCase("STE")) || (room.equalsIgnoreCase("INT"))) {

// validation passed - add passenger

Passenger newPassenger = new Passenger(newPassengerName, newCruiseName, room.toUpperCase());

passengerList.add(newPassenger);

} else {

System.out.println("Invalid input. Exiting to menu...");

return; // quits addPassenger() method processing

}

}

// Edit an existing passenger

public static void editPassenger() {

// This method does not need to be completed

System.out.println("The \"Edit Passenger\" feature is not yet implemented.");

}

// Method to check if input is a number

public static boolean isANumber(String str) {

for (int i = 0; i < str.length(); i++) {

if (Character.isDigit(str.charAt(i)) == false)

return false;

}

return true;

}

}

Cruise.java

package luxury;

public class Cruise {

// Instance Variables

private String cruiseName;

private String cruiseShipName;

private String departurePort;

private String destination;

private String returnPort;

// Constructor - default

Cruise() {

}

// Constructor - full

Cruise(String tCruiseName, String tShipName, String tDeparture, String tDestination, String tReturn) {

cruiseName = tCruiseName;

cruiseShipName = tShipName;

departurePort = tDeparture;

destination = tDestination;

returnPort = tReturn;

}

// Accessors

public String getCruiseName() {

return cruiseName;

}

public String getCruiseShipName() {

return cruiseShipName;

}

public String getDeparturePort() {

return departurePort;

}

public String getDestination() {

return destination;

}

public String getReturnPort() {

return returnPort;

}

// Mutators

public void setCruiseName(String tVar) {

cruiseName = tVar;

}

public void setCruiseShipName(String tVar) {

cruiseShipName = tVar;

}

public void setDeparturePort(String tVar) {

departurePort = tVar;

}

public void setDestination(String tVar) {

destination = tVar;

}

public void setReturnPort(String tVar) {

returnPort = tVar;

}

// print cruise details

public void printCruiseDetails() {

}

// method added to print ship's name vice memory address

@Override

public String toString() {

return cruiseName;

}

}

Passenger.java

package luxury;

public class Passenger {

// Instance variables

private String passengerName;

private String passengerCruise;

private String passengerRoomType;

// Constructor - default

Passenger() {

}

// Constructor - full

Passenger(String pName, String pCruise, String pRoomType) {

passengerName = pName;

passengerCruise = pCruise;

passengerRoomType = pRoomType; // should be BAL, OV, STE, or INT

}

// Accessors

public String getPassengerName() {

return passengerName;

}

public String getPassengerCruise() {

return passengerCruise;

}

public String getPassengerRoomType() {

return passengerRoomType;

}

// Mutators

public void setPassengerName(String tVar) {

passengerName = tVar;

}

public void setPassengerCruise(String tVar) {

passengerCruise = tVar;

}

public void setPassengerRoomType(String tVar) {

passengerRoomType = tVar;

}

// print method

public void printPassenger() {

int spaceCount;

String spaces1 = "";

String spaces2 = "";

spaceCount = 20 - passengerName.length();

for (int i = 1; i <= spaceCount; i++) {

spaces1 = spaces1 + " ";

}

spaceCount = 20 - passengerCruise.length();

for (int i = 1; i <= spaceCount; i++) {

spaces2 = spaces2 + " ";

}

System.out.println(passengerName + spaces1 + passengerCruise + spaces2 +

passengerRoomType);

}

// method added to print passenger's name vice memory address

@Override

public String toString() {

return passengerName;

}

}

Ship.java

package luxury;

public class Ship {

public static final boolean getType = false;

// Instance Variables

private String shipName;

private int roomBalcony;

private int roomOceanView;

private int roomSuite;

private int roomInterior;

private boolean inService;

// Constructor - default

Ship() {

}

// Constructor - full

Ship(String tName, int tBalcony, int tOceanView,

int tSuite, int tInterior, boolean tInService) {

shipName = tName;

roomBalcony = tBalcony;

roomOceanView = tOceanView;

roomSuite = tSuite;

roomInterior = tInterior;

inService = tInService;

}

// Accessors

public String getShipName() {

return shipName;

}

public int getRoomBalcony() {

return roomBalcony;

}

public int getRoomOceanView() {

return roomOceanView;

}

public int getRoomSuite() {

return roomSuite;

}

public int getRoomInterior() {

return roomInterior;

}

public boolean getInService() {

return inService;

}

// Mutators

public void setShipName(String tVar) {

shipName = tVar;

}

public void setRoomBalcony(int tVar) {

roomBalcony = tVar;

}

public void setRoomOceanView(int tVar) {

roomOceanView = tVar;

}

public void setRoomSuite(int tVar) {

roomSuite = tVar;

}

public void setRoomInterior(int tVar) {

roomInterior = tVar;

}

public void setInService(boolean tVar) {

inService = tVar;

}

// print method

public void printShipData() {

int spaceCount;

String spaces = "";

spaceCount = 20 - shipName.length();

for (int i = 1; i <= spaceCount; i++) {

spaces = spaces + " ";

}

System.out.println(shipName + spaces + roomBalcony + "\t" +

roomOceanView + "\t" + roomSuite + "\t" +

roomInterior + "\t\t" + inService);

}

// method added to print ship's name vice memory address

@Override

public String toString() {

return shipName;

}

public boolean getType() {

return false;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions