Java In this assignment, you will write a train car manager for a commerical tra
ID: 3753871 • Letter: J
Question
Java
In this assignment, you will write a train car manager for a commerical train. The train, modelled using a Double-Linked List data structure, consists of a chain of train cars, each of which contains a product load. A product load has a weight, a value, and can be dangerous or safe. The train car manager will be able to add and remove cars from the train, set the product load for each car, and determine useful properties of the train, such as it's length, weight, total value, and whether it contains any dangerous product loads. 1. Write a fully-documented class named ProductLoad which contains the product name(String), it's weight in tons (double), it's value in dollars (double), and whether the product is dangerous or not (boolean). You should provide accessor and mutator methods for each variable. The mutator methods for weight and value should throw exceptions for illegal arguments (ie. negative values). The class should include a constructor. The full list of required methods is: public ProductLoad-constructor (you may include a constructor with parameters) .getter and setter methods for each variable 2. Write a fully-documented class named TrainCar which contains a length in meters (double), a weight in tons (double), and a load reference (ProductLoad). The load variable of the train may be null, which indicates that the train car is empty and contains no product. The train car should have accessor methods for the length, weight, and load variables; however, you should only provide a mutator method for the load variable (the car weight and length should be constant once set). In addition, the class should specify a constructor method (with whatever parameters are necessary), and a method determining whether the car is empty or not. The ful list of required methods is: public TrainCar-constructor (you may include a constructor with parameters) getter methods for each variable . . setter method only for the load variable. isEmpty0 method 3. Write a fully-documented class named TrainCarNode which acts as a node wrapper around a TrainCar object. The class should contain two TrainCarNode references (one for the next node in the chain, one for the previous node in the chain), and one TrainCar reference variable containing the data. Iaclude mutator/accessor methods for each member variable, and a constructor method. The full list of required methods is: public TrainCarNode - constructor (you may include a constructor with parameters) . getter and setter methods for each variable TrainCarNode TrainCar ProductLoad prev TrainCarNode next: TrainCarNode car TrainCar carLength : double carWeight: double load: ProductLoad name: String weight: double value :ubio isDangerous: boolearn constructors constructors: TrainCan carWeight: double, TrainCarNode) TrainCarNode(car: TrainCar) carLength: double) Productl.oadname: String weight: double, value: double, getters /setters getters/ setters: All member vaniables getCarWeight0 : double isDangerous:boolean) getcarLength) : dobule setProductLoad(load: ProductLoad) : void methods methods: toString): String getterssetters W member variables. isEmptyo: boolean ProductLoad, TrainCar, and TrainCarNode UML. specification.Explanation / Answer
Below is your code: -
ProductLoad.java
public class ProductLoad {
private String name;
private double weight;
private double value;
private boolean dangerous;
/**
* Creates a new ProductLoad object with uninstantiated parameters
*/
public ProductLoad() {
}
/**
* Creates a new ProductLoad object using given values. Overloaded
* constructor
*
* @param name
* full name of the ProductLoad
* @param weight
* weight value of the ProductLoad in decimal
* @param value
* value of the ProductLoad in decimal
* @param dangerous
* indicates whether or not the ProductLoad is dangerous
*/
public ProductLoad(String name, double weight, double value, boolean dangerous) throws IllegalArgumentException {
if (weight < 0)
throw new IllegalArgumentException("Invalid Entry for weight (negative).");
if (value < 0)
throw new IllegalArgumentException("Invalid Entry for value (negative).");
this.name = name;
this.weight = weight;
this.value = value;
this.dangerous = dangerous;
}
/**
* Changes a particular ProductLoad's name to a new specified name
*
* @param name
* new name of the ProductLoad
*/
public void setName(String name) {
this.name = name;
}
/**
* Receives the name of a particular ProductLoad
*
* @return the name of a ProductLoad as a String
*/
public String getName() {
return name;
}
/**
* Changes a particular ProductLoad's weight value to a new specified weight
* value
*
* @param weight
* new weight value of the ProductLoad
* @throws IllegalArgumentException
* indicates that the weight is an invalid entry (negative
* value).
*/
public void setWeight(double weight) throws IllegalArgumentException {
if (weight < 0)
throw new IllegalArgumentException("Invalid entry for weight (negative).");
this.weight = weight;
}
/**
* Receives the weight value of a particular ProductLoad
*
* @return the weight value of a ProductLoad as a double
*/
public double getWeight() {
return weight;
}
/**
* Changes a particular ProductLoad's value to a new specified value
*
* @param value
* new value of the ProductLoad
* @throws IllegalArgumentException
* indicates that value is an invalid entry (negative value)
*/
public void setValue(double value) throws IllegalArgumentException {
if (value < 0)
throw new IllegalArgumentException("Invalid entry for value (negative).");
this.value = value;
}
/**
* Receives the value of a particular ProductLoad
*
* @return the value of a ProductLoad as a double
*/
public double getValue() {
return value;
}
/**
* Changes a particular ProductLoad's indication of danger
*
* @param dangerous
* new indication of whether or not the product is dangerous
*/
public void setDangerous(boolean dangerous) {
this.dangerous = dangerous;
}
/**
* Receives the danger status of a particular ProductLoad
*
* @return the indication of a dangerous ProductLoad as a boolean
*/
public boolean getDangerous() {
return dangerous;
}
}
TrainCarNode.java
public class TrainCarNode {
private TrainCarNode prev;
private TrainCarNode next;
private TrainCar car;
/**
* Creates a new TrainCarNode object with uninstantiated parameters
*/
public TrainCarNode() {
}
/**
* Creates a new TrainCarNode using given values.
*
* @param car
* new TrainCar of the TrainCarNode
*/
public TrainCarNode(TrainCar car) {
this.prev = null;
this.next = null;
this.car = car;
}
/**
* Changes a particular TrainCarNode's previous TrainCarNode to a new
* specified TrainCarNode
*
* @param prev
* new previous TrainCarNode of the current TrainCarNode
*/
public void setPrev(TrainCarNode prev) {
this.prev = prev;
}
/**
* Receives the previous TrainCarNode of a particular TrainCarNode
*
* @return the previous TrainCarNode of a particular TrainCarNode
*/
public TrainCarNode getPrev() {
return prev;
}
/**
* Changes a particular TrainCarNode's next TrainCarNode to a new specified
* TrainCarNode
*
* @param next
* new next TrainCarNode of the current TrainCarNode
*/
public void setNext(TrainCarNode next) {
this.next = next;
}
/**
* Receives the next TrainCarNode of a particular TrainCarNode
*
* @return the next TrainCarNode of a particular TrainCarNode
*/
public TrainCarNode getNext() {
return next;
}
/**
* Changes a particular TrainCarNode's TrainCar
*
* @param car
* new TrainCar of the TrainCarNode
*/
public void setCar(TrainCar car) {
this.car = car;
}
/**
* Receives the TrainCar of a particular TrainCarNode
*
* @return the car of a TrainCarNode as a TrainCar object
*/
public TrainCar getCar() {
return car;
}
/**
* Obtains the String representation of this TrainCarNode object, which is a
* neatly formatted output of the content of a particular TrainCarNode
*
* @return the representation of this TrainCarNode object as a String
*/
public String toString() {
// String a = "Previous TrainCarNode: ";
// String b = "Next TrainCarNode: ";
String c = "TrainCar: ";
String d = " Length(m): ";
String e = " Weight (t): ";
String f = " Load: ";
String g = " Name: ";
String h = " Weight: ";
String i = " Value: ";
String j = " Dangerous: ";
/*
* a+getPrev()+" "+ b+getNext()+
*/
return (" " + c + " " + d + car.getCarLength() + " " + e + car.getCarWeight() + " " + f + " " + g
+ car.getProductLoad().getName() + " " + h + car.getProductLoad().getWeight() + " " + i
+ car.getProductLoad().getValue() + " " + j + car.getProductLoad().getDangerous() + " ");
}
}
TrainCar.java
public class TrainCar {
private double carLength;
private double carWeight;
private ProductLoad load;
/**
* Creates a new TrainCar object with uninstantiated parameters
*/
public TrainCar() {
}
/**
* Creates a new TrainCar object using given values.
*
* @param carLength
* length value of the car
* @param carWeight
* weight value of the car
*/
public TrainCar(double carLength, double carWeight) throws IllegalArgumentException {
if (carLength < 0)
throw new IllegalArgumentException("Invalid entry for carLength; negative value.");
if (carWeight < 0)
throw new IllegalArgumentException("Invalid entry for carWeight; negative value.");
this.carLength = carLength;
this.carWeight = carWeight;
}
/**
* Creates a new TrainCar object using given values. Overload Constructor
*
* @param carLength
* length value of the car
* @param carWeight
* weight value of the car
* @param load
* the ProductLoad object being carried by the car
*/
public TrainCar(double carLength, double carWeight, ProductLoad load) {
this.carLength = carLength;
this.carWeight = carWeight;
this.load = load;
}
/**
* Receives the car length of a particular TrainCar
*
* @return the car length value of a TrainCar as a double
*/
public double getCarLength() {
return carLength;
}
/**
* Receives the car weight value of a particular TrainCar
*
* @return the car weight of a TrainCar as a double
*/
public double getCarWeight() {
return carWeight;
}
/**
* Receives the ProductLoad object of a particular TrainCar
*
* @return the ProductLoad object of a TrainCar
*/
public ProductLoad getProductLoad() {
return load;
}
/**
* Changes a particular TrainCar's ProductLoad to a new specified
* ProductLoad
*
* @param load
* new load of the TrainCar
*/
public void setProductLoad(ProductLoad load) {
this.load = load;
}
/**
* Determines whether the car is empty or not
*
* @return the indication of an empty TrainCar as a boolean
*/
public boolean isEmpty() {
if (load == null)
return true;
else
return false;
}
}
TrainLinkedList.java
public class TrainLinkedList {
private TrainCarNode head;
private TrainCarNode tail;
private TrainCarNode cursor;
private int size;
private double totalLength;
private double totalWeight;
private double totalValue;
private int isDangerous;
private String search;
/**
* Constructs an instance of the TrainLinkedList with no TrainCar objects in
* it
*/
public TrainLinkedList() {
size = 0;
totalLength = 0;
totalWeight = 0;
isDangerous = 0;
search = "";
head = null;
tail = null;
cursor = null;
}
/**
* Returns a reference to the TrainCar at the node currently referenced by
* the cursor
*/
public TrainCar getCursorData() throws Exception {
if (cursor == null)
throw new Exception("Cursor is null.");
return cursor.getCar();
}
/**
* Places car in the node currently referenced by the cursor
*/
public void setCursorData(TrainCar car) throws Exception {
if (cursor != null)
cursor.setCar(car);
else
throw new Exception("The list is empty; cursor is null.");
}
/**
* Moves the cursor to point at the previous TrainCarNode
*/
public void cursorBackward() throws Exception {
if (cursor != null && cursor != head) {
cursor = cursor.getPrev();
}
else
throw new Exception(" No previous car, cannot move cursor backward.");
}
/**
* Moves the cursor to point at the next TrainCarNode
*/
public void cursorForward() throws Exception {
if (cursor != null && cursor != tail) {
cursor = cursor.getNext();
}
else
throw new Exception(" No next car, cannot move cursor forward.");
}
/**
* Inserts a car into the train after the cursor position
*
*/
public void insertAfterCursor(TrainCar newCar) throws IllegalArgumentException {
if (newCar != null) {
TrainCarNode newNode = new TrainCarNode(newCar);
if (cursor == null) {
size++;
totalLength += newCar.getCarLength();
totalWeight += newCar.getCarWeight();
if (newCar.getProductLoad() != null) {
totalWeight += newCar.getProductLoad().getWeight();
totalValue += newCar.getProductLoad().getValue();
if (newCar.getProductLoad().getDangerous())
isDangerous++;
}
newNode.setNext(null);
newNode.setPrev(null);
cursor = newNode;
head = newNode;
tail = newNode;
}
else if (cursor == tail) {
size++;
totalLength += newCar.getCarLength();
totalWeight += newCar.getCarWeight();
if (newCar.getProductLoad() != null) {
totalWeight += newCar.getProductLoad().getWeight();
totalValue += newCar.getProductLoad().getValue();
if (newCar.getProductLoad().getDangerous())
isDangerous++;
}
newNode.setNext(null);
newNode.setPrev(cursor);
cursor.setNext(newNode);
tail = newNode;
cursor = tail;
}
else {
size++;
totalLength += newCar.getCarLength();
totalWeight += newCar.getCarWeight();
if (newCar.getProductLoad() != null) {
totalWeight += newCar.getProductLoad().getWeight();
totalValue += newCar.getProductLoad().getValue();
if (newCar.getProductLoad().getDangerous())
isDangerous++;
}
newNode.setNext(cursor.getNext());
newNode.setPrev(cursor);
cursor.setNext(newNode);
newNode.getNext().setPrev(newNode);
cursor = newNode;
}
}
else
throw new IllegalArgumentException("newCar is null.");
}
/**
* Removes the TrainCarNode referenced by the cursor and returns the
* TrainCar contained within the node
*/
public TrainCar removeCursor() throws Exception {
TrainCar removedNode = null;
if (cursor != null) {
removedNode = cursor.getCar();
if (cursor == head) {
if (cursor == tail) {
cursor = null;
tail = null;
head = null;
size = 0;
totalLength = 0;
totalWeight = 0;
totalWeight = 0;
totalValue = 0;
isDangerous = 0;
}
else {
size--;
totalLength -= cursor.getCar().getCarLength();
totalWeight -= cursor.getCar().getCarWeight();
if (cursor.getCar().getProductLoad() != null) {
totalWeight -= cursor.getCar().getProductLoad().getWeight();
totalValue -= cursor.getCar().getProductLoad().getValue();
if (cursor.getCar().getProductLoad().getDangerous())
isDangerous--;
}
cursorForward();
cursor.setPrev(null);
head = cursor;
}
}
else if (cursor == tail) {
size--;
totalLength -= cursor.getCar().getCarLength();
totalWeight -= cursor.getCar().getCarWeight();
if (cursor.getCar().getProductLoad() != null) {
totalWeight -= cursor.getCar().getProductLoad().getWeight();
totalValue -= cursor.getCar().getProductLoad().getValue();
if (cursor.getCar().getProductLoad().getDangerous())
isDangerous--;
}
cursorBackward();
// cursor.getNext().setPrev(null);
cursor.setNext(null);
tail = cursor;
}
else {
size--;
totalLength -= cursor.getCar().getCarLength();
totalWeight -= cursor.getCar().getCarWeight();
if (cursor.getCar().getProductLoad() != null) {
totalWeight -= cursor.getCar().getProductLoad().getWeight();
totalValue -= cursor.getCar().getProductLoad().getValue();
if (cursor.getCar().getProductLoad().getDangerous())
isDangerous--;
}
cursorForward();
cursor.getPrev().getPrev().setNext(cursor);
cursor.setPrev(cursor.getPrev().getPrev());
}
}
else
throw new Exception("Empty List.");
return removedNode;
}
/**
* Determines the number of TrainCar objects currently on the train
* <p>
* Notes: This function should complete in O(1) time
* </p>
*
* @return the number of TrainCar objects on this train
*/
public int size() {
return size;
}
/**
* Returns the total length of the train in meters
* <p>
* Notes: This function should complete in O(1) time
* </p>
*
* @return the sum of the lengths of each TrainCar in the train
*/
public double getLength() {
return totalLength;
}
/**
* Returns the total value of product carried by the train
* <p>
* Notes: This function should complete in O(1) time
* </p>
*
* @return the sum of the values of each TrainCar in the train
*/
public double getValue() {
return totalValue;
}
/**
* Returns the total weight in tons of the train
* <p>
* Notes: This function should complete in O(1) time
* </p>
*
* @return the sum of the weight of each TrainCar plus the sum of the
* ProductLoad carried by that car
*/
public double getWeight() {
return totalWeight;
}
/**
* Whether or not there is a dangerous product on one of the TrainCar
* objects on the train
* <p>
* Notes: This function should complete in O(1) time
* </p>
*
* @return returns true if the train contains at least one TrainCar carrying
* a dangerous ProductLoad, false otherwise
*/
public boolean isDangerous() {
if (isDangerous > 0)
return true;
return false;
}
/**
* Searches the list for all ProductLoad objects with the indicated name,
* sums together their weight and value, obtains its danger status, and
* prints a single ProductLoad record to the console
*
* @param name
* the name of the ProductLoad to find on the train
*/
public void findProduct(String name) {
double cursorWeight = 0;
double cursorValue = 0;
boolean cursorDanger = false;
boolean firstMatch = false;
TrainCarNode tempCursor = new TrainCarNode();
tempCursor = head;
int count = 0;
search = "";
while (tempCursor != null) {
if (tempCursor.getCar().getProductLoad() == null)
break;
if ((tempCursor.getCar().getProductLoad().getName()).equals(name)) {
if (firstMatch == false) {
cursorDanger = tempCursor.getCar().getProductLoad().getDangerous();
firstMatch = true;
}
cursorWeight += tempCursor.getCar().getProductLoad().getWeight();
cursorValue += tempCursor.getCar().getProductLoad().getValue();
count++;
}
tempCursor = tempCursor.getNext();
}
if (firstMatch == false) {
System.out.println(" There are no ProductLoad objects of the indicated name on board the train.");
System.out.println("No record of " + name + " on board train.");
search = (" There are no ProductLoad objects of the indicated name on board the train. ")
+ ("No record of " + name + " on board train. ");
}
else {
System.out.println(" The following products were found on " + count + " cars: ");
search = (" The following products were found on " + count + " cars: ");
System.out.println();
System.out.println(" Name Weight (t) Value ($) Dangerous");
search += (" Name Weight (t) Value ($) Dangerous ");
System.out.println("=============================================================");
search += ("============================================================= ");
String danger = (cursorDanger) ? "YES" : "NO";
System.out.printf(" %-15s%-16.1f%,-16.2f%-7s ", name, cursorWeight, cursorValue, danger);
search += String.format(" %-15s%-16.1f%,-16.2f%-7s ", name, cursorWeight, cursorValue, danger);
}
}
/**
* Prints a neatly formatted table of the car number, car length, car
* weight, load name, load weight, load value, and load dangerousness for
* all of the car on the train.
*/
public void printManifest() {
TrainCarNode temp = head;
int num = 1;
System.out.println();
System.out.println(" CAR: LOAD:");
System.out.println(" Num Length (m) Weight (t) | Name Weight (t) Value ($) Dangerous");
System.out.println(
"====================================+===============================================================");
while (temp != null) {
if (temp == cursor)
System.out.printf(" -> %-7d%-13.1f%-11.1f| %-18s%-16.1f%,-16.2f%s ", num,
temp.getCar().getCarLength(), temp.getCar().getCarWeight(),
(temp.getCar().getProductLoad() != null) ? temp.getCar().getProductLoad().getName() : "Empty",
(temp.getCar().getProductLoad() != null) ? temp.getCar().getProductLoad().getWeight() : 0,
(temp.getCar().getProductLoad() != null) ? temp.getCar().getProductLoad().getValue() : 0,
(temp.getCar().getProductLoad() != null && temp.getCar().getProductLoad().getDangerous())
? "YES" : "NO");
else
System.out.printf(" %-7d%-13.1f%-11.1f| %-18s%-16.1f%,-16.2f%s ", num,
temp.getCar().getCarLength(), temp.getCar().getCarWeight(),
(temp.getCar().getProductLoad() != null) ? temp.getCar().getProductLoad().getName() : "Empty",
(temp.getCar().getProductLoad() != null) ? temp.getCar().getProductLoad().getWeight() : 0,
(temp.getCar().getProductLoad() != null) ? temp.getCar().getProductLoad().getValue() : 0,
(temp.getCar().getProductLoad() != null && temp.getCar().getProductLoad().getDangerous())
? "YES" : "NO");
temp = temp.getNext();
num++;
}
}
/**
* Removes all dangerous cars from the train, maintaining the order of the
* cars in the train
* <dt><b>Postconditions:</b>
* <dd>all dangerous cars have been removed from this train; the order of
* all non-dangerous cars must be maintained upon the completion of this
* method
*
* @throws Exception
* indicates that cursor removal failed
*/
public void removeDangerousCars() throws Exception {
cursor = head;
TrainCar removed = null;
while (cursor != null) {
if (cursor.getCar().getProductLoad() != null) {
if (cursor.getCar().getProductLoad().getDangerous())
removed = removeCursor();
}
if (cursor == tail)
break;
cursorForward();
}
if (removed == null)
System.out.println(" The train contains no dangerous cars.");
else
System.out.println(" Dangerous cars successfully removed from the train.");
}
/**
* Returns a neatly formatted String representation of the train
*
* @return a neatly formatted string containing information about the train,
* including it's size (number of cars), length in meters, weight in
* tons, value in dollars, and whether it is dangerous or not
*/
public String toString() {
return (String.format(" Train: %d cars, %.1f meters, %.1f tons, $%,.2f value, %s.", size, totalLength,
totalWeight, totalValue, (isDangerous > 0) ? "DANGEROUS" : "not dangerous"));
}
/**
* Increments totalWeight variable from added ProductLoad weight
*
* @param prodWeight
* added ProductLoad weight
*/
public void addProdWeight(double prodWeight) {
totalWeight += prodWeight;
}
/**
* Decrements totalWeight variable from replaced ProductLoad weight
*
* @param prodWeight
* subtracted ProductLoad weight
*/
public void decProdWeight(double prodWeight) {
totalWeight -= prodWeight;
}
/**
* Increments totalValue variable from added ProductLoad value
*
* @param prodValue
* added ProductLoad value
*/
public void addProdValue(double prodValue) {
totalValue += prodValue;
}
/**
* Decrements totalValue variable from replaced/removed ProductLoad value
*
* @param prodValue
* subtracted ProductLoad value
*/
public void decProdValue(double prodValue) {
totalValue -= prodValue;
}
/**
* Increments private variable isDangerous to indicate product danger
*/
public void addDanger() {
isDangerous++;
}
/**
* Decrements private variable isDangerous to indicate that a product which
* was dangerous was removed.
*/
public void decDanger() {
isDangerous--;
}
/**
* Returns the the Manifest Display from the printManifest() method
*
* @return a neatly formatted table displayed in printManifest() as a String
*/
public String manifestString() {
TrainCarNode temp = head;
int num = 1;
String a = " ";
String b = (" CAR: LOAD: ");
String c = (" Num Length (m) Weight (t) | Name Weight (t) Value ($) Dangerous ");
String d = ("====================================+=============================================================== ");
String e = "";
while (temp != null) {
if (temp == cursor)
e += String.format(" -> %-7d%-13.1f%-11.1f| %-18s%-16.1f%,-16.2f%s ", num,
temp.getCar().getCarLength(), temp.getCar().getCarWeight(),
(temp.getCar().getProductLoad() != null) ? temp.getCar().getProductLoad().getName() : "Empty",
(temp.getCar().getProductLoad() != null) ? temp.getCar().getProductLoad().getWeight() : 0,
(temp.getCar().getProductLoad() != null) ? temp.getCar().getProductLoad().getValue() : 0,
(temp.getCar().getProductLoad() != null && temp.getCar().getProductLoad().getDangerous())
? "YES" : "NO");
else
e += String.format(" %-7d%-13.1f%-11.1f| %-18s%-16.1f%,-16.2f%s ", num,
temp.getCar().getCarLength(), temp.getCar().getCarWeight(),
(temp.getCar().getProductLoad() != null) ? temp.getCar().getProductLoad().getName() : "Empty",
(temp.getCar().getProductLoad() != null) ? temp.getCar().getProductLoad().getWeight() : 0,
(temp.getCar().getProductLoad() != null) ? temp.getCar().getProductLoad().getValue() : 0,
(temp.getCar().getProductLoad() != null && temp.getCar().getProductLoad().getDangerous())
? "YES" : "NO");
temp = temp.getNext();
num++;
}
return a + b + c + d + e;
}
/**
* Returns a String version of the table output for findProduct(String name)
* method
*
* @return returns a neatly formatted table displayed in the
* findProduct(String name) method as a String
*/
public String searchProdString() {
return search;
}
}
TrainManager.java
public class TrainManager {
/**
* The main method runs a menu driver application which first creates an
* empty TrainLinkedList object; the program prompts the user for a command
* to execute an operation in accordance to the menu
*
*/
public static void main(String[] args) throws IllegalArgumentException, Exception {
TrainLinkedList train = new TrainLinkedList();
Scanner in = new Scanner(System.in); // Scanner for user-input
boolean cont = true;
String selection = "";
char selChar = ' ';
while (cont == true) { // program will continue as long as cont remains
// true
// Main menu of program
System.out.println();
System.out.println("(F) Cursor Forward");
System.out.println("(B) Cursor Backward");
System.out.println("(I) Insert Car After Cursor");
System.out.println("(R) Remove Car At Cursor");
System.out.println("(L) Set Product Load");
System.out.println("(S) Search For Product");
System.out.println("(T) Display Train");
System.out.println("(M) Display Manifest");
System.out.println("(D) Remove Dangerous Cars");
System.out.println("(Q) Quit");
System.out.println();
System.out.print("Enter a selection: ");
selection = in.nextLine().toUpperCase();
selChar = selection.charAt(0); // reads char at position 0 of
// String, which should be expected
// to be only one character
if (selection.length() > 1)
System.out.println(" Invalid Entry, please try again.");
// Goes on to the switch statement in accordance to user-input char
else {
switch (selChar) {
case 'F':
try {
train.cursorForward();
System.out.println(" Cursor moved forward.");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
break;
case 'B':
try {
train.cursorBackward();
System.out.println(" Cursor moved backward.");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
break;
case 'I':
System.out.println();
System.out.print("Enter car length in meters: ");
double carLen = in.nextDouble();
in.nextLine();
System.out.print("Enter car weight in tons: ");
double carWeight = in.nextDouble();
in.nextLine();
try {
TrainCar insert = new TrainCar(carLen, carWeight);
train.insertAfterCursor(insert);
System.out.printf(" New train car %.1f meters %.1f tons " + "inserted into train. ", carLen,
carWeight);
} catch (IllegalArgumentException ex) {
System.out.println(ex.getMessage());
}
break;
case 'R':
try {
TrainCar removedCar = train.removeCursor();
System.out.println(
"Car successfully unlinked. The following load has been removed from the train: ");
System.out.println();
System.out.println(" Name Weight (t) Value ($) Dangerous");
System.out.println("=============================================================");
String danger = (removedCar.getProductLoad() != null
&& removedCar.getProductLoad().getDangerous()) ? "YES" : "NO";
System.out.printf(" %-15s%-16.1f%,-16.2f%-7s ",
(removedCar.getProductLoad() != null) ? removedCar.getProductLoad().getName() : "Empty",
(removedCar.getProductLoad() != null) ? removedCar.getProductLoad().getWeight() : 0,
(removedCar.getProductLoad() != null) ? removedCar.getProductLoad().getValue() : 0,
danger);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
break;
case 'L':
System.out.println();
System.out.print("Enter product name: ");
String prodName = in.nextLine();
System.out.print("Enter product weight in tons: ");
double prodWeight = in.nextDouble();
in.nextLine();
System.out.print("Enter product value in dollars: ");
double prodValue = in.nextDouble();
in.nextLine();
System.out.print("Enter is product dangerous? (y/n): ");
String dangerousInput = in.nextLine();
char dangerousCheck = dangerousInput.charAt(0);
switch (dangerousCheck) {
case 'y':
try {
if (train.getCursorData().getProductLoad() != null) {
train.decProdWeight(train.getCursorData().getProductLoad().getWeight());
train.decProdValue(train.getCursorData().getProductLoad().getValue());
if (train.getCursorData().getProductLoad().getDangerous())
train.decDanger();
}
ProductLoad tempLoadDangY = new ProductLoad(prodName, prodWeight, prodValue, true);
train.getCursorData().setProductLoad(tempLoadDangY);
train.addProdWeight(prodWeight);
train.addProdValue(prodValue);
train.addDanger();
System.out.println();
System.out.println(prodWeight + " tons of " + prodName + " added to the current car.");
} catch (IllegalArgumentException ex) {
System.out.println(ex.getMessage());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
break;
case 'n':
try {
ProductLoad tempLoadDangN = new ProductLoad(prodName, prodWeight, prodValue, false);
train.addProdWeight(prodWeight);
train.addProdValue(prodValue);
train.getCursorData().setProductLoad(tempLoadDangN);
System.out.println();
System.out.println(prodWeight + " tons of " + prodName + " added to the current car.");
} catch (IllegalArgumentException ex) {
System.out.println(ex.getMessage());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
break;
default:
System.out.println("Invalid input for product danger input.");
break;
}
break;
case 'S':
System.out.println();
System.out.print("Enter product name: ");
String productName = in.nextLine();
train.findProduct(productName);
break;
case 'T':
System.out.println(train.toString());
break;
case 'M':
train.printManifest();
break;
case 'D':
try {
train.removeDangerousCars();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
break;
case 'Q':
cont = false;
System.out.println("Program terminating successfully...");
break;
default:
System.out.println("Invalid Entry, please try again.");
break;
}
}
// System.out.println(train.toString());
}
}
}
Output
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: I
Enter car length in meters: 15
Enter car weight in tons: 10.0
New train car 15.0 meters 10.0 tons inserted into train.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: M
CAR: LOAD:
Num Length (m) Weight (t) | Name Weight (t) Value ($) Dangerous
====================================+===============================================================
-> 1 15.0 10.0 | Empty 0.0 0.00 NO
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: L
Enter product name: Corn
Enter product weight in tons: 100.0
Enter product value in dollars: 15440
Enter is product dangerous? (y/n): n
100.0 tons of Corn added to the current car.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: M
CAR: LOAD:
Num Length (m) Weight (t) | Name Weight (t) Value ($) Dangerous
====================================+===============================================================
-> 1 15.0 10.0 | Corn 100.0 15,440.00 NO
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: I
Enter car length in meters: 18.5
Enter car weight in tons: 8.3
New train car 18.5 meters 8.3 tons inserted into train.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: M
CAR: LOAD:
Num Length (m) Weight (t) | Name Weight (t) Value ($) Dangerous
====================================+===============================================================
1 15.0 10.0 | Corn 100.0 15,440.00 NO
-> 2 18.5 8.3 | Empty 0.0 0.00 NO
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: L
Enter product name: Corn
Enter product weight in tons: 85
Enter product value in dollars: 13120
Enter is product dangerous? (y/n): n
85.0 tons of Corn added to the current car.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: M
CAR: LOAD:
Num Length (m) Weight (t) | Name Weight (t) Value ($) Dangerous
====================================+===============================================================
1 15.0 10.0 | Corn 100.0 15,440.00 NO
-> 2 18.5 8.3 | Corn 85.0 13,120.00 NO
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: T
Train: 2 cars, 33.5 meters, 203.3 tons, $28,560.00 value, not dangerous.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: F
No next car, cannot move cursor forward.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: B
Cursor moved backward.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: M
CAR: LOAD:
Num Length (m) Weight (t) | Name Weight (t) Value ($) Dangerous
====================================+===============================================================
-> 1 15.0 10.0 | Corn 100.0 15,440.00 NO
2 18.5 8.3 | Corn 85.0 13,120.00 NO
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: I
Enter car length in meters: 32.1
Enter car weight in tons: 17.4
New train car 32.1 meters 17.4 tons inserted into train.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: M
CAR: LOAD:
Num Length (m) Weight (t) | Name Weight (t) Value ($) Dangerous
====================================+===============================================================
1 15.0 10.0 | Corn 100.0 15,440.00 NO
-> 2 32.1 17.4 | Empty 0.0 0.00 NO
3 18.5 8.3 | Corn 85.0 13,120.00 NO
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: L
Enter product name: TNT
Enter product weight in tons: 25.0
Enter product value in dollars: 151500
Enter is product dangerous? (y/n): y
25.0 tons of TNT added to the current car.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: M
CAR: LOAD:
Num Length (m) Weight (t) | Name Weight (t) Value ($) Dangerous
====================================+===============================================================
1 15.0 10.0 | Corn 100.0 15,440.00 NO
-> 2 32.1 17.4 | TNT 25.0 151,500.00 YES
3 18.5 8.3 | Corn 85.0 13,120.00 NO
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: T
Train: 3 cars, 65.6 meters, 245.7 tons, $180,060.00 value, DANGEROUS.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: F
Cursor moved forward.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: S
Enter product name: Milk
There are no ProductLoad objects of the indicated name on board the train.
No record of Milk on board train.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: D
Dangerous cars successfully removed from the train.
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: M
CAR: LOAD:
Num Length (m) Weight (t) | Name Weight (t) Value ($) Dangerous
====================================+===============================================================
1 15.0 10.0 | Corn 100.0 15,440.00 NO
-> 2 18.5 8.3 | Corn 85.0 13,120.00 NO
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: R
Car successfully unlinked. The following load has been removed from the train:
Name Weight (t) Value ($) Dangerous
=============================================================
Corn 85.0 13,120.00 NO
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: M
CAR: LOAD:
Num Length (m) Weight (t) | Name Weight (t) Value ($) Dangerous
====================================+===============================================================
-> 1 15.0 10.0 | Corn 100.0 15,440.00 NO
(F) Cursor Forward
(B) Cursor Backward
(I) Insert Car After Cursor
(R) Remove Car At Cursor
(L) Set Product Load
(S) Search For Product
(T) Display Train
(M) Display Manifest
(D) Remove Dangerous Cars
(Q) Quit
Enter a selection: Q
Program terminating successfully...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.