mardi 30 juin 2020

Convert code to use more OOP design patterns

I have to solve a problem of a vending machine which:

  1. Accepts coins of 0.1, 0.2, 0.5, 1, 2 euros.
  2. Allow user to select products Water(0.5euro), Coke(1euro), Pepsi(1.5euro), Soda(2.5euro)
  3. Allow user to take refund by canceling the request.
  4. Return selected product and remaining change(least amount of coins) if any
  5. Allow reset operation for vending machine supplier(initial state of machine: no coins inserted and no product selected).

I tried to implement this program by creating two classes. The first one is:

public enum Product {
    WATER("Water", 0.5), COKE("Coke", 1.0), PEPSI("Pepsi", 1.5), SODA("Soda", 2.5);
    
    private String name;
    private double price;
    
    //Constructor
    private Product(String name, double price) {
        this.name= name;
        this.price = price;
    }
    
    //Getter methods
    public String getName() {
        return name;
    }
    
    public double getPrice() {
        return price;
    }
}

and the second is the VendingMachine.java where i have implemented all the methods but i have problem with the program structure that i have made. I cant compose all the functions to create what the program wants. Here is the VendingMachine.java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class VendingMachine {
    private double totalMachineCoins;
    private double addedCoins;
    private Product selectedProduct;
    
    
    private void printingMenu(){
        System.out.println("1-> Water(0.5€)\n" + "2-> Coke 330 ml(1€)" +
                           "3-> Coke 500 ml(1.5€)\n" + "4-> Crunch(2.5€)");
    }
    
    
    private void selectProduct() {
        printingMenu();
        Scanner scanner = new Scanner(System.in);
        System.out.println("Select Product: ");
        int choice = scanner.nextInt();
        while(choice < 1 && choice > 5) {
            System.out.println("This product code doesn't exist. Please select again!");
            choice = scanner.nextInt();
        }
        if(choice == 1) {
            selectedProduct = Product.WATER;
        }
        else if(choice == 2) {
            selectedProduct = Product.COKE330;
        }
        else if(choice == 3) {
            selectedProduct = Product.COKE500;
        }
        else {
            selectedProduct = Product.CRUNCH;
        }
    }
    
    
    private boolean validCoin(double coin) {
        if(coin == 0.1 | coin == 0.2 | coin == 0.5 | coin == 1.0 | coin == 2.0) {
            return true;
        }
        return false;
    }
    
    
    private void insertCoins() {
        addedCoins = 0;
        while(addedCoins < selectedProduct.getPrice()) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Insert coin: ");
            double coin = scanner.nextDouble();
            while(validCoin(coin) == false) {
                System.out.println("The coin is not acceptable! Please put another");
                coin = scanner.nextDouble();
            }
            addedCoins += coin;
            returnRemainingChange(selectedProduct.getPrice());
        }
        
        totalMachineCoins += addedCoins;
    }
    
    
    private void returnRemainingChange(double productPrice) {
        if(addedCoins > productPrice) {
            double change = addedCoins - productPrice;
            int coin2 = (int) (change / 2.0);
            change = change % 2.0;
            int coin1 = (int) (change / 1.0);
            change = change % 1.0;
            int coin05 = (int) (change / 0.5);
            change = change % 0.5;
            int coin02 = (int) (change / 0.2);
            change = change % 0.2;  
            int coin01 = (int) (change / 0.1);
            System.out.println("Change: " + change);
            System.out.println("You took back: \n" +
                                coin2 + " coins of 2€\n" +
                                coin1 + " coins of 1€\n" +
                                coin05 + " coins of 0.5€\n" +
                                coin02 + " coins of 0.2€\n" +
                                coin01 + " coins of 0.1€");
        }
        else {
            System.out.println("There is no remaining change");
        }
    }
    
    
    //take refund by cancelling the request
    private void takeRefund() {
        System.out.println("Cancelling the request....");
        System.out.println("Take refund: " + addedCoins + "€");
        addedCoins = 0;
    }
    
    
    private void resetMachine() {
        totalMachineCoins = 0;
        addedCoins = 0;
    }
}

Can enyone help me to finalize this exercise?(use the functions to make the program run correctly and use a java pattern to make the code better). Thank you very much for your time

Aucun commentaire:

Enregistrer un commentaire