1

Here is my assignment:

Write a program that will emulate a cash register. Prompt the user to input the price of three items. Add them together to get a subtotal. Determine the tax (6% ) on the subtotal. Find the total amount of the sale subtotal plus tax. Display the price of each item, subtotal amount, tax amount and final amount.

I have it done like this:

package cashregisteremulator;

import java.util.Scanner;

public class CashRegisterEmulator {

  public static void main(String[] args) {

    Scanner price = new Scanner(System.in);


    System.out.print("Please enter a price for item number one $");
    double price1 = price.nextDouble();

    System.out.print("Please enter a price for item number two $" );
    double price2 = price.nextDouble();

    System.out.print("Please enter a price for item number three $");
    double price3 = price.nextDouble();

    double total = ((price1) + (price2) + (price3));
    System.out.println("The subtotal is $" + total);

    double tax = .06;

    double totalnotax = (total * tax );
    System.out.println("The tax for the subtotal is $" + totalnotax);
    double totalplustax = (total + totalnotax);
    System.out.println("The total for your bill with tax is $" + totalplustax);

  }
}

However, I need to do this assignment using a loop. Since the prompt asks for only 3 iterations, I thought of using a for loop. So far I have this:

package CashRegister;

import java.util.Scanner;

 public class CashRegister {

public static void main(String[] args) {
    Scanner askPrice = new Scanner(System.in);  

   for(double i = 0 ; i < 3; i++);  
 {
   System.out.println("Enter a value") ;
   double price = askPrice.nextDouble();



 }
}    
}

What do I have to do in order to ask the user for a price three times?

2
  • Why do you ask the user to enter a double, then assign it to counter then you set counter to be 0? Commented May 23, 2013 at 14:03
  • I'm not exactly sure what you're asking. Commented May 23, 2013 at 14:22

3 Answers 3

1

I'm not going to write all of your code for you, but this can be easily achieved using a for loop:

int numberOfIterations = 3;

// The format of the for loop:
// for (initialize counter; continuation condition; incrementer)
for(int i = 0; i < numberOfIterations; i++){
    // do something
}
Sign up to request clarification or add additional context in comments.

Comments

1
double sum = 0;
for(int i = ; i < n; i++)
{
    //ask for value;
    sum += value;
}

update

in your update this must go into the for loop:

double[] amount = new int[3];
for(int i = 0; i < amount.len; i++)
{
    // ask user for value
    amount[i] = value;
}

for(int i = 0; i < amount.len; i++)
{
     // do your calculations and print it.
}

// print the total sums.

For the totals you will need extra variables.

3 Comments

No reason to get overly complicated. If the OP doesn't understand a for loop, they're not going to understand interfaces and collections. Stick with an array (especially since there's a predefined fixed number of iterations).
Yeah, I just realized it and rewrote it into a simple version as not even an array is needed. :)
It would go into the for loop. Wether you really need an array depends on how the final list should look like. It definitely would be a good idea to have an array for what the user inputs, and then you can loop over it, and do the individual calculations and printing.
0

You need to make three iterations and for such scenario, the best thing to use is a for loop. In order to store the user inputs, you need an array in which you store the values with the index of the current position in the for loop.

Take a look at this example I built on your code:

double[] price= new double[3];
for (int i=0; i<3; i++); {
     System.out.println("Enter another ");
     double price[i] = price.nextDouble();
} 

Later on, you can iterator over the price array, and add all the values:

double total = 0.0;
for (int i=0; i<price.length; i++) {
    total += price[i];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.