package com.pxtadion.constant; /** * 枚举类--饮料种类 * @author pxtadion * */ public enum BevegrageEnum { TEA("TEA", "茶"), COFFEE("COFFEE", "咖啡"); private String key; private String value; private BevegrageEnum(String key, String value) { this.key = key; this.value = value; } public String getKey() { return this.key; } public String getValue() { return this.value; } public String getName() { return this.name(); } }
package com.pxtadion.constant; /** * 枚举类--咖啡配料 * @author pxtadion * */ public enum CoffeeEnum { COFFEE_ADD_COMDIMENTS("ADD_COMDIMENTS", "都加"), COFFEE_ADD_MILK("ADD_SUGAR", "加牛奶"), COFFEE_ADD_SUGAR("ADD_MILK", "加糖"), COFFEE_NOT_ADD_COMDIMENTS("NOT_ADD_COMDIMENTS", "都不加"); private String key; private String value; private CoffeeEnum(String key, String value) { this.key = key; this.value = value; } public String getKey() { return this.key; } public String getValue() { return this.value; } public String getName() { return this.name(); } }
package com.pxtadion.constant; /** * 枚举类--茶配料 * @author pxtadion * */ public enum TeaEnum { TEA_ADD_LEMON("ADD_LEMMON", "加柠檬"), TEA_NOT_ADD_LEMON("ADD_LEMMON", "不加柠檬"); private String key; private String value; private TeaEnum(String key, String value) { this.key = key; this.value = value; } public String getKey() { return this.key; } public String getValue() { return this.value; } public String getName() { return this.name(); } }
package com.pxtadion.model; /** * 抽象饮料类,公共步骤放这里 * @author pxtadion * */ public abstract class BevegrageAbstract { private String processBoilWater; private String processBrew; private String processInCup; private String processCoundiments; private String comment; //不能被重写 public final void create(){ //1、把水煮沸 boilWater(); //2、用沸水冲泡 brew(); //3、把咖啡/茶倒进杯子 pourInCup(); //4、添加辅料 addCoundiments(); } public void boilWater(){ this.processBoilWater = "把水煮沸"; } public abstract void pourInCup(); public abstract void addCoundiments(); public void brew(){ this.processBrew = "用沸水冲泡"; } public String getProcessBoilWater() { return processBoilWater; } public void setProcessBoilWater(String processBoilWater) { this.processBoilWater = processBoilWater; } public String getProcessBrew() { return processBrew; } public void setProcessBrew(String processBrew) { this.processBrew = processBrew; } public String getProcessInCup() { return processInCup; } public void setProcessInCup(String processInCup) { this.processInCup = processInCup; } public String getProcessCoundiments() { return processCoundiments; } public void setProcessCoundiments(String processCoundiments) { this.processCoundiments = processCoundiments; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } @Override public String toString() { return this.getProcessBoilWater() + "\n" + this.getProcessBrew() + "\n" + this.getProcessInCup() + "\n" + this.getProcessCoundiments() + "\n......." + this.getComment(); } }
package com.pxtadion.model; import java.io.Serializable; /** * 咖啡 * @author pxtadion * */ public class Coffee extends BevegrageAbstract implements Serializable { private static final long serialVersionUID = -7870579473701724441L; private boolean isAddSugar = true; //是否加糖 private boolean isAddMilk = true; //是否加牛奶 /** * 制作咖啡,是否添加佐料 * @param isAddSugar 是否加糖 * @param isAddMilk 是否加牛奶 */ public Coffee(boolean isAddSugar, boolean isAddMilk){ this.isAddSugar = isAddSugar; this.isAddMilk = isAddMilk; } @Override public void pourInCup() { this.setProcessInCup("把咖啡倒进杯子"); } @Override public void addCoundiments() { if(isAddSugar && isAddMilk) { this.setProcessCoundiments("加糖和牛奶"); this.setComment("咖啡(加糖和牛奶)准备好了^-^"); } else if(!isAddSugar && !isAddMilk){ this.setProcessCoundiments("不加糖和牛奶"); this.setComment("咖啡(不加糖和牛奶)准备好了^-^"); } else if(!isAddSugar){ this.setProcessCoundiments("加牛奶"); this.setComment("咖啡(加牛奶)准备好了^-^"); } else if(!isAddMilk){ this.setProcessCoundiments("加糖"); this.setComment("咖啡(加糖)准备好了^-^"); } } }
package com.pxtadion.model; import java.io.Serializable; /** * 茶 * @author pxtadion * */ public class Tea extends BevegrageAbstract implements Serializable { private static final long serialVersionUID = -4146372358381205083L; private boolean isAddLemon = true; //是否加柠檬 /** * 是否添加柠檬 * @param isAddlemon 是否加柠檬 */ public Tea(boolean isAddlemon){ this.isAddLemon = isAddlemon; } @Override public void pourInCup() { this.setProcessInCup("把茶倒进杯子"); } @Override public void addCoundiments() { if(isAddLemon) { this.setProcessCoundiments("加柠檬"); this.setComment("柠檬茶准备好了^-^"); } else { this.setProcessCoundiments(""); this.setComment("茶(不加柠檬)准备好了^-^"); } } }
package com.pxtadion; import java.awt.GridLayout; import javax.swing.ButtonGroup; import javax.swing.JPanel; import javax.swing.JRadioButton; /** * 饮料Panel * @author pxtadion * */ class BevegrageTypePanel extends JPanel { private static final long serialVersionUID = 2396422580458970858L; private JRadioButton teaRadioButton = new JRadioButton("茶", true); private JRadioButton coffeeRadioButton = new JRadioButton("咖啡"); private ButtonGroup bevegrageButtonGroup = new ButtonGroup(); public BevegrageTypePanel() { setLayout(new GridLayout(1, 3)); bevegrageButtonGroup.add(teaRadioButton); bevegrageButtonGroup.add(coffeeRadioButton); this.add(teaRadioButton); this.add(coffeeRadioButton); } public JRadioButton getTeaRadioButton() { return teaRadioButton; } public JRadioButton getCoffeeRadioButton() { return coffeeRadioButton; } public ButtonGroup getBevegrageButtonGroup() { return bevegrageButtonGroup; } }
package com.pxtadion; import java.awt.GridLayout; import javax.swing.ButtonGroup; import javax.swing.JPanel; import javax.swing.JRadioButton; import com.pxtadion.constant.CoffeeEnum; /** * 咖啡Panel * @author pxtadion * */ class CoffeePanel extends JPanel { private static final long serialVersionUID = 4781832144622033618L; private JRadioButton comdimentsRadioButton = new JRadioButton(CoffeeEnum.COFFEE_ADD_COMDIMENTS.getValue(), true); private JRadioButton sugarRadioButton = new JRadioButton(CoffeeEnum.COFFEE_ADD_SUGAR.getValue()); private JRadioButton milkRadioButton = new JRadioButton(CoffeeEnum.COFFEE_ADD_MILK.getValue()); private JRadioButton notComdimentsRadioButton = new JRadioButton(CoffeeEnum.COFFEE_NOT_ADD_COMDIMENTS.getValue()); private ButtonGroup comdimentsButtonGroup = new ButtonGroup(); public CoffeePanel() { setLayout(new GridLayout(1, 3)); comdimentsButtonGroup.add(comdimentsRadioButton); comdimentsButtonGroup.add(sugarRadioButton); comdimentsButtonGroup.add(milkRadioButton); comdimentsButtonGroup.add(notComdimentsRadioButton); this.add(comdimentsRadioButton); this.add(sugarRadioButton); this.add(milkRadioButton); this.add(notComdimentsRadioButton); } public JRadioButton getComdimentsRadioButton() { return comdimentsRadioButton; } public JRadioButton getSugarRadioButton() { return sugarRadioButton; } public JRadioButton getMilkRadioButton() { return milkRadioButton; } public JRadioButton getNotComdimentsRadioButton() { return notComdimentsRadioButton; } public ButtonGroup getComdimentsButtonGroup() { return comdimentsButtonGroup; } }
package com.pxtadion; import java.awt.GridLayout; import javax.swing.ButtonGroup; import javax.swing.JPanel; import javax.swing.JRadioButton; import com.pxtadion.constant.TeaEnum; /** * 茶Panel * @author pxtadion * */ class TeaPanel extends JPanel { private static final long serialVersionUID = 2396422580458970858L; private JRadioButton addLemonRadioButton = new JRadioButton(TeaEnum.TEA_ADD_LEMON.getValue(), true); private JRadioButton notAddLemonRadioButton = new JRadioButton(TeaEnum.TEA_NOT_ADD_LEMON.getValue()); private ButtonGroup lemonButtonGroup = new ButtonGroup(); public TeaPanel() { setLayout(new GridLayout(1, 3)); lemonButtonGroup.add(addLemonRadioButton); lemonButtonGroup.add(notAddLemonRadioButton); this.add(addLemonRadioButton); this.add(notAddLemonRadioButton); } public JRadioButton getAddLemonRadioButton() { return addLemonRadioButton; } public JRadioButton getNotAddLemonRadioButton() { return notAddLemonRadioButton; } public ButtonGroup getLemonButtonGroup() { return lemonButtonGroup; } }
package com.pxtadion; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.WindowConstants; import com.pxtadion.model.Coffee; import com.pxtadion.model.Tea; /** * 使用模板模式将公共的步骤放到抽象类中,独立的步骤放到各个子类中 * 如果想要添加不同的主料、配料和配方,则只需要修改或者添加子类即可 * @author pxtadion * */ public class BevegrageGui extends JFrame implements ItemListener { private static final long serialVersionUID = -5791377143594517519L; private TeaPanel teaPanel; private CoffeePanel coffeePanel; private BevegrageTypePanel bevegrageTypePanel; private Container container; private JButton confirmButton = new JButton("确定"); public BevegrageGui() { super(); init(); } /** * 初始化界面 */ private void init() { this.setTitle("制作饮料demo"); //关闭绝对布局 this.setLayout(null); //创建容器 container = this.getContentPane(); //创建panel teaPanel = new TeaPanel(); coffeePanel = new CoffeePanel(); bevegrageTypePanel = new BevegrageTypePanel(); //绑定饮料种类事件 bevegrageTypePanel.getTeaRadioButton().addItemListener(this); bevegrageTypePanel.getCoffeeRadioButton().addItemListener(this); //设置确定按钮功能 confirmButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { try { //选中了茶 if(bevegrageTypePanel.getTeaRadioButton().isSelected()) { Tea tea = null; if(teaPanel.getAddLemonRadioButton().isSelected()) { tea = new Tea(true); tea.create(); } else if(teaPanel.getNotAddLemonRadioButton().isSelected()) { tea = new Tea(false); tea.create(); } if(tea!=null) { JOptionPane.showMessageDialog(null, tea.toString()); } } //选中了咖啡 else if(bevegrageTypePanel.getCoffeeRadioButton().isSelected()) { Coffee coffee = null; if(coffeePanel.getComdimentsRadioButton().isSelected()) { coffee = new Coffee(true, true); coffee.create(); } else if(coffeePanel.getSugarRadioButton().isSelected()) { coffee = new Coffee(true, false); coffee.create(); } else if(coffeePanel.getMilkRadioButton().isSelected()) { coffee = new Coffee(false, true); coffee.create(); } else if(coffeePanel.getNotComdimentsRadioButton().isSelected()) { coffee = new Coffee(false, false); coffee.create(); } if(coffee!=null) { JOptionPane.showMessageDialog(null, coffee.toString()); } } } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); e.printStackTrace(); } } }); //将控件加入容器中 container.add(bevegrageTypePanel); container.add(teaPanel); container.add(confirmButton); //控件在容器中的位置及大小 bevegrageTypePanel.setBounds(40, 20, 300, 30); teaPanel.setBounds(40, 60, 300, 30); coffeePanel.setBounds(40,60,300,30); confirmButton.setBounds(40,100,60,30); //窗体在电脑中的位置及大小 this.setBounds(500, 260, 450, 400); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置窗体不可以拉伸 this.setResizable(false); this.setVisible(true); } /** * 原型模式,提供深克隆 * @return */ public Object deepClone(){ Object o = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(this); oos.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); o = ois.readObject(); ois.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return o; } @Override public void itemStateChanged(ItemEvent itemEvent) { if (itemEvent.getItemSelectable() == bevegrageTypePanel.getTeaRadioButton()) { container.remove(coffeePanel); container.add(teaPanel); } else if (itemEvent.getItemSelectable() == bevegrageTypePanel.getCoffeeRadioButton()) { container.remove(teaPanel); container.add(coffeePanel); } container.validate(); container.repaint(); } }
package com.pxtadion; public class Test { /** * @param args */ public static void main(String[] args) { new BevegrageGui(); } }