转换资金

时间:2016-05-26 18:06:08

标签: java algorithm hashmap

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;
        }
    }
}

}

无法正确调用方法 底部checkAndGiveMoney实现的方法应该显示必要的账单数量,如300 = 100:3告诉我如何适应案例3?平衡以纸币数量及其面值显示

1 个答案:

答案 0 :(得分:0)

您无法获得案例3的输出100:3的直接问题是因为您没有分配足够的100来撤销300,您只是已保留200 line 10: (private int AMOUNT_OF_100 = 2;)当然,将其更改为private int AMOUNT_OF_100 = 3;还不足以让程序在100之前选择20的纸币,因为Map.Entry<Integer, Integer> entry的数据结构{1}}是无序的,因为HashMap是无序/未排序的。因此,为了显示100:3,订单需要将100资金分拣为高于20,因此使用LinkedList可以在选择哪个值之前对资金进行排序在输出中使用的钞票。

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;
    } 
}

希望这有帮助! Наилучшиепожелания