Професионална програма
Loading...
VasilK avatar VasilK 39 Точки

Привет,

Реших да използвам Hashtable за решението. Скъса ми нервите докато разбера как се обхожда таблица.

Чудя се има ли по лесен начин да се създаде обект с key и value, който да се обхожда чрез индекс.

import java.util.*;

public class Vending_Machine {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String str;
        double money = 0.0;
       
        Dictionary <String, Double> dic = new Hashtable<String, Double>();

        dic.put("Nuts", 2.0);
        dic.put("Water", 0.7);
        dic.put("Crisps", 1.5);
        dic.put("Soda", 0.8);
        dic.put("Coke", 1.0);


        while (true) {

            String input = scanner.nextLine();
            if (input.equals("Start")) break;
            double in = Double.parseDouble(input);
            if (in != 0.1 && in != 0.2 && in != 0.5 && in != 1.0 && in != 2.0) System.out.printf("Cannot accept %.2f \n", in);
            else money+= in;
        }

        while (true) {

            Enumeration enu = dic.keys();

            String input = scanner.nextLine();

            if (input.equals("End")) break;

            if (((Hashtable<String, Double>) dic).containsKey( input ) == false) System.out.println("Invalid product");

            while (enu.hasMoreElements()) {
                str = (String) enu.nextElement();
                if (str.equals(input) && money - dic.get(str) >= 0) {
                    money -= dic.get( str );
                    System.out.printf( "Purchased %s \n", input );
                } else if (str.equals(input) && money - dic.get(str)  < 0) {
                    System.out.println("Sorry, not enough money");
                }

            }

        }

        System.out.printf("Change: %.2f", money);

    }

}

0
27/01/2019 22:50:07
markopizzy avatar markopizzy 0 Точки

Заимствах от колежката с флаговете по-горе и взе, че ми се получи и на мен...

 

import java.util.Scanner;

public class JF_7_Vending_Machine_1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String command = scanner.nextLine();
        double sum = 0;

        while (!"Start".equals(command)) {
            double coins = Double.parseDouble(command);
            if (coins == 0.1) {
                sum += coins;
            } else if (coins == 0.2) {
                sum += coins;
            } else if (coins == 0.5) {
                sum += coins;
            } else if (coins == 1) {
                sum += coins;
            } else if (coins == 2) {
                sum += coins;
            } else {
                System.out.println(String.format("Cannot accept %.2f", coins));
            }
            command = scanner.nextLine();
        }
        String good = scanner.nextLine();
        double price = 0;
        boolean isGood = false;
        while (!"End".equals(good)) {
            switch (good) {
                case "Nuts":
                    price = 2.0;
                    isGood = true;
                    break;
                case "Water":
                    price = 0.7;
                    isGood = true;
                    break;
                case "Crisps":
                    price = 1.5;
                    isGood = true;
                    break;
                case "Soda":
                    price = 0.8;
                    isGood = true;
                    break;
                case "Coke":
                    price = 1.0;
                    isGood = true;
                    break;
            }
            if (sum < price) {
                System.out.println("Sorry, not enough money");
            } else if (isGood) {
                System.out.println(String.format("Purchased %s", good));
                sum -= price;
            } else {
                System.out.println("Invalid product");
            }
            good = scanner.nextLine();
        }
        System.out.println(String.format("Change: %.2f", sum));

    }
}

0