# Thread: Trying to calculate cost based on size

1. Member Join Date
Feb 2010
Posts
3
Rep Power
0

## 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;
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.  Reply With Quote

2. Senior Member Join Date
Mar 2010
Posts
952
Rep Power
10

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

-Gary-  Reply With Quote

3. Member Join Date
Feb 2010
Posts
3
Rep Power
0

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

4. Senior Member Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
10

## 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
}```
One of the main benefits of OOP is code reusability, the sooner you make this your habit, the better.  Reply With Quote

5. ## 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  Reply With Quote

#### Posting Permissions

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