0

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!

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Jack
  • 3
  • 2

3 Answers3

0

Just initialize a double totalCost = 0; and then when you read the book proce just add it to totalCost += bookprice ;

Calculate the tax and shipping cost on totalCost.

Also in you for loop you can use

for(int x=0;x<numbooks;x++){......}

instead of

for(int x=0;x<=numbooks;x++){......}
JNL
  • 4,683
  • 18
  • 29
0

Use an array to hold the individual book prices and use totalbeforetax for the running total. (It's anyways not being used enough with the simple totalbeforetax = bookprice;)

numbooks = inputfromscanner.nextInt();
double[] bookprices = new double[numbooks]; // as many prices as books

Then scan your prices and compute the total as well within the loop.

for(int x=0; x<numbooks;x++){
    /* if( x == numbooks ) { // removed; condition updated to "<" instead
      break;
    } */
    System.out.print("Please enter the before-tax price of the book:");
    bookprices[x] = inputfromscanner.nextDouble();
    totalbeforetax += bookprices[x];
}

This way you get your totals as well as don't loose the individual book prices in case you need them.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0

You don't actually need to store the price of each individual book. Instead, you can add the price to totalbeforetax every time the user enters one.:

for(int x=0; x<=numbooks; x++){

    if( x == numbooks ) {
        break;
    }
    System.out.print("Please enter the before-tax price of the book:");
    totalbeforetax += inputfromscanner.nextDouble();
}

This way you can completely get rid of the bookprice variable.

As for your other questions...

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.

What this is doing is initialising the variable. The reason Eclipse suggested this is that most types of object are initialised as null until they have been given a value. This actually isn't the case with doubles because they're 'value types', which can't be null. Instead they're initialised to 0. It's a good idea to get into the habit of initialising variables when they're defined anyway though, which is probably why Eclipse suggested it.

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?

You can use a NumberFormat object, like this:

NumberFormat formatter = NumberFormat.getCurrencyInstance();

System.out.print("The total price of the order is: ");
System.out.println(formatter.format(totalwithshipping));

(Code taken from here: https://stackoverflow.com/a/2379425/626796)

Community
  • 1
  • 1
Tharwen
  • 3,057
  • 2
  • 24
  • 36