Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coding problem

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0

hi,
may I know what is wrong with my codes below? The problem that I am facing now is that the value of the total_price is always the same, 0. I think the problem lies in the if-else statements...but I cannot see anything that is wrong with it. Can anyone who is kind enough to help me spot it?

Codes:
public static void calculation(Vector sold, c1 a, c1 b, c1 c, c1 d, c1 e, c1 f, c1 g )
{
double total_price = 0.00;
c1 p, q, r, s, t , u, v;

while(sold.size() != 0) {
System.out.println(((Item)sold.firstElement()).getcode());

if(((Item)sold.firstElement()).getcode() == "c1") {
p = a;
total_price += p.getprice();
p.sold();
}
else if(((Item)sold.firstElement()).getcode() == "c2") {
q = b;
total_price += q.getprice();
q.sold();
}
else if(((Item)sold.firstElement()).getcode() == "c3") {
r = c;
total_price += r.getprice();
r.sold();
}
else if(((Item)sold.firstElement()).getcode() == "p1") {
s = d;
total_price += s.getprice();
s.sold();
}
else if(((Item)sold.firstElement()).getcode() == "p2") {
t = e;
total_price += t.getprice();
t.sold();
}
else if(((Item)sold.firstElement()).getcode() == "k1") {
u = f;
total_price += u.getprice();
u.sold();
}
else if(((Item)sold.firstElement()).getcode() == "k2"){
v = g;
total_price += v.getprice();
v.sold();
}

sold.remove(0);
}//while

System.out.println("Total amount due: $" + total_price);

}//sold

Thanks
 
First, you should be using more meaningful variable and class names then c1 and a,b,c,... etc. It makes it much easier to read the code.

Secondly, you should be using the equals() method to check for equality between Strings not ==.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top