`
machunlin
  • 浏览: 27353 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

面试题:模拟自动售货机

    博客分类:
  • java
 
阅读更多

   前两天面试,编程题是写一个模拟自动售货机的程序,当时时间匆忙,匆匆写了一下,回来就在机器上做了完善。记下来,下次或许能用到这些代码。

 

    代码还需要优化,如果卖的饮料品牌有变化,就得改类了,不符合开闭原则,暂时还没想到什么好的方法,期待高手指点。

 

/**
 * 消费者
 * 
 * @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 + "元";
		}

		// 本次交易结束,次交易金额重置为0
		sumPrice = 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();
		
	}

}
 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics