public class SpendingSpree
1: import java.util.Scanner;
2: public class SpendingSpree
3: {
4: public static final int SPENDING_MONEY = 100;
5: public static final int MAX_ITEMS = 3;
6: public static void main(String[] args)
7: {
8: Scanner keyboard = new Scanner(System.in);
9: boolean haveMoney = true;
10: int leftToSpend = SPENDING_MONEY;
11: int totalSpent = 0;
12: int itemNumber = 1;
13: while (haveMoney && (itemNumber <= MAX_ITEMS))
14: {
15: System.out.println("You may buy up to " +
16: (MAX_ITEMS - itemNumber + 1) +
17: " items");
18: System.out.println("costing no more than $" +
19: leftToSpend + ".");
20: System.out.print("Enter cost of item #" +
21: itemNumber + ": $");
22: int itemCost = keyboard.nextInt( );
23: if (itemCost <= leftToSpend)
24: {
25: System.out.println("You may buy this item. ");
26: totalSpent = totalSpent + itemCost;
27: System.out.println("You spent $" + totalSpent +
28: " so far.");
29: leftToSpend = SPENDING_MONEY - totalSpent;
30: if (leftToSpend > 0)
31: itemNumber++;
32: else
33: {
34: System.out.println("You are out of money.");
35: haveMoney = false;
36: }
37: }
38: else
39: System.out.println("You cannot buy that item.");
40: }
41: System.out.println("You spent $" + totalSpent +
42: ", and are done shopping.");
43: }
44: }
45: