Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Consider a vending machine that offers the following options:
[1] Get gum
[2] Get chocolate
[3] Get popcorn
[4] Get juice
[5] Display total soldDesign a program that continually allows users to select from these options. When options 1-4 are selected an appropriate message is to be displayed acknowledging their choice.
The program terminates when option 5 is selected, at which point the total number of each type of item sold is displayed. For example:
3 items of gum were sold
2 items of chocolate were sold
6 items of popcorn were sold
9 items of juice were soldIf an option other than 1-5 is entered an appropriate error message should be displayed.
CODE
/* program displays choice of items available from a vending machine, gets user's selection, then prints total number of items vended */
public class Chapter3VendingMachine
{
public static void main(String[] args)
{
int response;
System.out.println("VENDING MACHINE");
System.out.println();
do
{
System.out.println("[1] Get GUM");
System.out.println("[2] Get CHOCOLATE");
System.out.println("[3] Get POPCORN");
System.out.println("[4] Get JUICE");
System.out.println("[5] Display TOTAL ITEMS");
System.out.println("Enter choice: [1, 2, 3, 4, 5]");
response = EasyIn.getInt();
switch(response)
{
case '1': System.out.println("Here's your GUM!"); break;
case '2': System.out.println("Here's your CHOCOLATE!"); break;
case '3': System.out.println("Here's your POPCORN!"); break;
case '4': System.out.println("Here's your JUICE!"); break;
default: System.out.println("Options [1-5] only!");
}
}
while(response != 5);
}
}
As you can see, I've written the bulk of the program - all except the code needed to display the total number of items chosen.
The only solution I've come up with is to declare an array [n] to store the user's choices. I don't think I'm expected to use an array though, because I'm on Chapter 3, and arrays haven't been introduced yet? There must be a simple solution, but what? I have no idea.
PLEASE help! This is driving me nuts!

Why not just add an integer variable initiated a 0, then add 1 to it inside each of the case statements where something is bought? Then, in the 5th case, output that variable.

You are correct, an array would be the 'right' way to keep track of this, but using integer variables is probably acceptable since array's haven't been introduced.
But you'll need one int for each category that you'll want to count, ie:
int numGum;
int numChocolate;
int numPopcorn;etc.
Then, as Nick said, increment them inside the case.
But, to impress the teacher, using an array would be pretty easy.I don't know if Java arrays start at zero, or 1. If they start at zero, you'd do the following:
int count[6] = { 0,0,0,0,0,0 };
Then somewhere in your code:
++count[response];
I'll leave it to you to figure out the best place. Be sure that you understand the array code if you use it tho.

![]() |
Delphi software
|
Why doesnt my Serial ATA ...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |