New to Java Can not figure out how to create a table with my codes outputs!
It is my final project for my university and it was a 101 course. I can not figure out how to create a table that will represent the price with the discount but before shipping charges and taxes. My current code is
package assignment3;
Code :
public class DataEquations {
private int ItemsPurchased; // first number to add
private double ItemWeight; // second number to add
private double PricePerPound = 3.00; // sum of number1 and number2
private double taxrate = .01; // Tax Rate
private double Discount;
private double VariablePrice;
public DataEquations() {
}
public DataEquations(int Items, double Weight) {
ItemsPurchased = Items;
ItemWeight = Weight;
}
public void setItems(int Items) {
ItemsPurchased = Items;
}
public void setWeight(double Weight) {
ItemWeight = Weight;
}
public int getItems() {
return ItemsPurchased;
}
public double getWeight() {
return ItemWeight;
}
public double getPriceBeforeTaxes() {
return ItemsPurchased * ItemWeight * PricePerPound;
}
public double getPriceAfterTaxes() {
return ((getPriceBeforeTaxes() * getDiscount()) + getShipPrice() + getBoxPrice()) * (1 +getTax());
}
public double getTotalWeight(){
return ItemsPurchased * ItemWeight;
}
public double getTax(){
return (taxrate);
}
public double getDiscount() {
if (ItemsPurchased >= 25 & (ItemsPurchased < 50)){
Discount = 1 - .05;
} else if (ItemsPurchased >= 50 & (ItemsPurchased < 100)) {
Discount = 1 - .10;
} else if (ItemsPurchased >= 100 & (ItemsPurchased < 200)) {
Discount = 1 - .15;
} else if (ItemsPurchased >= 200 & (ItemsPurchased < 500)) {
Discount = 1 - .20;
} else if (ItemsPurchased >= 500& (ItemsPurchased < 1000)) {
Discount = 1 - .25;
} else if (ItemsPurchased >= 1000) {
Discount = 1 - .30;
} else if (ItemsPurchased < 25)
Discount = 1;
return Discount;
}
public double getShipPrice(){
if (getTotalWeight() >= 0 & (getTotalWeight() < 10)){
VariablePrice = 2.00;
} else if (getTotalWeight() >= 10 & (getTotalWeight() < 50)) {
VariablePrice = 4.00;
} else if (getTotalWeight() >= 50 & (getTotalWeight() < 100)) {
VariablePrice = 8.00;
} else if (getTotalWeight() >= 100 & (getTotalWeight() < 500)) {
VariablePrice = 12.00;
} else if (getTotalWeight() >= 500& (getTotalWeight() < 1000)) {
VariablePrice = 20.00;
} else if (getTotalWeight() >= 1000) {
VariablePrice = 30.00;
}
return VariablePrice + 2.95;
}
public double getBoxPrice() {
return getTotalWeight() / 10 * 2 ;
}
public int getBoxes() {
int Boxes = (int)(getTotalWeight() / 10 + .9999);
return Boxes;
}
}
package assignment3;
import java.util.Scanner;
public class PriceCalculator {
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
DataEquations price = new DataEquations();
System.out.print( "Enter Number of Items Purchased: " ); // prompt
int Items = input.nextInt(); // read first number from user
price.setItems(Items);
System.out.print( "Enter Weight of Items: " ); // prompt
double Weight = input.nextDouble(); // read second number from user
price.setWeight(Weight);
System.out.printf("Base Price = $%.2f\n", price.getPriceBeforeTaxes());
System.out.printf("Discount = $%.2f\n", price.getPriceBeforeTaxes() * (1- price.getDiscount()));
System.out.printf("Base Price Including Discount = $%.2f\n", price.getPriceBeforeTaxes() * price.getDiscount());
System.out.printf("Ship Price = $%.2f\n", price.getShipPrice());
System.out.printf("Base Price With Discount and Ship Price = $%.2f\n", price.getPriceBeforeTaxes() * price.getDiscount() + price.getShipPrice());
System.out.printf("Number of Boxes Needed = %d\n", price.getBoxes());
System.out.printf("Box Price = $%.2f\n", price.getBoxPrice());
System.out.printf("Subtotal = $%.2f\n", price.getPriceBeforeTaxes() * price.getDiscount() + price.getShipPrice() + price.getBoxPrice());
System.out.printf("Tax on Purchase = $%.2f\n", (price.getPriceBeforeTaxes() * price.getDiscount() + price.getShipPrice() + price.getBoxPrice()) * price.getTax());
System.out.printf("Total Price = $%.2f\n", price.getPriceAfterTaxes());
}
}
Thanks for all the help!
ps. the table's rows should represent the number of items sold, and the columns are the weight of items in pounds
Re: New to Java Can not figure out how to create a table with my codes outputs!
I'm not sure I understand what your problem is.
Do you know what the contents of the table are and don't know how to output the data?
Is the output to be printed on a console screen using print or how are you going to display it?
Can you create a sample output using pencil and paper so you know what goes in each row and column?
Quote:
ps. the table's rows should represent the number of items sold, and the columns are the weight of items in pounds
Can you enter some data showing what you mean here? This doesn't make any sense to me.