資源簡介
這個是我做的一個簡單的自動售貨機,比較符合java規范。加上了Junit測試用例。個人覺得還不錯。

代碼片段和文件信息
package?automat;
import?java.util.HashMap;
/**
?*?自動售貨機程序:
?*? 工作流程:
?*? 顧客投入硬幣購買飲料:啤酒和橙汁,售價都是5毛一個,機內預存兩種飲料各3個,
?*? 機內存硬幣5毛的5個,1元的3個,要求完成一下功能:
?*? 1。 如顧客投入5毛,則可以選擇飲料,如果選擇是啤酒和橙汁,且有存貨,顯示“請取回**飲料”字樣;
?*? 否則如果沒有存貨或者選擇飲料類型超出上面兩種,顯示錯誤信息,退出5毛給用戶;;
?*? 2。 如果用戶投入超過5毛的金額,則要考慮是否要找錢,只要在飲料種類正確、庫存足夠、金額足夠、
?*? 能夠找錢的情況下才給出飲料,否則顯示錯誤信息;
?*?
?*?@author?decoer
?*
?*/
public?class?SaleMachine?{
//number?of?50?coin
private?int?num50? =?5;
//number?of?100?coin
private?int?num100? =?3;
//type?of?juices
private?HashMap?types;
//constructor
public?SaleMachine(){
//initialization
types?=?new?HashMap(2);
types.put(“beer“?Integer.valueOf(3));
types.put(“orange“?Integer.valueOf(3));
}
/**
?*?buy?an?item?with?given?type?and?money
?*?@param?type
?*?@param?money
?*?@return?the?message?text
?*/
public?String?buy(String?type?int?money){
//whether?a?valid?money?number
if(!(money==50?||?money==100))
return?SaleMachineMessage.INVALID_MONEY;
//whether?an?allowed?type
if(!types.containsKey(type))
return?SaleMachineMessage.NOT_PROVIDED;
//still?available
Integer?num?=?types.get(type);
if(num.intValue()==0)
return?SaleMachineMessage.ALREADY_SOLD;
//whether?to?give?change
boolean?change?=?false;
if(money>50){
if(num50==0)
return?SaleMachineMessage.NO_CHANGE;
num50?--;
num100?++;
change?=?true;
}else
num50?++;
//ok?give?a?juice?out?and?decrease?the?number?by?1
num?--;
types.put(type?num);
return?SaleMachineMessage.PICK_UP?+?((change)?SaleMachineMessage.PICK_CHANGE:““);?
}
/**
?*?print?current?states
?*/
public?void?printInfo(){
System.out.println(“--------------------------“);
System.out.println(“Juice?:?“);
for(String?key?:?types.keySet()){
System.out.println(“\t“?+?key?+?“\t=\t“?+?types.get(key));
}
System.out.println(“50?coin?:?\t“?+?num50);
System.out.println(“100?coin?:?\t“?+?num100);
System.out.println(“Total?money?:?\t“?+?Integer.toString(num50*50?+?num100*100));
System.out.println(“--------------------------“);
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2011-01-23?20:53??ST.emma.automat\
?????文件?????????696??2011-01-24?21:01??ST.emma.automat\.classpath
?????文件?????????391??2011-01-23?08:04??ST.emma.automat\.project
?????目錄???????????0??2011-01-23?08:04??ST.emma.automat\.settings\
?????文件?????????629??2011-01-23?08:04??ST.emma.automat\.settings\org.eclipse.jdt.core.prefs
?????文件???????10341??2011-01-23?20:50??ST.emma.automat\beer.jpeg
?????目錄???????????0??2011-01-24?21:01??ST.emma.automat\bin\
?????目錄???????????0??2011-01-24?21:01??ST.emma.automat\bin\automat\
?????文件????????2698??2011-01-24?21:01??ST.emma.automat\bin\automat\SaleMachine.class
?????文件?????????827??2011-01-24?21:01??ST.emma.automat\bin\automat\SaleMachineMessage.class
?????文件????????1847??2011-01-25?06:16??ST.emma.automat\bin\automat\SaleMachineTest.class
?????目錄???????????0??2011-01-24?21:01??ST.emma.automat\bin\gui\
?????文件????????1403??2011-01-24?21:01??ST.emma.automat\bin\gui\SaleMachineGUI$1.class
?????文件????????1405??2011-01-24?21:01??ST.emma.automat\bin\gui\SaleMachineGUI$2.class
?????文件????????3180??2011-01-24?21:01??ST.emma.automat\bin\gui\SaleMachineGUI.class
?????文件??????107210??2011-01-23?20:30??ST.emma.automat\bin\org.eclipse.core.commands_3.6.0.I20100512-1500.jar
?????文件??????101957??2011-01-23?20:29??ST.emma.automat\bin\org.eclipse.equinox.common_3.6.0.v20100503.jar
?????文件?????1068738??2011-01-23?20:16??ST.emma.automat\bin\org.eclipse.jface_3.6.0.I20100601-0800.jar
?????文件?????1738337??2011-01-23?20:15??ST.emma.automat\bin\swt.jar
?????文件????????4817??2011-01-23?20:57??ST.emma.automat\orange.jpeg
?????目錄???????????0??2011-01-23?21:05??ST.emma.automat\src\
?????目錄???????????0??2011-01-23?21:05??ST.emma.automat\src\automat\
?????文件????????2406??2011-01-24?15:31??ST.emma.automat\src\automat\SaleMachine.java
?????文件?????????512??2011-01-23?21:05??ST.emma.automat\src\automat\SaleMachineMessage.java
?????文件????????1906??2011-01-25?06:16??ST.emma.automat\src\automat\SaleMachineTest.java
?????目錄???????????0??2011-01-23?21:06??ST.emma.automat\src\gui\
?????文件????????2808??2011-01-23?21:05??ST.emma.automat\src\gui\SaleMachineGUI.java
?????文件??????107210??2011-01-23?20:30??ST.emma.automat\src\org.eclipse.core.commands_3.6.0.I20100512-1500.jar
?????文件??????101957??2011-01-23?20:29??ST.emma.automat\src\org.eclipse.equinox.common_3.6.0.v20100503.jar
?????文件?????1068738??2011-01-23?20:16??ST.emma.automat\src\org.eclipse.jface_3.6.0.I20100601-0800.jar
?????文件?????1738337??2011-01-23?20:15??ST.emma.automat\src\swt.jar
............此處省略0個文件信息
評論
共有 條評論