Results 1 to 5 of 5
- 03-21-2010, 11:06 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
Trying to calculate cost based on size
So I have my main VendTest
and it's calling methods in Vending.Java Code:public class VendTest { public static void main(String[] args) { Vending v = new Vending(); v.turnOn(); v.calcCosts(); v.turnOff(); } }
My problem is with clacCosts. When I'm trying to calculate the cost and then output the cost. It is being output as 0 no matter what my size is. I'm calculating the cost in a Drink ClassJava Code:import javax.swing.JOptionPane; public class Vending { private Item item[]; private int count; public Vending() { item = new Item[10]; count = 0; } public void setCount(int c) { count=c; } public int getCount() { return count; } public void turnOn() { count=Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the number of items")); for(int i=0; i < count; i++) { String type = JOptionPane.showInputDialog(null, "Enter item type"); if (type.equals("Drink")) { Drink d = new Drink(); d.getData(); item[i] = d; } else if (type.equals("Coffee")) { Coffee c = new Coffee(); c.getData(); item[i] = c; } else if (type.equals("Snack")) { Snack s = new Snack(); s.getData(); item[i] = s; } else JOptionPane.showMessageDialog(null, "Invalid type"); } } public void calcCosts() { for(int i=0; i < count; i++) { item[i].calcCost(); } } public void turnOff() { for(int i=0; i < count; i++) System.out.println("Item " + i + " data is " + item[i]); } }
which extends off an Item class.Java Code:import javax.swing.JOptionPane; public class Drink extends Item { private String size; public Drink() { super (" "); size = " "; } public Drink(String n, String s) { super(n); size = s; } public void setSize(String s) { size = s; } public String getSize() { return size; } public void calcCost() { if (size == "S") setCost(.75); else if (size == "M") setCost(1.00); else if (size == "L") setCost(1.50); } public String toString() { return super.toString() + " and it is a size " + size; } public void getData() { String n = JOptionPane.showInputDialog(null, "Enter the name of your drink"); setName(n); String s = JOptionPane.showInputDialog(null, "Enter the size"); setSize(s); } }
From what I can tell when Drink is trying to calculate the cost, the if statement doesn't know the size I entered thus leaving it at 0. But I can't figure out why.Java Code:import javax.swing.JOptionPane; public abstract class Item { private String name; private double cost; public Item(String n) { name = n; cost = 0; } public void setName(String n) { name = n; } public String getName() { return name; } public void setCost(double c) { cost = c; } public double getCost() { return cost; } public abstract void calcCost(); public String toString() { return " This items name is " + name + " and it costs $" + cost; } public void getData() { String n = JOptionPane.showInputDialog(null, "Enter the name"); setName(n); } }
- 03-21-2010, 11:19 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Don't compare Strings with ==. Use .equals() instead. I don't know if that's your only problem, but fix that first.
-Gary-
- 03-22-2010, 01:03 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
That worked, I'm getting the right prices now. Thank you so much =)
- 03-22-2010, 06:48 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Also, learn why you extend classes. If you take a look at your getData() methods, the first few lines are the same in Item and Drink. Since Drink extends Item, why not do this:
One of the main benefits of OOP is code reusability, the sooner you make this your habit, the better.Java Code://in Drink public void getData() { super.getData(); //calls getData() in Item //second JOptionPane about size }
- 03-22-2010, 07:13 AM #5
Similar Threads
-
i need an example of JSR179 ((Location based Ser)implementation for CDC based device
By talk_to_vivekmishra in forum CDC and Personal ProfileReplies: 3Last Post: 12-30-2010, 10:07 AM -
Check difference between no. of stops, calculate cost
By JavaStudent23 in forum New To JavaReplies: 1Last Post: 11-14-2009, 04:03 PM -
Setting frame size to the size of an image
By Yoruichi in forum AWT / SwingReplies: 5Last Post: 04-22-2009, 04:37 PM -
Finding a connection between cities using least-cost search algorithm
By Java Tip in forum java.langReplies: 0Last Post: 04-12-2008, 08:38 PM -
difference between code based security and role based security
By boy22 in forum New To JavaReplies: 1Last Post: 07-23-2007, 11:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks