-5

I don't know a lot about java if yous can could you keep your replies simple to understand.

I am making a simple checkout program. The program will let the user input the amount items they bought & calculate the total bill.

The way I am going about this is I will print a item to the screen and the the user inputs the amount of that item they bought and then the program will move onto the next item in the array. This is my code so far

class Checkout{

    public static void main(String args[]){
        String items[] = {
                "Milk", "Bread", "Butter", "Cheese", "Orange Juice", "Sugger","Potatoes", "Sausages","Cabbage","Noodles"};
        int price;
        for (String str : items) {
            System.out.println(str);
        }

    }
    }

I have no clue on how to print the array one at a time. I would also like to get the user to input the amount they bought.

I hope you guys understand what I am saying, Thanks

  • Your program already prints the items one at a time. What is missing is the code asking the user to enter an amount after each item. Have you googled for "read integer from command line in Java"? – JB Nizet Mar 14 '15 at 22:46
  • You will also need a price per item, perhaps? – puj Mar 14 '15 at 22:47
  • If you feel that you're knowledge about Java is too low then maybe a good place to start would be the [tutorials](http://docs.oracle.com/javase/tutorial/) that Oracle provides. – jpw Mar 14 '15 at 22:48

1 Answers1

0

A couple things you'll need to add.

First, prices for the items.

int[] prices = { 3, 2, 4, 5, 6, 1, 2, 7, 1, 2 };

Second, a way to collect user input from the console (java.util.Scanner).

Scanner input = new Scanner(System.in);

Third, a way to calculate the total price. Instead of using the enhanced for loop as for (String str: items) it would be more appropriate using the price list above to enumerate the items.

for (int i = 0; i < items.length; i++) {
    System.out.print("How many " + items[i] + "?");
    int howMany = input.nextInt();
    price += prices[i] * howMany;
}

At that point, price should be the total bill as you wanted.

gknicker
  • 5,509
  • 2
  • 25
  • 41
  • Thanks didn't think about that. I find it hard to think of all the variables I will need when I start a program. I will get to work on that thanks :) – Jack Bourke Mar 14 '15 at 23:06