Question: This assignment provides practice creating and testing Java classes with a main method. 1. First, we will create a VendingMachine.java class to represent a vending

This assignment provides practice creating and testing Java classes with a main method.

1. First, we will create a VendingMachine.java class to represent a vending machine.

public class VendingMachine { //fields, constructors, methods... }

2. Next, you and your group will create the VendingMachineTester.java class that will contain the main method and can prompt and receive input from the keyboard using the Scanner class. This class will test the Vending Machine object

as outlined below.

Objectives

Write fields, constructors, and methods for a class you create.

To write and use getter, setter, and toString() methods in a class.

To write code segments that work around the constraints of encapsulation

Use the Scanner object to read in input from the keyboard

Perform input validation on user's input using indefinite loops

Part 1: We will create the VendingMachine.java which is described below as a class

Class Description
VendingMachine
 

Your class should have the following field that is encapsulated:

inventory

The class should have the following components:

A default constructor which assigns 20 as the default number of drinks(inventory) in the machine.

A getter(accessor) method for the number of drinks (inventory) in the machine

A purchase() method that receives the number of drinks that were purchased and updates the inventory field properly

A restock() method that receives the number of drinks that were placed in the machine and updates the inventory field properly

A public String toString() method that returns a String representation of the VendingMachine object. The format of the string should look like this: Inventory: 20 bottles.

Part 2: You will create the VendingMachineTester.java class with your group

This class will have the main method. In the main method of VendingMachineTester.java you should complete the following tasks:

1. Construct a VendingMachine object using the default constructor

2. Prompt the user for the number of drinks to purchase using a Scanner object. (Do not allow more drinks to be purchased than in the inventory)

3. Call the purchase() method on this VendingMachine object for the number of purchased drinks

4. Prompt the user for the number of drinks to restock using a Scanner object. (Do not allow the user to place more drinks in the inventory then the machine can hold (20) )

5. Call the restock() method on this VendingMachine object for the number of restocked drinks

6. Call the toString() method on this VendingMachine object to display the inventory.

Sample Program Runs:

Enter number of drinks to purchase: 5 Enter number of drinks to restock: 2 Inventory: 17 bottles ______________________________________
Enter number of drinks to purchase: 5 Enter number of drinks to restock: 10 Try Again - You can restock 0 - 5 drinks Enter number of drinks to restock: 3 Inventory: 18 bottles

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!