0
Beginner Java help please!
https://code.sololearn.com/c38gYM4CdTNa/?ref=app This program requires me to Prompt a user to enter customer id, unit price in this format (e.g. 3.75), quantity (as whole number), product description, and discount in this format (e.g., .10) (use Scanner for input). (2) Calculates the customer's overall order total before and after discount. (3) Displays the input data along with the order total before and after the discount to the console. I stuck now and I think I need to use BigDecimal?
12 Antworten
+ 1
check line 34, need to declare var first ...
0
It is unusual to write variable names, such as input for the Scanner, with a capital letter in Java. Also, you are overwriting "doubleNumber" with the last input. Better to use meaningful names as you did for customerId for example.
What are you stuck at now and why do you think you would be needing BigDecimals?
0
I feel like i am stuck on writing the code to output the order total before and after taxes
0
So i should use something like doubleDiscount and from what I keep reading because this is dealing with monitary value its better to use BigDecimal
0
But you know the formula, right? And you have all the variables... the price per quantity, the quantity and the discount. You do know how to multiply variables to calculate new values from them, I reckon.
You are all set :)
0
Ah, because of the monetary values! Well, yes. The question is, do you really need the accuracy? Meaning, is this an academic exercise in getting you to think in algorithm to solve a particular problem and learn the Java language, or are you dealing with a real life piece of software to be used in finance?
0
Academic excercise and yes i know the formula thank you so much for helping :)
0
You're welcome :)
In an academic setting, the next question is: Have you studied BigDecimal, and do you suspect that this exercise aims at recognising the need for BigDecimals? If not, then stick with the double type.
0
It was only put in our tips and tricks section of the class but we have not apecifically talked about it yet
0
I see. Well, you can make one with a double type. And for some practising with the BigDecimals, you can make another version using BigDecimals. Can't hurt :)
With the BigDecimals you need to operate solely within BigDecimals. So, all numbers that you need to add and multiply need to be BigDecimals. Since you use them in order NOT to lose precision, you should READ the values as string from the input (not as double values) and construct BigDecimals from them. There is a BigDecimal constructor that takes a string representation of a number.
Adding and multiplying is then just to use the methods of the BigDecimal classes, namely .multiply() and .add(). You may end up with more than two decimals after the decimal point. Then you need to round. I believe in the finance world, rounding half-even is common practice...
0
Thank you so much! This really helped!! :)
0
Well, I'm happy to hear that :)