Hey Everyone,
I'm new to this form, and I seem to like it so far. Anyways, I have this Project that i'm nearly done with but in the sub-class I'm getting 0.0 as an output instead of the actual calculation. Below is the project and the code:
You are requested to design the class Package with instance variables representing package number, weight, and shipping method. The shipping method is a character: A for Air, T for Truck, and M for Mail. The class contains a constructor that requires arguments for weight and shipping method. The constructor should create automatic unique package numbers. The class defines a calculateCost()method that determines the shipping cost of a package based on the following:
Weight (in kg) Shipping method
-----------------------Air---Truck---Mail
1 - to - 8 ------------2.00---1.50---0.5
9 - to - 16 -----------3.00---2.35---1.50
17-to-over ------------4.50---3.25---2.15
The package class also contains a method display()that displays the values of a package object.
Create a subclass InsuredPackage that adds an insurance cost to the shipping cost based on the following:
Shipping cost before insurance (in $)-----Additional cost
10.00 to 100.00 --------------------------15.00
10.001 to 300.00--------------------------25.50
301.00 to over --------------------------34.00
Create a class Owner that represents the owner of a package. The class should contain an array reference that references the packages belonging to the owner, in addition to the name, mailing address, and owner ID (created automatically and uniquely by the constructor).
Write a test program that interacts with the user to create a number of owners and packages belonging to them. The program must allow the user to request a report showing information about owners and their packages.
My code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test2;
/**
*
* @author moe
*/
import java.util.*;
public class TestMain {
public static void main (String[] args){
Scanner in = new Scanner(System.in);
Package pf = new Package ();
Owner of = new Owner ();
InsuredPackage ip = new InsuredPackage ();
int userChoice;
boolean quit = false;
System.out.println ("Please Enter Your Name:" );
Owner ob1 = new Owner ();
String name = in.nextLine();
System.out.println ("Please Enter Your Address:" );
Owner ob2 = new Owner ();
String address = in.nextLine();
System.out.println ("Owner Name is:" + name
+ "\nOwner Address is:" + address
+ "\nUnique User ID is:" + of.getOwnerID()
+ "\nPackage number " + pf.getPackageID() + " belongs to you.");
do {
System.out.println("1. Air");
System.out.println("2. Truck");
System.out.println("3. Mail");
System.out.println("0. Quit");
System.out.print("Your choice: ");
userChoice = in.nextInt();
switch (userChoice) {
case 1:
System.out.println ("You have choosen to Ship by Air.");
System.out.println ("Please enter the package weight");
double weight = in.nextDouble();
pf.weight = weight;
System.out.println("Your Shipment Will Cost:" + pf.getAir());
System.out.println("Insurance will cost" + ip.getRange());
break;
case 2:
System.out.println ("You have choosen to Ship by Truck.");
System.out.print("Please enter the package weight: ");
pf.weight = in.nextDouble();
System.out.println(pf.getTruck());
System.out.println("Insurance will cost" + ip.getRange());
break;
case 3:
System.out.println ("You have choosen to Ship by Mail.");
System.out.print("Please enter the package weight: ");
pf.weight = in.nextDouble();
System.out.println(pf.getMail());
System.out.println("Insurance will cost" + ip.getRange());
break;
case 0:
quit = true;
break;
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
System.out.println("Thank you!");
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test2;
/**
*
* @author moe
*/
import java.util.*;
public class Package {
private double pnumber;
public double weight;
private double Air;
private double Truck;
private double Mail;
private double cost;
public Package (double cost, double pnumber, double weight, double Air, double Truck, double Mail){
this.cost = cost;
this.pnumber = pnumber;
this.weight = weight;
this.Air = Air;
this.Truck = Truck;
this.Mail = Mail;
}
/**
*
*/
public Package(){
}
public void setPackageID (double pnumber){
this.pnumber = pnumber;
}
public double getPackageID (){
Random rand = new Random();
int pick = rand.nextInt(40)+1;
return pick;
}
public void setAir (double Air){
this.Air = Air;
}
public double getAir (){
if (weight <= 1) {
System.out.println("Please enter the correct amount of weight.");
}
else {
if (weight <= 8) {
return weight * 2;
}
}
if ((weight >= 9) && (weight <= 16)) {
return weight * 3;
}
else {
if (weight >= 17) {
return weight * 4.50;
}
}
return cost;
}
public void setTruck (double Truck){
this.Truck = Truck;
}
public double getTruck (){
if (weight <= 8) {
System.out.println("The Shipment of package will cost $" + weight * 1.50);
}
else {
if ((weight >= 9) && (weight <= 16)) {
System.out.println("The Shipment of package will cost $" + weight * 2.35);
}
else {
if (weight >= 17) {
System.out.println("The Shipment of package will cost $" + weight * 3.25);
}
}
}
return getCost();
}
public void setMail (double Mail){
this.Mail = Mail;
}
public double getMail (){
if (weight <= 8) {
System.out.println("The Shipment of package will cost $" + weight * 0.50);
}
else {
if ((weight >= 9) && (weight <= 16)) {
System.out.println("The Shipment of package will cost $" + weight * 1.50);
}
else {
if (weight >= 17) {
System.out.println("The Shipment of package will cost $" + weight * 2.15);
}
}
}
return getCost();
}
/**
* @return the cost
*/
public double getCost() {
return cost;
}
/**
* @param cost the cost to set
*/
public void setCost(double cost) {
this.cost = cost;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test2;
/**
*
* @author moe
*/
public class InsuredPackage extends Package {
private double range;
public InsuredPackage (double cost, double pnumber, double weight, double Air, double Truck, double Mail){
super (cost, pnumber, weight, Air, Truck, Mail);
}
public InsuredPackage (){
super ();
}
public void setRange (double range){
this.range = range;
}
public double getRange(){
if (getCost() == 10 && getCost() <= 100) {
return getCost() + 15;
}
if (getCost() == 100.01 && getCost() <= 300) {
return getCost() + 25.50;
}
if (getCost() >= 300.01) {
return getCost() + 34;
}
return range;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test2;
/**
*
* @author moe
*/
import java.util.*;
public class Owner extends Package {
private double onumber;
private String name;
private String address;
public void setOwnerID (double onumber){
this.onumber = onumber;
}
public double getOwnerID (){
Random rand = new Random();
int opick = rand.nextInt(40)+1;
return opick;
}
public void setName (String name){
this.name = name;
}
public String getName (){
return name;
}
public void setAddress (String address){
this.address = address;
}
public String getAddress (){
return address;
}
}
My Output is correct but its showing the Insurance as 0.0
Please help, any would be appreciated !!