-
大小: 5.25MB文件類(lèi)型: .zip金幣: 2下載: 0 次發(fā)布日期: 2023-09-18
- 語(yǔ)言: Java
- 標(biāo)簽:
資源簡(jiǎn)介
這個(gè)是我做的一個(gè)簡(jiǎn)單的自動(dòng)售貨機(jī),比較符合java規(guī)范。加上了Junit測(cè)試用例。個(gè)人覺(jué)得還不錯(cuò)。

代碼片段和文件信息
package?automat;
import?java.util.HashMap;
/**
?*?自動(dòng)售貨機(jī)程序:
?*? 工作流程:
?*? 顧客投入硬幣購(gòu)買(mǎi)飲料:啤酒和橙汁,售價(jià)都是5毛一個(gè),機(jī)內(nèi)預(yù)存兩種飲料各3個(gè),
?*? 機(jī)內(nèi)存硬幣5毛的5個(gè),1元的3個(gè),要求完成一下功能:
?*? 1。 如顧客投入5毛,則可以選擇飲料,如果選擇是啤酒和橙汁,且有存貨,顯示“請(qǐng)取回**飲料”字樣;
?*? 否則如果沒(méi)有存貨或者選擇飲料類(lèi)型超出上面兩種,顯示錯(cuò)誤信息,退出5毛給用戶;;
?*? 2。 如果用戶投入超過(guò)5毛的金額,則要考慮是否要找錢(qián),只要在飲料種類(lèi)正確、庫(kù)存足夠、金額足夠、
?*? 能夠找錢(qián)的情況下才給出飲料,否則顯示錯(cuò)誤信息;
?*?
?*?@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(“--------------------------“);
}
}
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????目錄???????????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個(gè)文件信息
評(píng)論
共有 條評(píng)論