I'm trying to answer the following question: The following pseudo code describes how a book store computes the price of an order from the total price and the number of books that were ordered. Please write the java code needed to create this.
- Read the total book price and number of books.
- Compute the tax (7.5 % of the total book price).
- Compute the shipping charge ($2 per book).
- The price of the order is the sum of the total book price, the tax, and shipping charge.
- Print the price of the order.
I'm new to programming so sorry if there's a lot of mistakes. Here's what I have so far:
package test2;
import java.util.Scanner;
public class test2 {
public static void main (String args[]){
Scanner inputfromscanner = new Scanner(System.in);
double bookprice=0, taxpercentage, shippingcharge, totalbeforetax,
totalwithtax, totalwithshipping;
int numbooks;
taxpercentage = 1.075;
shippingcharge = 2;
System.out.print("Please enter the number of books:");
numbooks = inputfromscanner.nextInt();
for(int x=0;x<=numbooks;x++){
if( x == numbooks ) {
break;
}
System.out.print("Please enter the before-tax price of the book:");
bookprice = inputfromscanner.nextDouble();
}
inputfromscanner.close();
totalbeforetax = bookprice;
totalwithtax = totalbeforetax*taxpercentage;
totalwithshipping = totalwithtax + (numbooks*shippingcharge);
System.out.print("The total price of the order is: $");
System.out.println(totalwithshipping);
}
}
An example of the output:
Please enter the number of books:5
Please enter the before-tax price of the book:10.50
Please enter the before-tax price of the book:20.50
Please enter the before-tax price of the book:30.50
Please enter the before-tax price of the book:40.50
Please enter the before-tax price of the book:50.50
The total price of the order is: $64.2875
So I realized what I've done wrong but I'm not sure how to fix it. The "totalbeforetax= bookprice;" line is incorrect. This should be the sum of all the book prices entered. So, in this example the math would be totalbeforetax= 10.50 + 20.50 + 30.50 + 40.50 + 50.50 = 152.50. What's happening is each time I type in a price for a book, it get's overwritten by whatever book price I enter next. So when I enter 40.50 and then 50.50, 40.50 gets thrown out, and only 50.50 is used in the calculation.
I need a way to store each of these book price values independently. I don't want to create variables like bookprice1, bookprice2, bookprice3..etc. because I need it to be an infinite number of book prices. I'm not sure how to tell it to hold each one of those values separately. Maybe there's some sort of variable that I can tell it to add 1 onto the end of a variable then sum these together?
Also I'm not sure what "bookprice=0" is doing. I'm using Eclipse as an IDE and it told me to change it to have "=0" on the end.
Explaining the math. Here's an example if a user entered 5 book prices:
$10.50 + $20.50 + $30.50 + $40.50 + $50.50 = $152.50 (totalbeforetax)
$152.50 * 1.075 = $163.9375 (totalwithtax)
$163.9375 + ( 5 books * $2 per book shipping fee) = $173.9375 (totalwithshipping)
I've assumed taxes are not included on the shipping fee and that the shipping fee is added after since it wasn't specifically stated in the question.
What my program is doing:
$50.50 = $50.50 (totalbeforetax)
$50.50 * 1.075 = $54.2875 (totalwithtax)
$54.2875 + ( 5 books * $2 per book shipping fee) = $64.2975 (totalwithshipping)
Also other thing's I would like to do to make things look pretty (which seem like small issues but I think have complex solutions) would be to round the answer to 2 decimal places since its money, but I'm not sure how to do it. So if the total price of the order is $64.2875, it would only show $64.29. (so I'm taking off 2 numbers and also rounding it at the same time)
Another thing I would like to do is that sometimes the answer may say something like "The total price of the order is: $65.5". Is there a way to add a zero on the end of this so it would say $65.50 instead? How can I tell it to add one zero on the end only when the answer has one number after the decimal place?
I hope I've explained everything in detail. Please ask if you have a question. Thank you for your time and help! I really appreciate it!