0% found this document useful (0 votes)
22 views2 pages

Homework 4

This homework assignment requires students to create a Java program simulating a vending machine using switch statements and loops. The program should handle item selection, validate user input, calculate total costs, and display the final bill upon exit. The assignment is due on November 5, 2024, and includes specific requirements and sample outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Homework 4

This homework assignment requires students to create a Java program simulating a vending machine using switch statements and loops. The program should handle item selection, validate user input, calculate total costs, and display the final bill upon exit. The assignment is due on November 5, 2024, and includes specific requirements and sample outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ENG 200-04A: Computer Programming for Engineers Dr.

Badour Albahar

Homework #4
October 29, 2024
Due Sunday, November 5, 2024 at 11:59PM in Moodle
Collaborators: (LIST OF YOUR COLLABORATORS, OR WRITE NONE)

1. Vending Machine Simulation with Switch, Continue, and Break Statements.


Write a Java program that simulates a vending machine, using a ‘switch’ statement to handle
item selection and a loop to process additional orders until the user decides to exit.

(a) The vending machine offers the following items:


• 1 - Soda (1.5 Dinars)
• 2 - Chips (0.75 Dinars)
• 3 - Chocolate (1.25 Dinars)
• 4 - Exit
(b) Program Requirements:
• Prompt the user to select an item number or 4 to exit.
• If an invalid item number is entered, display "Invalid selection" and use continue
to prompt again.
• When the user selects a valid item, ask for the quantity. If the quantity is negative,
display "Quantity cannot be negative" and ignore that selection (use continue).
• Calculate the total cost for each selection and add it to the final bill.
• When the user selects 4 to exit, print the total bill amount and terminate the pro-
gram.

Sample Output 1:
Select an item number:
1 - Soda (1.5 Dinars)
2 - Chips (0.75 Dinars)
3 - Chocolate (1.25 Dinars)
4 - Exit
5
Invalid selection
Select an item number:
1 - Soda (1.5 Dinars)
2 - Chips (0.75 Dinars)
3 - Chocolate (1.25 Dinars)
4 - Exit
4
Total cost: 0.0

Homework 4 1 Due November 5, 2024


ENG 200-04A: Computer Programming for Engineers Dr. Badour Albahar

Sample Output 2:
Select an item number:
1 - Soda (1.5 Dinars)
2 - Chips (0.75 Dinars)
3 - Chocolate (1.25 Dinars)
4 - Exit
1
Enter quantity: 2
Select an item number:
1 - Soda (1.5 Dinars)
2 - Chips (0.75 Dinars)
3 - Chocolate (1.25 Dinars)
4 - Exit
2
Enter quantity: 1
Select an item number:
1 - Soda (1.5 Dinars)
2 - Chips (0.75 Dinars)
3 - Chocolate (1.25 Dinars)
4 - Exit
3
Enter quantity: 1
Select an item number:
1 - Soda (1.5 Dinars)
2 - Chips (0.75 Dinars)
3 - Chocolate (1.25 Dinars)
4 - Exit
4
Total cost: 5.0

Sample Output 3:
Select an item number:
1 - Soda (1.5 Dinars)
2 - Chips (0.75 Dinars)
3 - Chocolate (1.25 Dinars)
4 - Exit
1
Enter quantity: -1
Invalid quantity
Select an item number:
1 - Soda (1.5 Dinars)
2 - Chips (0.75 Dinars)
3 - Chocolate (1.25 Dinars)
4 - Exit
4
Total cost: 0.0

Homework 4 2 Due November 5, 2024

You might also like