Chapter 5: Goodies Co. Examples

The Goodies Company Project

vendingmachines

From Wikipedia:
In computer programming, an application programming interface (API) is a set of routines, protocols, and tools for building software applications. An API expresses a software component in terms of its operations, inputs, outputs, and underlying types. An API defines functionalities that are independent of their respective implementations, which allows definitions and implementations to vary without compromising the interface. A good API makes it easier to develop a program by providing all the building blocks. A programmer then puts the blocks together.

Cay Horstmann:
A class should represent a single concept. The public methods
and constants that the public interface exposes should be cohesive.
That is, all interface features should be closely related to the single
concept that the class represents.
If you find that the public interface of a class refers to multiple concepts,
then that is a good sign that it may be time to use separate classes instead.

NO CODE – It is no programming language related!

Examples of public interfaces for the Goodies Company Application:
Example 1:

Class BD_VendingMachine:
public BD_VendingMachine: initializes allocated change and sets profit to 0
void addChange(value) adds allocated change
void addProduct(BD_Product) adds a product to the machine
double getBalance() returns profit
double getChange() returns allocated change remaining
void purchase(BD_Product, double money) buys product inputted and determines change to return, subtracting from allocated change accordingly

Class BD_Product:
public BD_Product(string name, double price)initializes new product and price

Example 2:
Classes:

Class 1 – snack
name, price, stock
can set the name price and amount of the item
can change the price of the item
will decrement the amount when the item is purchased

Class 2 – machine
sales, list of snacks
holds several objects of the class snack
method for purchasing an item which will decrement the snack, increase sales, and output change
method for restocking the amount of a snack
can remove and add snacks
can return the total sales of a machine

Class 3/ Main Class –
runs several of the machines
will buy snacks from various machines
will restock the machines
will check the total sales and amount of snacks in each machine

Example 3:
Vending Machine Project Outline

VendingMachine —> Class
– Array of Products
– Amount initially put in for change
– Total
– Name / Location
addProduct() – allows you to add a product object to the inventory of the machine
addChange() – allows you to add change to the machine
getProfit() – subtracts the added change from the total to display the profit of the machine
buy() – takes in a Product object, and a money value (possibly in coin objets) and allows you to “buy” the product

Product —> Class
– name
– price
getPrice()
getName()
toString()

Coin —> Class
– name
– value
getValue()
getName()
toString()

Example 4:
buyDrink: contains getPrice, giveChange, will give an error message if there are none left
toString: will show everything in the machine and its prices (and quantities?)
addChange: adds to the machine’s change supply w/o buying anything
getQuantity: tells you how many of a drink are in the machine

contains different types of drinks (Drink class?)
takes coins form Coin class

Example 5:
order(x) – The order method is called whenever the user places an order. It decreases the
quantity of the specific item by 1 and adds the price to the overall profit. The item the person
order is given by x, which is an integer that corresponds to the object.
makeChange(x, y) – The makeChange method is always called right after the order method. It
takes in the item as it’s first parameter and the amount the user put into the machine as it’s
second. Then, it returns how much the machine owes the user.
getProfit() – The getProfit method returns the total profit the machine makes.
getQuantity() – The getQuantity method returns the total quantity left of each item in the
machine.

Example 6:
Classes:
endingMachine: This class deals with Product objects and the number of Product objects that exist. This also deals with money taken from the user.
Product: This contains instance variables for different products with their descriptions and prices.
Driver: represents the user’s interaction with the vendingMachine

Methods (VendingMachine):
sellProduct: This method gives a product to the user by lowering the stock of that product and increases profit but checks if the amount of inserted money is enough for the purchase. And if not it refuses it and uses the giveChange method to return the rest.
collectMoney: Collects only the amount of money needed by the machine and allows for sellProduct method.
giveChange: Gives the remaining amount of money that was given but not required to purchase the product.
restockItems: Resets the amount of goods that exist in the vendingmachine.

Product Methods:
getPrice: gives price of product
getProductName: gives the name of the product chosen