Results 1 to 5 of 5
  1. #1
    space4rent00 is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default Trying to calculate cost based on size

    So I have my main VendTest
    Java Code:
    public class VendTest
    {
    	public static void main(String[] args)
    	{
    		Vending v = new Vending();
    		v.turnOn();
    		v.calcCosts();
    		v.turnOff();
    	}
    }
    and it's calling methods in Vending.
    Java 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]);
    	}
    }
    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 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);	
    	}
    }
    which extends off an Item class.
    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);
    	}
    }
    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.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Don't compare Strings with ==. Use .equals() instead. I don't know if that's your only problem, but fix that first.

    -Gary-

  3. #3
    space4rent00 is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    That worked, I'm getting the right prices now. Thank you so much =)

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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:
    Java Code:
    //in Drink
    public void getData() {
      super.getData(); //calls getData() in Item
      //second JOptionPane about size
    }
    One of the main benefits of OOP is code reusability, the sooner you make this your habit, the better.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    Quite apart from which, any method with a name that starts with get... should return a value other than void. Those methods might be better named readData or inputData.

    db

Similar Threads

  1. i need an example of JSR179 ((Location based Ser)implementation for CDC based device
    By talk_to_vivekmishra in forum CDC and Personal Profile
    Replies: 3
    Last Post: 12-30-2010, 10:07 AM
  2. Check difference between no. of stops, calculate cost
    By JavaStudent23 in forum New To Java
    Replies: 1
    Last Post: 11-14-2009, 04:03 PM
  3. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 04:37 PM
  4. Replies: 0
    Last Post: 04-12-2008, 08:38 PM
  5. Replies: 1
    Last Post: 07-23-2007, 11:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •