This is an indiviual homework assignment. Please review Academic Integrity section of the class syllabus for rules on individual homework assignments.
Implement a program that simulates a simple vending machine. A vending machine is a machine that sells snacks/drinks: you insert money into the money slot, select an item you want to purchase, and if the amount of money you inserted is sufficient to pay for your selected item, your selected item is dropped into a dispenser compartment. If you supplied more than enough money, change is provided. Some vending machine accept credit cards. Our simulation will simply model a vending machine and will not deal with any real money. An example vending machine is shown below:
In your CSCI 2300 git repositories, you will find a hw1
directory. Place all the code you write for this homework into hw1
directory. IMPORTANT NOTE this is the directory I will be looking for when grading your solution. You are responsible for making sure your code is submitted to the correct directory. If you submit your code to some other directory, I will not find your solution.
We'll represent items that the vending machine sells with a Snack
class that has the following attributes:
Note that
price
is an integer and represents the price of the item in cents. For exaple, an item that costs $1.25 will have price=125. We will need a way to set values for the price
and name
attributes of Snack
objects. Include a constructor in your Snack
class that has the following method signature:
public Snack(String description, int price)
Snack
object with values provided as arguments to the constructor.
We'll represent a vending machine with a VendingMachine
class, which stores some number of Snack
objects in an attribute.The number of Snack
objects in a vending machine is not fixed: it can increase and decrearse as the vending machine is being used. What are the possible ways to represent such an attribute in a class? Determine how you will represent it.
To simulate a vending machine, we need to consider the possible uses (or use cases) that we need to model. Specifically, a vending machine operator may (1) add more snacks to the machine, (2) turn on the vending machine, (3) turn off the vending machine. A vending machine customer may (4) purchase an item from the machine. Therefore, we'll start by designing a vending machine class that can support the 5 operations (use cases) we outlined. Specifically, your VendingMachine
will need the following methods:
public void add(Snack snack)
: adds the given snack object to the vending machine.public void turnOn()
: sets the state of the vending machine to "on". You'll need to use an attribute to hold the vending machine state. What is a good data type to use for this?public void turnOff()
: sets the state of the vending machine to "off".public Snack sell(String snackName)
: finds and returns the Snack
object with the given snackName
. If no such Snack
object is found, returns null
. The machine needs to be "on" for this method to work. If the machine is "off", this method returns null
. How will you find the snack with the given snackName
?Snack
class and a VendingMachine
class that has methods outlined above. (Tip: start with a Snack
class and make sure your code compiles. Then add a VendingMachine
class and get this code to compile).
Let's now add a Driver
class with a main
method that uses you VendingMachine
class. Specifically, your main
method needs to:
System.out.println
to print the returned value. What method do you need to add to the Snack
class to make sure your output shows the price and name of the snack?. The vending machine should return null
. Why is that?System.out.println
to print the returned value. What method do you need to add to the Snack
class to make sure your output shows the price and name of the snack?.null
. Why is that?Driver
. Does it do what you expected it to do or did you get a NullPointerException
. If you got a NullPointerException
, it means you are using some variable that has not been instantiated. Most likely, it is a VendingMachine
variable/attribute that holds the collection of Snack
objects. Let's consider what happened. When you don't write a constructor for a class, a no-argument constructor is provided for you by default. However, the no-argument constructor will leave your class attributes uninitialized (or initialze them to default values). In the case of non-primitive data types, the default value is null, which is exactly what's causing the NullPointerException
when you use your VendingMachine
object. To fix this problem, add a no-argument constructor to the VendingMachine
class that instantiates the variable holding the collection of Snack
objects. Now run your driver again. Your output should look like this:
null
Twix: $1.00
null
Gum: $0.85
Pretzels: $1.05
Note, that the price is formatted as $<DOLLARS>.<CENTS>. For example, Pretzels should show the price of $1.05 and Twix should show the price of $1.00.
Submit your VendingMachine.java
, Snack.java
, and Driver.java
to hw1
directory of your CSCI 2300 git repo using git command line tool. Remember to use
git add FILE_NAME or DIRECTORY_NAME
git commit -m 'commit message'
git push
Additionally, select git commands and their output from your terminal window, copy your selection, and paste it into a file MY_COMMIT.txt. Submit this file to the same hw1
directory using git command line tool. This will allow me to verify that you used git command line tool to commit your code, which contributes to your grade for this homework.
hw1
directory of your CSCI 2300 repository. If you do not, it means that your solution is not submitted and I will not find it.
All submitted code must compile. Code that does not compile will receive a grade of zero. I will be compiling your submissions on hopper.slu.edu with the following command:
javac Driver.java
It is a good idea to verify that your code compiles on hopper.slu.edu using this command.
To grade your programs, I will use the list of questions below. Each question can score you from 0 to 2 points: No - 0, Somewhat - 1, Yes - 2. The sum of these points will be your grade for this homework.
VendingMachine
contain all required methods?sell
method of the VendingMachine
work correctly?Snack
class?Snack
and the VendingMachine
classes private?