Vending Machines

Introduction

It’s the year 2021, and you have graduated from Georgia Tech. Your first real job is with Coca-Cola, who is trying to spearhead the vending machine market by developing specialized vending machines for college campuses. The first models will be placed in the CULC and CoC, and are catered specifically to Tech students. Since you have experience with the student body, you have been tasked with writing the software. However, Coke wants to make sure that their product will be an overwhelming success. For that reason, you will first write a simulator for the machine to test the functionality.

Problem Description

You will be writing classes that represent various parts of a vending machine. You will need to write two files: VendingMachine.java and VendingItem.java. A simple driver class VendingWorld.java has been provided, and it will allow you to interact with your simulation within the commandline/terminal. Specific instructions for each file are given in later sections.

Background

This section contains some helpful information about material that appears on this homework. If you are more experienced with Java, you may already know everything here. However, we still suggest reading this section to make sure all the techniques are clear.

Enums

An enum is a special class that has a set of predefined instances. An enum is an enumerated type, and each possible value has an associated numeric value. Enums are used when you have a small set of possible values that you want to represent with a convenient syntax. For example, instead of having an int (or String) representing colors where some value stands for red, some value for green, and some value for blue, you could have the following enum type:

public enum Color {
    RED, GREEN, BLUE;
}

// in other parts of the code...

if (color == Color.RED || color == Color.BLUE) {...}

Another feature of enums is the ability to given them properties. For this assignment, you will need to use an enum of vending items that also records each item’s price. If you don’t remember how to give an enum properties, take a look at the oracle tutorials. For our situation, we are using an enum in almost exactly the same way you would use a class.

Random

The Random class is used to generate numbers in a certain range. After importing java.util.Random, you must create a new Random object. For example, Random rand = new Random(). In order to generate random integers in a certain range, take note of the nextInt() method. Remember that the argument is exclusive, which means that it is 1 larger than the maximum value the method will generate. This might seem counter intuitive, but it gives us a useful rule. Given a call like rand.nextInt(5), we know that exactly 5 values (that is , 0, 1, 2, 3, and 4) can be generated.

There are two very common techniques involving random numbers that you will need to use in this homework. The first is generating random integers in a range that does not start at 0. Imagine you want to generate numbers ranging from 5 to 10, inclusive on both ends (i.e, the numbers 5, 6, 7, 8, 9, 10). Since there are six possible values, we need the parameter of nextInt() to be 6. However, we also want the smallest number to be 5 (instead of 0). To accomplish this, we can simply add 5 to the random value. This suggests the following formula for generating random integers between a and b, inclusive: int n = rand.nextInt(b - a + 1) + a.

The second technique is using random numbers to generate a percent chance for an event. For example, you might want something to have a 25% chance of happening on each iteration through a loop. In that case, you could do something like:

if (rand.nextInt(4) == 0) {
    // do something...
}

For more complicated percentages, like 30%, you can think of the percent as a fraction where the numerator and denominator are both whole numbers (for example 30 / 100). Then, you can generate numbers in the range of the denominator, and compare them against the numerator. To have an event with a 30% chance of happening, you could write:

if (rand.nextInt(100) < 30) {
    // event happens
} else {
    // event doesn't happen
}

Solution Description

VendingItem.java (enum)

Instances of this enum will represent items in your vending machine. Your boss at Coke has done extensive analysis and determined that the following items and prices are ideal for Georgia Tech students:

Item Price
Lays $1.50
Doritos $1.50
Coke $2.50
Ramblin_Reck_Toy $180.75
Rubiks_Cube $30.00
Rat_Cap $15.00
FASET_Lanyard $10.00
Graphing_Calculator $120.00
UGA_Diploma $0.10
Pie $3.14
Clicker $55.55
Cheetos $1.25
Sprite $2.50
Red_Bull $4.75
Ramen $3.15
Cold_Pizza $0.99

The names of the elements in your enum should be exactly as they appear here, and the price should be a parameter to the enum constructor. Your VendingItem enum should have the following fields, methods, and constructors:

VendingMachine.java

This class represents the vending machine itself and is the bulk of the assignment. It has the following fields, methods, and constructors:

public String toString() {
    StringBuilder s = new StringBuilder();
    s.append("----------------------------------------------------------"
        + "------------\n");
    s.append("                            VendaTron 9000                "
        + "            \n");
    for (int i = 0; i < shelf.length; i++) {
        s.append("------------------------------------------------------"
            + "----------------\n");
        for (int j = 0; j < shelf[0].length; j++) {
            VendingItem item = shelf[i][j][0];
            String str = String.format("| %-20s ",
                (item == null ? "(empty)" : item.name()));
            s.append(str);
        }
        s.append("|\n");
    }
    s.append("----------------------------------------------------------"
        + "------------\n");
    s.append(String.format("There are %d items with a total "
        + "value of $%.2f.%n", getNumberOfItems(), getTotalValue()));
    s.append(String.format("Total sales across vending machines "
        + "is now: $%.2f.%n", getTotalSales()));
    return s.toString();
}

Running and Testing

VendingWorld.java has been provided for you. It creates several instances of VendingMachine, and allows the user to interact with them. You can run the main method to start a simulation, and test from there. The tester may not cover all cases, so be sure to write your own code to test your simulation.

Javadocs

Checkstyle

As mentioned in the previous homework, you will be running a style checking program on your code. For each violation the tool finds, you will lose one point on your total grade for this assignment.

To make things easier for you in the beginning of the semester, the first few homeworks will have a checkstyle cap, or a maximum amount of points that can be lost to checkstyle. For this homework, the checkstyle cap is 25, meaning you can lose up to 25 points on this assignment due to style errors. As the semester goes on, this cap will increase with each homework and eventually go away. Run checkstyle early, and get in the habit of writing style compliant code the first time. Don’t wait until 5 minutes before the deadline to find out that you have 100+ violations.

Hints and Tips

As part of your extensive training, you have been given the secret formula for Coke for success at CS 1331 (shhh). If you’ve read this far, you’re well on your way. If anything seems confusing, read through the entire description and instructions again. As always, feel free to contact your TAs, post on Piazza, or come in for office hours. In addition, here are some tips specific to this homework:

int i = 'a' - 32;
System.out.println(i);
System.out.println((char) i);

// ...outputs...
65
A

This ascii table gives you the numeric values corresponding to each char. You can use this to very easily convert between user input and array indices for the vending machine.

Collaboration

When completing homeworks for CS1331 you may talk with other students about:

You may not discuss, show, or share by other means the specifics of your code, including screenshots, file sharing, or showing someone else the code on your computer, or use code shared by others.

Examples of approved/disapproved collaboration:

In addition to the above rules, note that it is not allowed to upload your code to any sort of public repository. This could be considered an Honor Code violation, even if it is after the homework is due.

Submitting

You should not import any libraries or packages that trivialize the assignment. This includes data structures other than arrays (so no List, Map, Set, etc). If you are unsure of whether something is allowed, ask on Piazza. In general, if something does a large part of the assignment for you, it is probably not allowed. Important: java.util.Arrays is not allowed. However, that is different from a Java array (e.g int[] nums = new int[10]), which is necessary for this assignment. Be sure to check Piazza for clarifications and updates before submitting.