Question: implement Slot.java and SlotTest.java The Slot class models a slot in the vending machine. Slots are loaded from the rear, and purchases are removed from

 implement Slot.java and SlotTest.java The Slot class models a slot in

implement Slot.java and SlotTest.java

The Slot class models a slot in the vending machine. Slots are loaded from the rear, and purchases are removed from the front. This ensures that the items that have been in the machine the longest are sold before newly added items.

Data

SLOT_SIZE: int = 10 - the maximum number of products that a slot in the vending machine can hold.

products: ArrayList - models the actual products that are in the slot. Removing the one at the front models buying one of the products in the slot and all of the others are moved forward similar to an actual vending machine.

Slot()

The Slot() constructor creates an empty array list of products.

Slot(Product product)

This constructor creates a new slot that is filled with SLOT_SIZE of product.

load(Product product)

This method loads the slot with however many new products are required to make sure it is full and returns the number of products it took to fill the slot.

load(Product product, int count)

This method loads the slot with up to count new products in an attempt to fill the slot and returns the number of products it used.

nextProduct()

This method returns a reference to the next product available for purchase. If the slot is empty this method will return null.

buyOne()

This method simulates the purchase of one item from the perspective of the slot. That means no money is dealt with here, rather the slot is checked to make sure there is product to buy and then one product is removed from the front of the ArrayList modeling the slot. If a product is successfully removed from the slot, it is returned, otherwise null is returned.

toString()

The toString() method returns a String exactly like the one below for a slot with 10 M&M products. Items should start with the front slot and end with the rear slot.

SlotCount: 10 of Product: M&Ms Cost: 1.02 Price: 1.25. Product: M&Ms Cost: 1.02 Price: 1.25. Product: M&Ms Cost: 1.02 Price: 1.25. Product: M&Ms Cost: 1.02 Price: 1.25. Product: M&Ms Cost: 1.02 Price: 1.25. Product: M&Ms Cost: 1.02 Price: 1.25. Product: M&Ms Cost: 1.02 Price: 1.25. Product: M&Ms Cost: 1.02 Price: 1.25. Product: M&Ms Cost: 1.02 Price: 1.25. Product: M&Ms Cost: 1.02 Price: 1.25.

Hint

Hint: Dont forget to make use of other toString() methods.

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!