I'm new to Java and in the book I'm reading, Java Programming by Joyce Farrell, things have gotten very complicated very quickly. I've spent well over ten hours on this problem and think I've butchered it to the point of no return. The problem is to create a delivery class that puts the year, invoice number, code (1 for long distance delivery and 2 for overnight), and a weight. Based on the weight, it determines the fee. Finally, using a boolean I have to determine if the person wants overnight delivery (an extra 35) or not (no extra charge).
I made a lot more progress than I thought I ever would but I'm still completely stuck on how to tie it all together, especially how to show the displayFee in the main and get it to print out something understandable. I genuinely do not know what to do and every minor correction I make leads to a bunch of errors and it not compiling. Can someone please help me?
This is what I have for the class:
public class Delivery { int year; int delNum; double weight; int code; boolean day = false; boolean regular = false; double fee; boolean overNight = true; public Delivery(int year, int delNum, int code, double weight, boolean overNight)//constructor for data. { this.year = year;//year and delNum will combine for delivery number. this.delNum = delNum; this.code = code;//must be (1) or (2) this.weight = weight; this.overNight = overNight; if(overNight == true) { System.out.println("it's true"); } else { System.out.println("it's false"); } } public String toString() { String info = "Delivery year = " + year + " Delivery Number = " + delNum + " code = " + code + " weight = " + weight + "Charge for overnight delivery is:" + overNight; return info; } public double displayFee(double weight)//used to calculate fees with if...else if loop(s). { double fee = 0.0; if(code == 1) { if(weight < 5) { fee = 12; } else if(weight >= 5 && weight <= 20) { fee = 16.50; } else if(weight > 20) { fee = 22.00; } else if(code == 2) { if(weight < 5) { fee = 35.00; } else if(weight >= 5) { fee = 47.95; } } } return fee; } public void displayFee(int year, int delNum, int code, double weight)//calls methods { System.out.println("Year is: " + year); System.out.println("Delivery number is: " + delNum); System.out.println("Delivery code is: " + code); System.out.println("Weight is: " + weight); } }
The main:
import java.util.Scanner; public class CreateDelivery { public static void main(String[] args) { int year; int delNum; double weight; int code; double fee; double displayFee; boolean overNight = true; Scanner input = new Scanner(System.in); System.out.println("Hi! What is the current year?"); year = input.nextInt(); while(year < 2001 || year > 2025) { System.out.println("You have entered an invalid year."); System.out.println("Please select a year between 2001 and 2025."); year = input.nextInt(); } System.out.println("Please enter your four digit delivery number"); delNum = input.nextInt(); while(delNum < 1 || delNum > 9999) { System.out.println("You have entered an invalid delivery number."); System.out.println("Please select a delivery number between 1 and 9999."); delNum = input.nextInt(); } System.out.println("Please enter your code."); code = input.nextInt(); while(code < 1 || code > 2) { System.out.println("You have entered an invalid code number."); System.out.println("Please select 1 for local delivery or 2 for long distance."); code = input.nextInt(); } System.out.println("Please enter the weight of your package"); weight = input.nextInt(); while(weight < .10 || weight > 100) { System.out.println("You have entered an invalid weight"); System.out.println("Please select a weight between .10 pounds and 100 pounds."); weight = input.nextDouble(); } System.out.println("Please enter 1 for overnight delivery or 2 for regular"); overNight = input.nextBoolean(); if(overNight == true){ System.out.println("This will add $35 to your total.");} else if(overNight == false){ System.out.println("This will add no additional charges."); } Delivery firstDelivery = new Delivery(year, delNum, code, weight, overNight); System.out.println(firstDelivery.toString()); } { } }
And this is the ouput (I have no idea what it means or how to correct it):
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextBoolean(Scanner.java:1825) at CreateDelivery.main(CreateDelivery.java:65) ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.