前两天面试,编程题是写一个模拟自动售货机的程序,当时时间匆忙,匆匆写了一下,回来就在机器上做了完善。记下来,下次或许能用到这些代码。
代码还需要优化,如果卖的饮料品牌有变化,就得改类了,不符合开闭原则,暂时还没想到什么好的方法,期待高手指点。
/**
* 消费者
*
* @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();
}
}
分享到:
相关推荐
大数据面试题V3.0完成了。共523道题,679页,46w+字,来源于牛客870+篇面经。 主要分为以下几部分: Hadoop面试题:100道 Zookeeper面试题:21道 Hive面试题:47道 Flume面试题:11道 Kafka面试题:59到 HBase面试题...
前端面试题: 精选Vue面试题及答案.pdf
经典面试题: 2021Vue经典面试题总结(含答案).pdf
python笔记50-面试题:交换圣诞节礼物全文共5页,当前为第1页。python笔记50-面试题:交换圣诞节礼物全文共5页,当前为第1页。python笔记50-面试题:交换圣诞节礼物 python笔记50-面试题:交换圣诞节礼物全文共5页,...
医疗卫生面试真题:卫生类典型面试题汇总及答案.pdf
2021最新大厂AI面试题:107题(含答案及解析).pdf
2021最新大厂AI面试题:Q3版107题(含答案及解析).pdf
JAVA面试题JAVA面试题JAVA面试题JAVA面试题JAVA面试题JAVA面试题
大数据面试题V3.0完成了。共523道题,679页,46w+字,来源于牛客870+篇面经。 主要分为以下几部分: Hadoop面试题:100道 Zookeeper面试题:21道 Hive面试题:47道 Flume面试题:11道 Kafka面试题:59到 HBase面试题...
最新面试题:vue-面试题.pdf
经典面试题:最长公共子序列.html
python爬虫Python爬虫知识领域+网络数据抓取技术关键词+HTTP请求、选择器、解析、存储、反爬虫、动态加载、数据去重、异步编程、Scrapy框架、...降权策略、Item、Pipeline内容关键词+数据挖掘、市场调研、自动化测试
vue面试题: 前端面试题库
Vue面试题:.txt
医疗卫生面试真题:卫生类典型面试题汇总.docx
BAT及各大互联网公司2014前端笔试面试题:HTML