Use Case
select item
insert coin
execute transaction
cancel transaction
refill items
Class
class VendingMachine{
List<Item> items = Arrays.asList(...);
Map<Integer, ItemInfo> itemIdentifies = new HashMap<>();
ItemInfo currentItemInfo;
List<Coin> savings = Arrays.asList(...);
List<Coin> currentCoins = new ArrayList<>();
Map<ItemInfo, List<Item>> stocks;
void selectItem(int id){
currentItemInfo = itemIdentifies.get(id);
}
void insertCoins(List<Coins> coins){
if(coins==null){return;}
currentCoins = coins;
}
Item executeTrnsctn(){
// compare price
int sum = 0;
for(Coin x : currentCoins){
sum += x.getValue();
}
// if no sufficient, throw exception
if(sum<currentItemInfo.getPrice()){
throw new NoSufficientMoneyException();
}
// if sufficient, return back;
Item ans = ;
if(sum>price){
makeRefund();
}
}
List<Coin> cancelTrnsctn(){}
void refill(List<Item>){
}
private List<Coin> refund(){
}
}
class Item{
Iteminfo iteminfo;
}
Enum ItemInfoEnum{
CokeInfo(2.5);
GingerInfo(3.5);
private double price;
ItemInfoEnum(double price){
this.price = price;
}
public double getPrice(){
return this.price;
}
}
近期评论