本文主要是介绍面试题:模拟自动售货机,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前两天面试,编程题是写一个模拟自动售货机的程序,当时时间匆忙,匆匆写了一下,回来就在机器上做了完善。记下来,下次或许能用到这些代码。
代码还需要优化,如果卖的饮料品牌有变化,就得改类了,不符合开闭原则,暂时还没想到什么好的方法,期待高手指点。
/*** 消费者* * @author ma_clin* */
public class Consumer implements ConsumeAction {private List<Drink> orders = new ArrayList<Drink>();private Commander commander;public Consumer(Commander commander) {this.commander = commander;}public void buyCola(int num) {while (num > 0) {orders.add(new Cola());num--;}}public void buyOrangeJuice(int num) {while (num > 0) {orders.add(new OrangeJuice());num--;}}public void buySprite(int num) {while (num > 0) {orders.add(new Sprite());num--;}}public String check(double payment) {System.out.println(commander.check(orders, payment));return "";}}
/*** 消费动作* @author ma_clin**/
public interface ConsumeAction {/*** * @param num 购买数量*/public void buyCola(int num);public void buySprite(int num);public void buyOrangeJuice(int num);}
/*** 命令* @author ma_clin**/
public class Commander{private VendingMachine machine;public Commander(VendingMachine machine){this.machine = machine;}public String check(List orders, double payment) {return machine.check(orders, payment);}}
/*** 自动售货机* * @author ma_clin* */
public class VendingMachine {// 自动售货机中的可乐private List<Drink> colaBox = new ArrayList<Drink>();// 雪碧private List<Drink> spriteBox = new ArrayList<Drink>();// 橙汁private List<Drink> orangeJuiceBox = new ArrayList<Drink>();// 价格管理器private PriceManager priceManager;// 本次交易应付款private double sumPrice = 0;// 本次交易结果private String result;// 最大存储数量private int maxCapacity = 50;/*** * @param number* 售货机中各款饮料的数量* @throws IOException*/public VendingMachine(int number) throws IOException {// 初始化价格管理器try {priceManager = new PriceManager();System.out.println("售货机价格设定完毕。");System.out.println("可乐,雪碧,橙汁: " + priceManager.getColaPrice() + ","+ priceManager.getSpritePrice() + ","+ priceManager.getOrangeJuicePrice());} catch (IOException e) {throw e;}// 入货if(number > maxCapacity){number = maxCapacity;}while (number > 0) {colaBox.add(new Cola());number--;spriteBox.add(new Sprite());number--;orangeJuiceBox.add(new OrangeJuice());number--;}System.out.println("售货机入货完毕。");System.out.println("当前数量: 可乐\t雪碧\t橙汁\n\t" + colaBox.size() + "\t"+ spriteBox.size() + "\t" + orangeJuiceBox.size());}/*** * @param payment* 付款金额* @return*/public String check(List orders, double payment) {Iterator itr = orders.iterator();Drink drink;// 算账while (itr.hasNext()) {drink = (Drink) itr.next();if (drink instanceof Cola) {sumPrice += priceManager.getColaPrice();} else if (drink instanceof Sprite) {sumPrice += priceManager.getSpritePrice();} else if (drink instanceof OrangeJuice) {sumPrice += priceManager.getOrangeJuicePrice();}}// 结帐if (payment >= sumPrice) {spitOutDrink(orders);result = "本次应付账款:" + sumPrice + "元,实收现金:" + payment + "元,找零:"+ (payment - sumPrice) + "元";} else {result = "对不起,现金不足。您本次需要支付" + sumPrice + "元";}// 本次交易结束,次交易金额重置为0sumPrice = 0;return result;}/*** 售货机吐出饮料* * @param orders*/private void spitOutDrink(List orders) {Iterator itr = orders.iterator();Drink drink;while (itr.hasNext()) {drink = (Drink) itr.next();if (drink instanceof Cola && colaBox.size()>0) {colaBox.remove(colaBox.size()-1);System.out.println(" >>> 可乐 ");} else if (drink instanceof Sprite && spriteBox.size()>0) {spriteBox.remove(spriteBox.size()-1);System.out.println(" >>> 雪碧 ");} else if (drink instanceof OrangeJuice && orangeJuiceBox.size()>0) {orangeJuiceBox.remove(orangeJuiceBox.size()-1);System.out.println(" >>> 橙汁 ");}}}/*** 查询当前数量*/public void showNum(){System.out.println("当前数量: 可乐\t雪碧\t橙汁\n\t" + colaBox.size() + "\t"+ spriteBox.size() + "\t" + orangeJuiceBox.size());}}
/** 售货机的价格管理器*/
public class PriceManager {private double colaPrice;private double spritePrice;private double orangeJuicePrice;private Properties properties = new Properties(); public PriceManager() throws IOException{InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("price.properties");try {properties.load(inputStream);this.colaPrice = Double.valueOf(properties.getProperty("colaPrice"));this.spritePrice = Double.valueOf(properties.getProperty("spritePrice"));this.orangeJuicePrice = Double.valueOf(properties.getProperty("orangeJuicePrice"));} catch (IOException e) {throw e;}}public double getColaPrice() {return colaPrice;}public double getSpritePrice() {return spritePrice;}public double getOrangeJuicePrice() {return orangeJuicePrice;}}
#src目录下的price.properties#可乐价格
colaPrice=2.00
#雪碧价格
spritePrice=2.00
#橙汁价格
orangeJuicePrice=3.00
/**
*测试类
*/
public class ConsumeTest {public static void main(String[] args) {VendingMachine machine = null; try {machine = new VendingMachine(50);} catch (IOException e) {System.out.println("初始化售货机失败");}Commander commander = new Commander(machine);Consumer consumer = new Consumer(commander);consumer.buyCola(2);consumer.buyOrangeJuice(20);consumer.buySprite(10);consumer.check(100);machine.showNum();}}
这篇关于面试题:模拟自动售货机的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!