Welcome to SWYK check 2!

Time to show what y'all know!

Set up

Before continuing, verify each of the following:

  1. You have a partner, whose name you have memorized.
  2. The partners are sitting side by side with their laptops, and can both easily see both screens.
  3. The director has a browser open to this page: http://stephendavies.org/cpsc240/SWYKnum2.html
  4. The typist has two windows logged into the cpsc server.
  5. The typist has created a new, empty directory for this SWYK check.
  6. The typist has cd'd into that directory in both windows.

The typist should then perform the following commands to download the template and test harness for this SWYK check:

Then, if the typist doesn't already have them in their .bash_profile, set up the following aliases in your right-most window:

For this SWYK check, you can type compile in the right-most window to compile your code, and runtests to run the tests against it.

The actual problem

For this problem, you are to write a class called VendingMachine (spelled exactly that way, capitalized exactly that way) to represent the sugar-laden machines on college campuses:

How it works

Here's how this style of vending machine works:

  1. It dispenses only two products: Dr. Pepper and Starbucks frappuccinos. The former cost $1.25 each, and the latter, $8.00 each.
  2. When created, it is stocked with 8 Dr. Peppers and 6 Starbucks drinks.
  3. A customer can purchase an item buy putting coins through the coin slot. For this SWYK check we're not going to worry about the denominations of U.S. coinage, or verifying that the amount inserted is that of a single coin. (The user, in other words, can call a method to insert 57¢, even though there is no 57¢-coin in circulation.) The user can insert more money as many times as they like before pressing a selection.
  4. When the user presses either "buy Dr. Pepper" or "buy Starbucks," the operation will only work if they have inserted enough money before pressing it, of course. (If not, an Exception will be thrown to show something went wrong.)
  5. If the customer overpaid, they will get the appropriate change (as a return value). All change is dispersed as soon as a purchase is made. In other words, it's not possible for a customer to enter $2.50 and then press the "buy Dr. Pepper" button twice to get two sodas. Instead, they would get one soda and $1.25 in change. If they want their second soda, they'll have to reinsert their change first.
  6. If the machine is empty (of the type of drink the user requested) an Exception will be thrown to show something went wrong.
  7. If the user enters money but then changes their mind, they can press a cancel button to get it all back.

Remember: throughout this SWYK check, you do not have to worry about the exact types of coins that exist in customer's wallets and in the machine. Just treat amounts as doubles and add/subtract them as necessary. It's perfectly valid to add $1.13 worth of money all in one go, for example, even though there is no 113¢-coin.

The requirements

Instance variables

Your VendingMachine class should probably have instance variables. It's up to you to decide what they are.


Your VendingMachine class must have the following methods. You should use the VendingMachine.java template file as a starting point, which has the beginnings of each of these:

  1. A constructor that takes no arguments and instantiates a new, fully-stocked VendingMachine object.
  2. An .insertMoney() method that takes a double, and adds that amount of money to the customer's credit. .insertMoney() can be called any number of times to insert additional funds before making a purchase.
  3. A .cancel() method that will take all the inserted money and simply refund it to the customer.
  4. A .buyDrPepper() method that will attempt to buy a Dr. Pepper. It will return the amount of change after making this $1.25 purchase. If there are no more Dr Peppers in the machine, or if the user hasn't inserted enough money, this method should throw an Exception instead of returning.
  5. A .buyStarbucks() method that will attempt to buy a Starbucks drink. It will return the amount of change after making this $8.00 purchase. If there are no more Starbucks in the machine, or if the user hasn't inserted enough money, this method should throw an Exception instead of returning.
  6. A .refill() method that will reset the vending machine to its original state: 8 Dr. Peppers, 6 Starbucks, and no internal cash. The method will return the amount of money (in dollars) that the vending machine man retrieves from the machine when he comes to do the refill.

Compiling your code

Don't forget to compile your code (using the "compile" alias defined above) every time you write (save) a change in vim! Otherwise your changes will not be reflected in what's executed when you runtests.

Running your code

Executing the "runtests" alias will trigger the entire test suite. To get full credit for the SWYK check, you must pass all the test cases. When you do, you're done! Until then, continue to study your error messages and your code, and make the vending machine work properly.

Running your own main()

It is absolutely permissible (and a good idea, actually) to create your own main() method in the VendingMachine class, and use it to experiment with your own client code. Recall that it must look like this:

class VendingMachine {

    ...possibly other stuff...

    public static void main(String args[]) {
        ...Your client code that fiddles around with VendingMachine objects...

    ...possibly other stuff...

and that to run it, you would type:

$ java VendingMachine

Turning it in

When you have completed your class and gotten all the test cases to pass, do the following:

  1. Wave me over so I can look at y'all's screen and then give y'all a high-five.
  2. Download your VendingMachine.java code to your laptop (using Filezilla or any other method you choose) and then send it as an email attachment to both (1) cpsc240submissions@gmail.com and (2) the partner whose laptop is not being used for this assignment. The subject line of this email should be "SWYK check 2 turn-in".

If you can't get all the test cases before time's up, don't despair, but do perform step #2, above.