
Adding nested loop in order to print out a table
dylan terry
Greenhorn
Joined: May 11, 2011
Posts: 1
posted
May 11, 2011 16:37:14
0
My final project for my university is to enhance my code in order to have it print out the total price including the discount but before shipping charges and taxes. The rows should be items sold and the columns should be the wait of the items. I am starting to get frustrated, been at this for hours cant seem to figure it out. As a hint he says to use nested loops?
Heres my codes I have 1 main class and 1
test
class
package assignment3; 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; } }
and my
Main Class
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); while ((Items <=0 || (Items >10000000))) 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 ive been really struggling!
[HENRY: Fixed code tags. And did some formatting]
Steve Fahlbusch
Bartender
Joined: Sep 18, 2000
Posts: 595
7
I like...
posted
May 11, 2011 18:49:31
0
Greetings and welcome.
-- now that the greetings are done.
you should use code tags for your code --- please read the FAQs prior to posting.
you did not pose a question --- this is not a codemill ---- please read the FAQs prior to posting.
Please focus and give us a question with your understanding and we shall try to point you in the proper direction.
After you have read the FAQs, you may want to answer the following questions for us:
Where are you having difficulty?
Why are you having this difficuly?
How would you like to approch this problem?
And what do you see as going wroing with your approch?
-steve
Jared Malcolm
Ranch Hand
Joined: May 02, 2011
Posts: 54
I like...
posted
May 11, 2011 19:28:41
0
dylan terry wrote:
As a hint he says to use nested loops?
I don't see where you've followed his hint first off.....
SCJA 6 (Studying for SCJP 6)
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 11874
18
I like...
posted
May 12, 2011 07:04:46
0
No offense, but your code is kind of a mess... You don't follow conventions, so it gets very confusing. For example, when I see "Items", Since the first letter is capitalized, I think it is a class, not a reference.
You have a class called "DataEquations", but then make a reference variable it called 'price'. A price is not an equation, or a set of equations...a price should be (at least in my mind) a number.
On line 21 of your PriceCalculator class, you have a while loop, with an...odd...condition. I'm not saying it is wrong, but it doesn't look like anything that makes any sense to me. Why have these magic numbers?
Your while statement doesn't have any curly braces, so there is only one line in it's body. So let's assume I input a value of '2'. Since 2 is not <= 0, or > 10000000 (however many 0's you have), it skips the loop body, which is only the line 24. Then lines 26-45 all run.
Now, let's assume i put in a value of "-2". This makes the condition TRUE, and now i'm stuck in an infinite loop printing "Base Price = $-84.00" (or whatever price I input) forever, since you never change anything that would affect your condition.
so...I am assuming you need some major revisions to your code here. I would guess that you are supposed to ask the user how many items to ship (that part seems to work). I would then guess that you need a loop that gets the input weight of each and every item, one at a time, an stores all those values in a collection of some kind (and Array or
ArrayList
would work fine).
Once you have that done, I would work on printing out the data for each element.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Adding nested loop in order to print out a table
Similar Threads
Why is my type unexpected? required variable found value
Wanted: Java2D example of zooming and panning with scroll bars
my first Java nightmare
My program wont work.
Illegal start?
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
JForum
|
Paul Wheaton