I have to write a program that calculates the shipping cost for something being sent in the mail.
Here are some specifications and what a practice run should look like:
Calculations:
Use: Ship Method Cost Overnight $5 * weight Two Day $2 * weight Economy $1 * weight (yes, that's just = weight) Data Validation On this program, you'll also need to perform some validation in the main method. Specifically:
The item description must not be empty. The item weight must be > 0. The shipping method must be either O, T or E. The program should accept either lower or uppercase equivalents. Sample Runs: Be sure to end your output with a println.
Run #1: No item description entered:
Enter item description: <-- user hits enter key but doesn't enter item description Invalid description. Program cannot continue
Run #2: Invalid item weight entered:
Enter item description: A big box <-- user enters valid description Enter item weight in lbs: -5 <-- user enters invalid weight Invalid shipping weight. Program cannot continue
Run #3: Invalid shipping method entered:
Enter item description: A big box Enter item weight in lbs: 3.5
How fast would you like to ship your package: (O)vernight (T)wo Days (E)conomy (may take up to 7 days) Choose an option: P Invalid shipping method. Program cannot continue
Run #4: Invoice Generated
Enter item description: A big box Enter item weight in lbs: 3.5
How fast would you like to ship your package: (O)vernight (T)wo Days (E)conomy (may take up to 7 days) Choose an option: o
*** WE SHIP INVOICE *** Item Description: A big box Item Weight: 3.50 Ship Method: O Total Cost: $17.50
Decomposition You must use the decomposition provided below. For maximum sanity, remember to build and test one method at a time in development mode. Two of these methods have already been written for you and supplied as part of you code template for this exercise.
Use: double getWallHeight(Scanner scnr) - This method is called from the main method. It accepts the scanner as a parameter, prompts the user for the wall height and returns the value entered by the user to the main method.
Use: double getWallWidth(Scanner scnr) - This method is called from the main method. It accepts the scanner as a parameter, prompts the user for the wall height and returns the value entered by the user to the main method.
Use: double calcWallArea(double wallHeight, double wallWidth) - This method is called from the main method. It accepts the wallHeight and wallWidth and calculates the wall area in square feet.
Use: double calcGallons(double wallArea) - This method is called from the main method. It accepts the wall area and determines how much paint is need to paint the entire room.
Use: void displayResults(double wallArea, double gallonsPaintNeeded) - This method is called from the main method. It accepts the wallArea and the amount of paint needed to paint the room. It displays the results to the user including the walk area and the number of paint cans needed to be purchased as an integer.
Here is my code:
import java.util.*;
import java.util.Scanner;
public class WeShipIt {
public static final int OVERNIGHT_CHARGE = 5;
public static final int TWO_DAY_CHARGE = 2;
public static final int ECONOMY_CHARGE = 1;
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in); //scanner object to pass around
double weight;
String itemDescription;
char shipMethod;
double shippingCost;
itemDescription = getitemDescription(keyboard);
weight = getShipWeight(keyboard);
shipMethod = getShipClass(keyboard);
shippingCost = calculateShipping(shipMethod, weight);
if (itemDescription.length() == 0){
System.out.println("Invalid description. Program cannot continue");
System.exit(0);
} else {
getShipWeight(keyboard);
if (weight <= 0){
System.out.println("Invalid shipping weight. Program cannot continue");
} else {
getShipClass(keyboard);
if (!(shipMethod == 'O' || shipMethod == 'T' || shipMethod == 'E')){
System.out.println("Invalid shipping method. Program cannot continue");
} else {
displayResults(itemDescription, weight, shipMethod, shippingCost);
}
}
}
}
//get item description
public static String getitemDescription(Scanner keyboard){
System.out.println("Enter item description:");
String itemDescription = keyboard.next();
return itemDescription;
}
//get item weight
public static double getShipWeight(Scanner console){
System.out.println("Enter item weight in lbs:");
double itemWeight = console.nextDouble();
return itemWeight;
}
//get user's choice for shipping method and return it
public static char getShipClass(Scanner keyboard) {
char shipMethod;
//get shipping method
System.out.println();
System.out.println("How would you like to ship your package:");
System.out.println("(O)vernight");
System.out.println("(T)wo Days");
System.out.println("(E)conomy (may take up to 7 days)");
System.out.println("Choose an option: ");
shipMethod = keyboard.next().charAt(0); //Prof. Dan says leave this line in here. Will explain in
class.
shipMethod = Character.toUpperCase(shipMethod);
return shipMethod;
}
//calculate and return shipping charge
public static double calculateShipping(char shipMethod, double weight){
double shipCharge;
if (shipMethod == 'O') {
shipCharge = weight * OVERNIGHT_CHARGE;
}
else if (shipMethod == 'T') {
shipCharge = weight * TWO_DAY_CHARGE;
}
else if (shipMethod == 'E') {
shipCharge = weight * ECONOMY_CHARGE;
} else {
shipCharge = 0;
}
return shipCharge;
}
//display shipping charge invoice
public static void displayResults(String itemDescription, double shipWeight, char shipMethod, double
shipCost) {
System.out.println();
System.out.println("*** WE SHIP INVOICE ****");
System.out.println("Item Description: " + itemDescription);
System.out.printf("Item Weight: %.2f\n" + shipWeight);
System.out.println("Ship Method: " + shipMethod);
System.out.printf("Total Cost: $%.2f\n" + shipCost);
}
}
This is the output when I use "box 3.5 0" as my input:
Enter item description: Enter item weight in lbs:
How would you like to ship your package: (O)vernight (T)wo Days (E)conomy (may take up to 7 days) Choose an option: Enter item weight in lbs:
I don't have to reprint out the input's given, but I am very confused why it asks for the item's weight again and doesn't printout the final result.
I am also getting this error:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at WeShipIt.getShipWeight(WeShipIt.java:67)
at WeShipIt.main(WeShipIt.java:35)
I have tried for hours to fix this on my own, but I am asking for help as a last resort. I am new to computer science and probably made some very dumb mistake. Thank you