返回java中的列表< enum>

8zzbczxx  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(360)

我有以下枚举类:public enum banknote{

FIVE_JOD(new BigDecimal("5.0")), 
TEN_JOD(new BigDecimal("10.0")),
TWENTY_JOD(new BigDecimal("20.0")),
FIFTY_JOD(new BigDecimal("50.0"));

private final BigDecimal value; 

//constructor
Banknote(BigDecimal value) {
    this.value = value;
}

public BigDecimal getValue() {
    return value;
}

}
如何在此atm类方法中返回钞票列表:
公共类atm\u实现实现atm{

@Override
public List<Banknote> withdraw(String accountNumber, BigDecimal amount) {

    int accountNumberIntegerInput = Integer.parseInt(accountNumber);
    System.out.println("Enter amount ");

    BankingSystemImplementation object = new BankingSystemImplementation();

    BigDecimal balanceInSystem = object.getAccountBalance(accountNumber);
    int equals = balanceInSystem.compareTo(amount); //compare between two bigDecimal objects 

    if ((equals == 0 || equals == 1)) {
        object.debitAccount(accountNumber, amount); //subtact two bigdacimal objects
    }
    else if (equals==-1){
    throw new InsufficientFundsException();

            }
     return null;

}

tyu7yeag

tyu7yeag1#

你应该把取款金额分成钞票(如果我理解你的问题的话)。它幼稚的实现。
例如:金额=100;结果:[五十个工作日,五十个工作日]
例:金额=87;结果:[五十工,二十工,十工,五工,一工,一工]
实现(元代码):

List<Banknotes> banknotes = new List<Banknotes>;

BigDecimal remains = amount;
int count50;
int count20;
int count10;
int count5;
int count1;

// Evaluate each nominal count

if(remains >= 50)
{
   count50 = (int)(remains/50);
   remains = remains - (count50 * 50);
}

if(remains >= 20)
{
   count20 = (int)(remains/20);
   remains = remains - (count20 * 50);
}

if(remains >= 10)
{
   count10 = (int)(remains/10);
   remains = remains - (count10 * 10);
}

if(remains >= 5)
{
   count5 = (int)(remains/5);
   remains = remains - (count5 * 5);
}

if(remains >= 1)
{
   count5 = (int)(remains/1);
   remains = remains - (count1 * 1);
}

// Fill banknotes list

for(int i = 0; i < count50; i++)
    banknotes.Add(FIFTY_JOD)

for(int i = 0; i < count20; i++)
    banknotes.Add(TWENTY_JOD)

for(int i = 0; i < count10; i++)
    banknotes.Add(TEN_JOD)

for(int i = 0; i < count5; i++)
    banknotes.Add(FIVE_JOD)

for(int i = 0; i < count1; i++)
    banknotes.Add(ONE_JOD)

return banknotes;

相关问题