-1
public class Terminal {
private Map<Integer, Integer> money= new HashMap<>();
private int balance;
private Scanner in;
private int AMOUNT_OF_5 = 500;
private int AMOUNT_OF_10 = 100;
private int AMOUNT_OF_15 = 60;
private int AMOUNT_OF_20 = 20;
private int AMOUNT_OF_50 = 4;
private int AMOUNT_OF_100 = 2;
public Terminal() {
    money.put(5, AMOUNT_OF_5 * 5);
    money.put(10, AMOUNT_OF_10 * 10);
    money.put(15, AMOUNT_OF_15 * 15);
    money.put(20, AMOUNT_OF_20 * 20);
    money.put(50, AMOUNT_OF_50 * 50);
    money.put(100, AMOUNT_OF_100 * 100);
    balance = 5000;


    in = new Scanner(System.in);
    while (true) {
        System.out.println("Банкомат");
        System.out.println("1.Пополнить счёт");
        System.out.println("2.Выдача наличных");
        System.out.println("3.Остаток на счёте");
        System.out.println("4.Выход");
        System.out.print("Выберите нужную Вам операцию:");
        int n = in.nextInt();
        switch (n) {

            case 1:
                System.out.print("Внесите сумму для пополнения:");
                int deposit = in.nextInt();
                while ((deposit % 10) != 0){
                    System.out.println("Неверная сумма");
                    deposit = in.nextInt();
                } if (balance >= deposit) {
                balance = balance + deposit;
                System.out.println("Ваш баланс был успешно пополнен");
            }else {
                System.out.println("Ваш депозит превышает лимит");
            }
                System.out.println("");
                break;



            case 2:
                System.out.print("Введите сумму для снятия:");
                int withdraw = in.nextInt();
                while ((withdraw % 10) != 0){
                    System.out.println("Неверная сумма");
                    withdraw = in.nextInt();
                }
                if (balance >= withdraw) {
                    balance = balance - withdraw;
                    System.out.println("Введите сумму для снятия");
                } else {
                    System.out.println("Недостаточный баланс");
                }
                System.out.println("");
                break;


            case 3:
                checkAndGiveMoney();
                System.out.println("Balance : " + balance);
                System.out.println("");
                break;

            case 4:
                System.exit(0);
        }
    }
}
public static void main (String[] args) throws java.lang.Exception
{
    Terminal terminal = new Terminal();
    terminal.checkAndGiveMoney();
}

public void checkAndGiveMoney(){

    int requiredMoney = in.nextInt();


    for (final Map.Entry<Integer, Integer> entry : money.entrySet()) {
        int value = entry.getValue();
        int key = entry.getKey();
        if (value >= requiredMoney && ((value - requiredMoney) % key == 0)){
            entry.setValue(value - requiredMoney);
            balance = balance - requiredMoney;
            System.out.println(entry.getKey() + ":" + requiredMoney / key);
            break;
        }
    }
}

}

it is impossible to call a method right bottom checkAndGiveMoney implemented method that should display the number of the necessary bills such as 300 = 100: 3 tell me how I fit it in case 3? Balance to show up in the form of number of banknotes and their face value

Morozov
  • 3,411
  • 3
  • 25
  • 48
  • 1
    I think we have the issue of not understanding what your problem is. Can you be more clear? If I had to guess, [this](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) is of help. – RaminS May 26 '16 at 18:07
  • Also, please translate the inside Strings of your program in english and not in cyrillic – T. Claverie May 26 '16 at 18:11
  • 1
    Perhaps [Stack overflow in Russian](http://ru.stackoverflow.com/) would work better for you? – RealSkeptic May 26 '16 at 18:23

1 Answers1

0

The immediate problem you're not able to get the output 100:3 for case 3 is because you've not assigned enough 100's to withdraw 300, you've only deposited 200 line 10: (private int AMOUNT_OF_100 = 2;) Of course changing it to private int AMOUNT_OF_100 = 3; is not enough for the program to select 100s banknote before 20's because the data structure of Map.Entry<Integer, Integer> entry, is unordered as a HashMap is unordered/unsorted. So in order for 100:3 to display, the order needs to have 100 money be sortered higher than 20, hence using a LinkedList enables sorting the money before choosing which value of banknote to use in the output.

import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Comparator;

public class Terminal {
    private Map<Integer, Integer> money= new HashMap<Integer, Integer>();
    private int balance;
    private Scanner in;
    private int AMOUNT_OF_5 = 500;
    private int AMOUNT_OF_10 = 100;
    private int AMOUNT_OF_15 = 60;
    private int AMOUNT_OF_20 = 20;
    private int AMOUNT_OF_50 = 4;
    private int AMOUNT_OF_100 = 3;
    public Terminal() {
        money.put(5, AMOUNT_OF_5 * 5);
        money.put(10, AMOUNT_OF_10 * 10);
        money.put(15, AMOUNT_OF_15 * 15);
        money.put(20, AMOUNT_OF_20 * 20);
        money.put(50, AMOUNT_OF_50 * 50);
        money.put(100, AMOUNT_OF_100 * 100);
        balance = 5000;


        in = new Scanner(System.in);
        while (true) {
            System.out.println("Банкомат");
            System.out.println("1.Пополнить счёт");
            System.out.println("2.Выдача наличных");
            System.out.println("3.Остаток на счёте");
            System.out.println("4.Выход");
            System.out.print("Выберите нужную Вам операцию:");
            int n = in.nextInt();
            switch (n) {

            case 1:
                System.out.print("Внесите сумму для пополнения:");
                int deposit = in.nextInt();
                while ((deposit % 10) != 0){
                    System.out.println("Неверная сумма");
                    deposit = in.nextInt();
                } if (balance >= deposit) {
                    balance = balance + deposit;
                    System.out.println("Ваш баланс был успешно пополнен");
                }else {
                    System.out.println("Ваш депозит превышает лимит");
                }
                System.out.println("");
                break;



            case 2:
                System.out.print("Введите сумму для снятия:");
                int withdraw = in.nextInt();
                while ((withdraw % 10) != 0){
                    System.out.println("Неверная сумма");
                    withdraw = in.nextInt();
                }
                if (balance >= withdraw) {
                    balance = balance - withdraw;
                    System.out.println("Введите сумму для снятия");
                } else {
                    System.out.println("Недостаточный баланс");
                }
                System.out.println("");
                break;


            case 3:
                checkAndGiveMoney();
                System.out.println("Balance : " + balance);
                System.out.println("");
                break;

            case 4:
                System.exit(0);
            }
        }
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        Terminal terminal = new Terminal();
        terminal.checkAndGiveMoney();
    }

    public void checkAndGiveMoney(){

        int requiredMoney = in.nextInt();

        //System.out.println(money.entrySet());
        Map sortedMoney = sortByValue(money);
        //System.out.println(sortedMoney);
        for (final Map.Entry<Integer, Integer> entry : money.entrySet()) {
            int value = entry.getValue();
            int key = entry.getKey();
            //System.out.println(key +","+ value);
            if (value >= requiredMoney && ((value - requiredMoney) % key == 0)){
                entry.setValue(value - requiredMoney);
                balance = balance - requiredMoney;
                System.out.println(entry.getKey() + ":" + requiredMoney / key);
                break;
            }       
        }
    }

    static Map sortByValue(Map map) {
        List list = new LinkedList(map.entrySet());

        Collections.sort(list, new Comparator() {
                public int compare(Object o1, Object o2) {
                    return ((Comparable) ((Map.Entry) (o2)).getValue())
                        .compareTo(((Map.Entry) (o1)).getValue());
                }
            });

        Map result = new LinkedHashMap();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry)it.next();
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    } 
}

Hope this helps! Наилучшие пожелания

davedwards
  • 7,011
  • 2
  • 15
  • 45