1.3-Object-Georienteerd-Programmeren

Candy machine

In this assignment we model a candy machine. Each candy machine consists of compartments. Each compartment contains several copies of a product (e.g. 8x a Mars bar). The candy machine contains two types of products: soda cans and candy. The name and price of each product must be stored. In addition, we need to store the contents of the soda cans in millilitres, the amount of sugar in grams and whether it contains caffeine. For sweets, we also need to store the weight in grams.

The candy machine has 20 compartments. Identifiable by an index (ranging from 0 up to 19). When you want to buy a product from the machine, you have to enter the index of the compartment.

The machine has the following functionality:

In addition, the machine must have good error handling and a clear printReport() method (see example), which prints the contents of the machine. Implement the class candy dispenser and write the necessary classes to store the products and the subject content.

Note that you do not have to implement any interactions with the machine. We have provided you with the code that generated the output below. Also, we have created a TestApplication that you can use to test your application. (Note that this version is a bit different to previous versions!)

Example

This code:

public static void main(String[] args) {
    CandyMachine machine = new CandyMachine();

    // Add drinks
    machine.addProduct(0, new Drink("Coca Cola", 200, 300, 20, false));
    machine.addProduct(1, new Drink("RedBull", 250, 300, 100, true));

    // Add snacks
    machine.addProduct(2, new Candy("M&M's", 150, 230));
    machine.addProduct(3, new Candy("Mars", 100, 230));
    machine.addProduct(4, new Candy("Twix", 120, 230));
    machine.addProduct(5, new Candy("Lion", 125, 230));
    machine.addProduct(6, new Candy("Snickers", 150, 230));

    for (int i = 0; i < 3; i++) {
        machine.takeItem(0);
        machine.takeItem(1);
    }

    machine.printReport();
}

produced this output:

CandyMachine status: 

Compartment 0: Coca Cola (€2.0) [2 items remaining]
Compartment 1: RedBull (€2.5) [2 items remaining]
Compartment 2: M&M's (€1.5) [5 items remaining]
Compartment 3: Mars (€1.0) [5 items remaining]
Compartment 4: Twix (€1.2) [5 items remaining]
Compartment 5: Lion (€1.25) [5 items remaining]
Compartment 6: Snickers (€1.5) [5 items remaining]
Compartment 7: Not in use
Compartment 8: Not in use
Compartment 9: Not in use
Compartment 10: Not in use
Compartment 11: Not in use
Compartment 12: Not in use
Compartment 13: Not in use
Compartment 14: Not in use
Compartment 15: Not in use
Compartment 16: Not in use
Compartment 17: Not in use
Compartment 18: Not in use
Compartment 19: Not in use

Total profit: €13,50