Computing.Net > Forums > Programming > Basic Java Program (A Vending Machi

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Basic Java Program (A Vending Machi

Reply to Message Icon

Name: MiKeY
Date: September 13, 2004 at 14:18:38 Pacific
OS: WinXP Home
CPU/Ram: 2.4GHz Althlon XP/768MB
Comment:

Consider a vending machine that offers the following options:

[1] Get gum
[2] Get chocolate
[3] Get popcorn
[4] Get juice
[5] Display total sold

Design 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 sold

If 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!



Sponsored Link
Ads by Google

Response Number 1
Name: Dr. Nick
Date: September 13, 2004 at 17:18:52 Pacific
Reply:

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.


0

Response Number 2
Name: Don Arnett
Date: September 13, 2004 at 18:25:06 Pacific
Reply:

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.


0

Response Number 3
Name: MiKeY
Date: September 14, 2004 at 02:11:40 Pacific
Reply:

thanks guys, i'll give that a try!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Delphi software Why doesnt my Serial ATA ...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Basic Java Program (A Vending Machi

Visual Basic: Is there a learner's thing www.computing.net/answers/programming/visual-basic-is-there-a-learners-thing/924.html

C++ GUI in a java program www.computing.net/answers/programming/c-gui-in-a-java-program/16600.html

Please help with java program. www.computing.net/answers/programming/please-help-with-java-program/14834.html