自动柜员机-分配的钞票数量有限,没有正确分配



我正在开发一个spring应用程序,基本上是ATM机的基本功能,试图从ATM机中提取钞票,每次提取应该分配最少数量的钞票。自动取款机每个面额的钞票数量有限,我使用mysql后端来处理账户和出纳员的东西,比如钞票和现金总量等,创建了一个INoteBucket接口

interface INoteBucket
{
public int GetDemon();
public boolean TakeNotes(int qtyofnotes);
public boolean CheckTakeNote();
}
class NoteBucket implements INoteBucket
{
private int Denominator;
private int AmountOfNotes;

public NoteBucket(int denom, int notes){
this.Denominator = denom;
this.AmountOfNotes = notes;
}
public boolean CheckTakeNote(){
if(this.AmountOfNotes > 0)
return true;

return false;

}
public int GetDemon(){
return this.Denominator;
}
public boolean TakeNotes(int qtyofnotes){
if (this.AmountOfNotes >= qtyofnotes)
{
this.AmountOfNotes -= qtyofnotes;
return true;
}
return false;
}
}

我也有一个"dispense"函数(可能是一个糟糕的名字),它意味着实现接口(它确实),但是当我"撤回"时;这个函数似乎让我提取超过我实际拥有的纸币数量,也就是说,如果我在ATM机中存储了2个50,10个20,10个10和5个5,并试图提取200,结果是从ATM机中提取了4个50,数据库中的后端值被存储为-2 50,不知道为什么?看起来正确吗?

public String dispense(@RequestBody Teller teller, @PathVariable Double amount) {
String notes = "";
ArrayList<INoteBucket> NoteBuckets = new ArrayList<INoteBucket>();
NoteBuckets.add(new NoteBucket(50,teller.getFifty()));
NoteBuckets.add(new NoteBucket(20,teller.getTwenty()));
NoteBuckets.add(new NoteBucket(10,teller.getTen()));
NoteBuckets.add(new NoteBucket(5,teller.getFive()));
while(amount > 0)
{
for(INoteBucket noteBucket : NoteBuckets)
{
if(amount >= noteBucket.GetDemon() && noteBucket.CheckTakeNote())
{
//We know that the remaining request amount is greater than the current denominator && we're allowed to take a note
double remainder = amount % noteBucket.GetDemon();
if(remainder < amount)
{
notes += "Amount of: " + Integer.toString(noteBucket.GetDemon()) + " : " + Double.toString((amount - remainder) / noteBucket.GetDemon()) + "n";

if(noteBucket.GetDemon() == 50) {
int updatefifty = (int) (teller.getFifty() - ((amount - remainder) / noteBucket.GetDemon()));
teller.setFifty(updatefifty);
tellerService.updateTeller(teller);
}

if(noteBucket.GetDemon() == 20) {
int updatetwenty = (int) (teller.getTwenty() - ((amount - remainder) / noteBucket.GetDemon()));
teller.setTwenty(updatetwenty);
tellerService.updateTeller(teller);
}

if(noteBucket.GetDemon() == 10) {
int updateten = (int) (teller.getTen() - ((amount - remainder) / noteBucket.GetDemon()));
teller.setTen(updateten);
tellerService.updateTeller(teller);
}

if(noteBucket.GetDemon() == 5) {
int updatefive = (int) (teller.getFive() - ((amount - remainder) / noteBucket.GetDemon()));
teller.setFive(updatefive);
tellerService.updateTeller(teller);
}

amount = remainder;
}

}

}

}
return notes;
}

下面是一个简化的基本java应用程序,具有相同的实现,具有相同的问题

import java.util.ArrayList;
import java.lang.*;
import java.io.*;
public class MyClass {
public static void main(String args[]) {
ArrayList<INoteBucket> NoteBuckets = new ArrayList<INoteBucket>();
//Add Notes
NoteBuckets.add(new NoteBucket(50,2));
NoteBuckets.add(new NoteBucket(20,15));
NoteBuckets.add(new NoteBucket(10,20));
NoteBuckets.add(new NoteBucket(5,10));

int RequestAmount = 200;

while(RequestAmount >= 0)
{
for(INoteBucket noteBucket : NoteBuckets)
{
if(RequestAmount >= noteBucket.GetDemon() && noteBucket.CheckTakeNote())
{
//We know that the remaining request amount is greater than the current demoninator && we're allowed to take a note
int remainder = RequestAmount % noteBucket.GetDemon();
if(remainder < RequestAmount)
{
System.out.println("Amount of: " + Integer.toString(noteBucket.GetDemon()) + " : " + Integer.toString((RequestAmount - remainder) / noteBucket.GetDemon()));
RequestAmount = remainder;
}

}

}

}

}
}
interface INoteBucket
{
public int GetDemon();
public boolean TakeNotes(int qtyofnotes);
public boolean CheckTakeNote();
}
class NoteBucket implements INoteBucket
{
private int Denominator;
private int AmountOfNotes;

public NoteBucket(int denom, int notes){
this.Denominator = denom;
this.AmountOfNotes = notes;
}

public boolean CheckTakeNote(){
if(this.AmountOfNotes > 0)
return true;

return false;

}

public int GetDemon(){
return this.Denominator;
}

public boolean TakeNotes(int qtyofnotes){
if (this.AmountOfNotes >= qtyofnotes)
{
this.AmountOfNotes -= qtyofnotes;
return true;
}
return false;
}

}

修复了它使用的基本实现,应该是这个

import java.util.ArrayList;
import java.lang.*;
import java.io.*;
public class MyClass {
public static void main(String args[]) {
ArrayList<INoteBucket> NoteBuckets = new ArrayList<INoteBucket>();
//Add Notes
NoteBuckets.add(new NoteBucket(50,2));
NoteBuckets.add(new NoteBucket(20,15));
NoteBuckets.add(new NoteBucket(10,20));
NoteBuckets.add(new NoteBucket(5,10));
int remainder = 0;
int RequestAmount = 200;
while(RequestAmount > 0)
{
for(INoteBucket noteBucket : NoteBuckets)
{
int counter = 0;
while(remainder >= 0 && noteBucket.CheckTakeNote()){
remainder = RequestAmount - noteBucket.GetDemon();
noteBucket.TakeNotes(1);
counter += 1;
RequestAmount = remainder;
if(RequestAmount == 0)
break;
}
System.out.println("Note: " + noteBucket.GetDemon() + " Count: " + counter);
if(RequestAmount == 0)
break;
}
}
}
}



interface INoteBucket
{
public int GetDemon();
public int GetNotes();
public boolean TakeNotes(int qtyofnotes);
public boolean CheckTakeNote();
}
class NoteBucket implements INoteBucket
{
private int Denominator;
private int AmountOfNotes;

public NoteBucket(int denom, int notes){
this.Denominator = denom;
this.AmountOfNotes = notes;
}

public boolean CheckTakeNote(){
if(this.AmountOfNotes > 0)
return true;

return false;

}

public int GetDemon(){
return this.Denominator;
}

public int GetNotes(){
return this.AmountOfNotes;
}

public boolean TakeNotes(int qtyofnotes){
if (this.AmountOfNotes >= qtyofnotes)
{
this.AmountOfNotes -= qtyofnotes;
return true;
}
return false;
}

}

最新更新