0

I have this idea for my assignment where I wanted a cash register system to calculate the total for an item when the user enters it's cost price and quantity of said item.

That seemed to work, but then led my main problem - I wanted to let the user type the letter "T" after say, 10 transactions, to find out the total takings for the day.

I tried to use a for loop with the BigDecimal math class within the calculations etc. I have errors on the words 'valueOf' within my calculations & Eclipse keeps trying to change my values to 'long' & i'm pretty sure that's not right.

My explanation isnt amazing so i'll give you the code i wrote and place comments next to where my errors are ..

 try{
    Scanner in = new Scanner (System.in);       
    String t = "T";
    int count;

    for (count = 1;count<=10;count++){
           System.out.println("\n\nValue of Item " + count + " :");
       BigDecimal itemPrice = in.nextBigDecimal();
       System.out.println("Quantity of item " + count + " :");
       BigDecimal itemQuantity = in.nextBigDecimal();
       BigDecimal itemTotal = (BigDecimal.valueOf(itemPrice).multiply // error here
                                  (BigDecimal.valueOf(itemQuantity))); // error here
           System.out.println("\nTotal for item(s): £" + itemTotal); 
       count++;

           while (t == "T"){
       BigDecimal amountOfItems = (BigDecimal.valueOf(itemTotal).divide // error here
                                      (BigDecimal.valueOf(itemQuantity))); // error here 
       BigDecimal totalTakings = (BigDecimal.valueOf(itemTotal).multiply // error here
                                     (BigDecimal.valueOf(amountOfItems))); // error here
       System.out.println("The Total Takings For Today is £" + totalTakings + " " );

     }
    }  
   }
  }
 }

Like I said, the 'red lines' that eclipse uses to show there is an error are only under the words, "valueOf" within my BigDecimal calculations.

Any help would be great because i'm tearing my hair out !!!!

Thanx,

Vinnie.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
Vinnie
  • 1
  • 1
  • 1
    Note: never compare Java strings with `==`, as strings are *not* primitives, they are objects - hence the `==` is a reference compare, not an equality compare. Use the `.equals()` (or `.equalsIgnoreCase()`) method instead, as this will do the comparison you expect. – Clockwork-Muse Aug 05 '11 at 15:38

1 Answers1

2

There's no method BigDecimal.valueOf(BigDecimal). itemPrice and itemQuantity are already BigDecimal values - you don't need any conversion:

BigDecimal itemTotal = itemPrice.multiply(itemQuantity);

EDIT: Okay, so the above solves your immediate problem, but you've also got:

while (t == "T")
{
    // Loop that never changes the value of t
}

There are two issues with this:

  • The loop will always either execute forever, not execute at all, or keep going until an exception is thrown, because the loop condition can never change as you're not changing the value of t. My guess is you want t = System.in.readLine() at some point...
  • You're comparing two string references here whereas I suspect you want to compare their values, e.g.

    while (t.equals("T"))
    
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Oh wow yeh that seems good -- however i have caused an infinate loop and it cuts to the total takings calculation automatically and i wanted it to only occure when the user types in "T" ... – Vinnie Aug 05 '11 at 15:23
  • @Vinnie: Okay, this time I've edited the answer to explain the other problem in your code. But in general it's best to stick to *one* problem per question. – Jon Skeet Aug 05 '11 at 16:09