As of now, my "Shipping Cost" calculator runs well on an infinite loop. Here's how I'd like the program to be, if possible: prompt the user for the Weight and the Distance 13 times, then, AFTER 13 prompts for user input of weight & distance, display the weight, the distance, and the cost to ship the item for each of their input, if possible in 13 different lines. Here's the current code as it is, on an infinite loop. The driver is below.
Ex:
//User prompts x 13
Weight: #.##
Distance: ###.#
Weight: ##
Distance: ##
.
.
.
//Report x 13
Weight: #.## Distance: ###.# Cost: ##.#
Weight: ## Distance: ## Cost: ##.#
.
.
.
public class ShippingCharges {
private double weight;
private double shippingDistance; //class declarations
public ShippingCharges(){
}
//mutators and accessors (setters and getters)
public ShippingCharges(double weight, double shippingDistance) {
this.weight = weight;
this.shippingDistance = shippingDistance;
}
public double getShippingDistance() {
return shippingDistance;
}
public void setShippingDistance(double shippingDistance) {
this.shippingDistance = shippingDistance;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getShippingCharges(){
//Determines the rate by setting numPer500 = (distance/500.25), rounds up
double numPer500=(double)((int)(shippingDistance/500.25)+1);
//series of if statements to determine the shipping charges
if(shippingDistance==0) return 0.00*numPer500;
if(weight==0) return 0.00*numPer500;
if(weight<=2) return 1.10*numPer500;
if(weight>2&&weight<=6) return 2.20*numPer500;
if(weight>6&&weight<=10) return 3.70*numPer500;
if(weight>10) return 4.80*numPer500;
return 0;
}
//API- toString: "This object, (which is already a string!), is itself returned."
public String toString(){
return "Weight of package="+weight+"kg. Shipping distance="+shippingDistance+"km. Cost to send the package is $"+getShippingCharges();
}
}
DRIVER
import java.util.Scanner; //import scanner class
import java.text.NumberFormat; //NumberFormat class to help parse numbers
import java.text.DecimalFormat; //DecimalFormat class to help format decimals
public class TesterShippingCharges {
public static void main(String[] args){
NumberFormat formatter = new DecimalFormat("#0.00");
ShippingCharges num = new ShippingCharges();//num finds out the multiplication
Scanner scanner = new Scanner(System.in); // factor for the price
ShippingCharges scan=new ShippingCharges();
while (true){ //Causes infinite loop
System.out.print("Weight of package: "); // to continue prompting user.
double weight = scanner.nextDouble(); //Retrieves user's input weight
num.setWeight(weight); // and sets equal to weight.
System.out.print("Distance to be sent: "); //Prompts user for distance
double distance = scanner.nextDouble(); //Retrieves user's input distance
num.setShippingDistance(distance); // and sets eual to distance
scan.setWeight(weight);
scan.setShippingDistance(distance);
num.getShippingCharges();
System.out.println(scan);
if (weight==10000) break;
}
}
}
Thanks for reading! Thanks even more if you could help with a fix!
This post has been edited by MemoryLeak2013: 15 July 2010 - 02:20 PM

New Topic/Question
Reply




MultiQuote








|