In: Computer Science
Project part 3:
Part Three: Complete the addShip() Method
1. In the Driver.java file, see below for where you will add code
to finish the addShip() method.
// Add a New Ship public static void addShip() {
// complete this method
}
2. Next, review the required functionality of the “Add Ship”
menu option below.
Menu Option Validation Check(s) Functionality
Add Ship - Ensures ship does not already exist in our system -
Ensures all class variables are populated
Adds ship to system
3. Add Ship Method: Write the code for the addShip() method. When
your code is complete, test the output. Be sure that the completed
method does the following:
a. Adds a new Ship object b. Includes all class variables c.
Updates appropriate ArrayList
TIP: You can refer to the Ship.java class constructor to make sure
you have included all variables.
Here is the given ship code:
public class Ship {
// Class 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;
}
}
And here is the driver code that needs to be completed:
import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.parseInt;
public class Driver {
// class variables (add more as needed)
private static ArrayList<Ship> shipList = new
ArrayList();
private static ArrayList<Cruise> cruiseList = new
ArrayList();
private static ArrayList<Passenger> passengerList = new
ArrayList();
public static void main(String[] args) {
initializeShipList(); // initial ships
initializeCruiseList(); // initial cruises
initializePassengerList(); // initial passengers
// 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("\nThere are no ships to print.");
return;
}
if (listType == "name") {
System.out.println("\n\nSHIP LIST - Name");
for (int i = 0; i < shipList.size(); i++) {
System.out.println(shipList.get(i));
}
} else if (listType == "active") {
System.out.println("\n\nSHIP LIST - Active");
System.out.println("-----------------------------------------------");
System.out.println(" Number of Rooms In");
System.out.print("SHIP NAME Bal OV Ste Int Service");
System.out.println("\n-----------------------------------------------");
for (Ship eachShip: shipList) {
if(eachShip.getType()==true)
eachShip.printShipData();
}
} else if (listType == "full") {
System.out.println("\n\nSHIP LIST - Full");
System.out.println("-----------------------------------------------");
System.out.println(" Number of Rooms In");
System.out.print("SHIP NAME Bal OV Ste Int Service");
System.out.println("\n-----------------------------------------------");
for (Ship eachShip: shipList)
eachShip.printShipData();
} else
System.out.println("\n\nError: List type not defined.");
}
public static void printCruiseList(String listType) {
if (cruiseList.size() < 1) {
System.out.println("\nThere are no cruises to print.");
return;
}
if (listType == "list") {
System.out.println("\n\nCRUISE LIST");
for (int i=0; i < cruiseList.size(); i++) {
System.out.println(cruiseList.get(i));
}
} else if (listType == "details") {
System.out.println("\n\nCRUISE LIST - Details");
System.out.println("------------------------------------------------------------------------------------------");
System.out.println("
|----------------------PORTS-----------------------|");
System.out.print("CRUISE NAME SHIP NAME DEPARTURE DESTINATION
RETURN");
System.out.println("\n-----------------------------------------------------------------------------------------");
for (Cruise eachCruise: cruiseList)
eachCruise.printCruiseDetails();
} else
System.out.println("\n\nError: List type not defined.");
}
public static void printPassengerList() {
if (passengerList.size() < 1) {
System.out.println("\nThere are no passengers to print.");
return;
}
System.out.println("\n\nPASSENGER LIST");
System.out.println("-----------------------------------------------------");
System.out.print("PASSENGER NAME CRUISE ROOM TYPE");
System.out.println("\n-----------------------------------------------------");
for (Passenger eachPassenger: passengerList)
eachPassenger.printPassenger();
}
// display text-based menu
public static void displayMenu() {
System.out.println("\n\n");
System.out.println("\t\t\tLuxury Ocean Cruise Outings");
System.out.println("\t\t\t\t\tSystem Menu\n");
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("\nEnter a menu selection: ");
}
// Add a New Ship
public static void addShip() {
// complete this method
}
// Edit an existing ship
public static void editShip() {
/******************************ShipDriver.java*******************/
import java.util.ArrayList;
import java.util.Scanner;
import Cruise.Cruise;
import static java.lang.Integer.parseInt;
public class ShipDriver {
// class variables (add more as needed)
private static ArrayList<Ship> shipList;
private static ArrayList<Cruise>
cruiseList;
private static ArrayList<Passenger>
passengerList;
public static void main(String[] args) {
shipList = new
ArrayList<>();
cruiseList = new
ArrayList<>();
passengerList = new
ArrayList<>();
initializeShipList(); // initial
ships
initializeCruiseList(); // initial
cruises
initializePassengerList(); //
initial passengers
// 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("\nThere are no ships to print.");
return;
}
if (listType == "name") {
System.out.println("\n\nSHIP LIST - Name");
for (int i = 0;
i < shipList.size(); i++) {
System.out.println(shipList.get(i));
}
} else if (listType == "active")
{
System.out.println("\n\nSHIP LIST - Active");
System.out.println("-----------------------------------------------");
System.out.println(" Number of Rooms In");
System.out.print("SHIP NAME Bal OV Ste Int Service");
System.out.println("\n-----------------------------------------------");
for (Ship
eachShip : shipList) {
if (eachShip.getType() == true)
eachShip.printShipData();
}
} else if (listType == "full")
{
System.out.println("\n\nSHIP LIST - Full");
System.out.println("-----------------------------------------------");
System.out.println(" Number of Rooms In");
System.out.print("SHIP NAME Bal OV Ste Int Service");
System.out.println("\n-----------------------------------------------");
for (Ship
eachShip : shipList)
eachShip.printShipData();
} else
System.out.println("\n\nError: List type not defined.");
}
public static void printCruiseList(String listType)
{
if (cruiseList.size() < 1)
{
System.out.println("\nThere are no cruises to print.");
return;
}
if (listType == "list") {
System.out.println("\n\nCRUISE LIST");
for (int i = 0;
i < cruiseList.size(); i++) {
System.out.println(cruiseList.get(i));
}
} else if (listType == "details")
{
System.out.println("\n\nCRUISE LIST - Details");
System.out.println(
"------------------------------------------------------------------------------------------");
System.out.println("
|----------------------PORTS-----------------------|");
System.out.print("CRUISE NAME SHIP NAME DEPARTURE DESTINATION
RETURN");
System.out.println(
"\n-----------------------------------------------------------------------------------------");
for (Cruise
eachCruise : cruiseList)
eachCruise.printCruiseDetails();
} else
System.out.println("\n\nError: List type not defined.");
}
public static void printPassengerList() {
if (passengerList.size() < 1)
{
System.out.println("\nThere are no passengers to print.");
return;
}
System.out.println("\n\nPASSENGER
LIST");
System.out.println("-----------------------------------------------------");
System.out.print("PASSENGER NAME
CRUISE ROOM TYPE");
System.out.println("\n-----------------------------------------------------");
for (Passenger eachPassenger :
passengerList)
eachPassenger.printPassenger();
}
// display text-based menu
public static void displayMenu() {
System.out.println("\n\n");
System.out.println("\t\t\tLuxury
Ocean Cruise Outings");
System.out.println("\t\t\t\t\tSystem Menu\n");
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("\nEnter 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() {
Scanner scan = new
Scanner(System.in);
System.out.print("Enter shipName:
");
String shipName =
scan.nextLine();
Ship ship1 = null;
for (Ship ship : shipList) {
if (ship.getShipName().equalsIgnoreCase(shipName)) {
ship1 = ship;
}
}
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();
ship1.setRoomBalcony(balcony);
ship1.setRoomOceanView(oceanView);
ship1.setRoomSuite(roomSuite);
ship1.setRoomInterior(roomInterior);
ship1.setInService(inService);
}
}
Please let me know if you have any doubt or modify the answer, Thanks:)