外賣訂餐系統
阿新 • • 發佈:2018-12-18
本系統採用了抽象工廠的用例模式 飲料抽象類->四種飲料類 食物抽象類->三種食物類 抽象工廠類->七種工廠類 客戶類 gui類
客戶類傳送訊息給工廠類,由工廠類創造具體食物物件,再由gui實現介面
客戶類:
package kfc; import java.util.Scanner; import drink.Drink; import factory.*; import food.Food; import food.MashedPotato; public class Client { private double finalPrice; private Factory factory; public Client() { finalPrice=0; } public Drink chooseCola() {//選擇可樂 factory=new ColaFactory(); return factory.createDrink(); } public Drink chooseFenda() {//選擇芬達 factory=new FendaFactory(); return factory.createDrink(); } public Drink chooseOrange() {//選擇可樂 factory=new OrangeFactory(); return factory.createDrink(); } public Drink chooseSprite() {//選擇雪碧 factory=new SpriteFactory(); return factory.createDrink(); } public Food chooseFriedChicken() {//選擇炸雞腿 factory=new FriedChickedFactory(); return factory.createFood(); } public Food choosePopChicked() {//選擇炸雞翅 factory=new PopChickenFactory(); return factory.createFood(); } public Food chooseMashedPotato() {//選擇炸薯條 factory=new MashedPotatoFactory(); return factory.createFood(); } public double calculatePrice(double price) { this.finalPrice+=price; return this.finalPrice; } public void reducePrice(double price) { this.finalPrice-=price; } }
客戶端類
package kfc; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JComboBox; import javax.swing.JButton; import javax.swing.BorderFactory; import javax.swing.DefaultComboBoxModel; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Color; import javax.swing.JTextField; import javax.swing.JSeparator; import javax.swing.SwingConstants; import javax.swing.JTable; import javax.swing.border.LineBorder; import javax.swing.table.DefaultTableModel; import drink.Drink; import food.Food; import javax.swing.JScrollPane; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class App extends JFrame { Client me; private JPanel contentPane; private JTextField number; private JTable table; private String sign; private int row; private DefaultTableModel model; /** * Launch the application. * @throws IOException */ public static void main(String[] args) throws IOException { EventQueue.invokeLater(new Runnable() { public void run() { try { App frame = new App(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public App() { me=new Client(); sign="炸雞腿"; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 984, 577); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel imagess = new JLabel(""); imagess.setBounds(74, 54, 159, 147); imagess.setIcon(new ImageIcon("images\\炸雞腿.jpg")); contentPane.add(imagess); JLabel price = new JLabel(""); price.setBounds(126, 337, 72, 18); price.setText(me.chooseFriedChicken().getPrice()+"元"); contentPane.add(price); JComboBox comboBox = new JComboBox(); comboBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==e.SELECTED) { Icon icon=new ImageIcon("images\\"+e.getItem()+".jpg"); imagess.setIcon(icon); if(e.getItem()=="炸雞腿") { price.setText(me.chooseFriedChicken().getPrice()+"元"); sign="炸雞腿"; } else if(e.getItem()=="炸雞翅") { price.setText(me.choosePopChicked().getPrice()+"元"); sign="炸雞翅"; } else if(e.getItem()=="炸薯條") { price.setText(me.chooseMashedPotato().getPrice()+"元"); sign="炸薯條"; } else if(e.getItem()=="可樂") { price.setText(me.chooseCola().getPrice()+"元"); sign="可樂"; } else if(e.getItem()=="雪碧") { price.setText(me.chooseSprite().getPrice()+"元"); sign="雪碧"; } else if(e.getItem()=="芬達") { price.setText(me.chooseFenda().getPrice()+"元"); sign="芬達"; } else if(e.getItem()=="橙汁") { price.setText(me.chooseOrange().getPrice()+"元"); sign="橙汁"; } } } }); comboBox.setModel(new DefaultComboBoxModel(new String[] {"炸雞腿", "炸雞翅", "炸薯條", "可樂", "雪碧", "芬達", "橙汁"})); comboBox.setBounds(293, 54, 138, 39); contentPane.add(comboBox); JLabel label = new JLabel("價格"); label.setBounds(74, 327, 53, 39); contentPane.add(label); JLabel label_1 = new JLabel("份數"); label_1.setBounds(282, 337, 72, 18); contentPane.add(label_1); JLabel lblNewLabel_2 = new JLabel(""); lblNewLabel_2.setBounds(14, 35, 441, 467); lblNewLabel_2.setBorder(BorderFactory.createTitledBorder("點餐")); contentPane.add(lblNewLabel_2); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(552, 54, 380, 364); contentPane.add(scrollPane); table = new JTable(); table.setRowHeight(50); scrollPane.setViewportView(table); Object[][] datas={ {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, }; String[] names={ "名稱", "單價", "數量", "總價" }; model = new DefaultTableModel(datas, names); // table.setModel(model); table.setModel(new DefaultTableModel(datas,names) { boolean[] columnEditables = new boolean[] { false, false, false, false }; public boolean isCellEditable(int row, int column) { return columnEditables[column]; } }); model=(DefaultTableModel) table.getModel(); table.setBorder(new LineBorder(new Color(0, 0, 0), 1, true)); JLabel lblNewLabel_3 = new JLabel(""); lblNewLabel_3.setBounds(523, 35, 441, 467); lblNewLabel_3.setBorder(BorderFactory.createTitledBorder("購物車")); contentPane.add(lblNewLabel_3); row=0; JButton button = new JButton("加入購物車"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Drink drink; Food food; try { if(sign.equals("炸雞腿")) { food=me.chooseFriedChicken(); food.setNum(Integer.parseInt(number.getText())); table.setValueAt(food.getName(), row, 0); table.setValueAt(food.getPrice(), row, 1); table.setValueAt(food.getNum(), row, 2); table.setValueAt(food.getTotalPrice(), row, 3); table.setValueAt("總計", row+1, 0); table.setValueAt(me.calculatePrice(food.getTotalPrice()), row+1, 3); row++; } else if(sign.equals("炸雞翅")) { food=me.choosePopChicked(); food.setNum(Integer.parseInt(number.getText())); table.setValueAt(food.getName(), row, 0); table.setValueAt(food.getPrice(), row, 1); table.setValueAt(food.getNum(), row, 2); table.setValueAt(food.getTotalPrice(), row, 3); table.setValueAt("總計", row+1, 0); table.setValueAt(me.calculatePrice(food.getTotalPrice()), row+1, 3);![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20181028175400454.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BvbGl0ZWJveQ==,size_27,color_FFFFFF,t_70) row++; } else if(sign.equals("炸薯條")) { food=me.chooseMashedPotato(); food.setNum(Integer.parseInt(number.getText())); table.setValueAt(food.getName(), row, 0); table.setValueAt(food.getPrice(), row, 1); table.setValueAt(food.getNum(), row, 2); table.setValueAt(food.getTotalPrice(), row, 3); table.setValueAt("總計", row+1, 0); table.setValueAt(me.calculatePrice(food.getTotalPrice()), row+1, 3); row++; } else if(sign.equals("可樂")) { drink=me.chooseCola(); drink.setNum(Integer.parseInt(number.getText())); table.setValueAt(drink.getName(), row, 0); table.setValueAt(drink.getPrice(), row, 1); table.setValueAt(drink.getNum(), row, 2); table.setValueAt(drink.getTotalPrice(), row, 3); table.setValueAt("總計", row+1, 0); table.setValueAt(me.calculatePrice(drink.getTotalPrice()), row+1, 3); row++; } else if(sign.equals("雪碧")) { drink=me.chooseSprite(); drink.setNum(Integer.parseInt(number.getText())); table.setValueAt(drink.getName(), row, 0); table.setValueAt(drink.getPrice(), row, 1); table.setValueAt(drink.getNum(), row, 2); table.setValueAt(drink.getTotalPrice(), row, 3); table.setValueAt("總計", row+1, 0); table.setValueAt(me.calculatePrice(drink.getTotalPrice()), row+1, 3); row++; } else if(sign.equals("芬達")) { drink=me.chooseFenda(); drink.setNum(Integer.parseInt(number.getText())); table.setValueAt(drink.getName(), row, 0); table.setValueAt(drink.getPrice(), row, 1); table.setValueAt(drink.getNum(), row, 2); table.setValueAt(drink.getTotalPrice(), row, 3); table.setValueAt("總計", row+1, 0); table.setValueAt(me.calculatePrice(drink.getTotalPrice()), row+1, 3); row++; } else if(sign.equals("橙汁")) { drink=me.chooseOrange(); drink.setNum(Integer.parseInt(number.getText())); table.setValueAt(drink.getName(), row, 0); table.setValueAt(drink.getPrice(), row, 1); table.setValueAt(drink.getNum(), row, 2); table.setValueAt(drink.getTotalPrice(), row, 3); table.setValueAt("總計", row+1, 0); table.setValueAt(me.calculatePrice(drink.getTotalPrice()), row+1, 3); row++; } }catch(NumberFormatException e) { System.out.println("輸入的數字出現異常,重新輸入"); } } }); number = new JTextField(); number.setText("1"); number.setBounds(358, 332, 72, 29); contentPane.add(number); number.setColumns(10); button.setBounds(317, 431, 113, 27); contentPane.add(button); JButton remove = new JButton("移除"); remove.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int rows = table.getSelectedRow(); if(rows == -1){ JOptionPane.showMessageDialog(App.this,"請選擇要刪除的套餐!"); }else{ model.removeRow(rows); System.out.println(rows); row--; me.reducePrice(Double.parseDouble(String.valueOf(table.getValueAt(rows-1, 3)))); table.setValueAt("總計", row, 0); table.setValueAt(me.calculatePrice(0), row, 3); } } }); remove.setBounds(564, 431, 113, 27); contentPane.add(remove); JButton buy = new JButton("購買"); buy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new Ensure(App.this); } }); buy.setBounds(819, 431, 113, 27); contentPane.add(buy); this.setResizable(false); } public void print() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(date)); System.out.println("名稱\t單價\t數量\t總價"); for(int i=0;i<row+1;i++) { for(int j=0;j<4;j++) { if(table.getValueAt(i, j)==null) { System.out.print("\t"); continue; } System.out.print(table.getValueAt(i, j)+"\t"); } System.out.println(); } } }
另外通過本程式對於gui的combobox和jtable元件有了進一步的瞭解 combobox的建立並新增監視器
JComboBox comboBox = new JComboBox(); comboBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==e.SELECTED) { } } }); comboBox.setModel(new DefaultComboBoxModel(new String[] {" ", " ", " ", " ", " ", " ", " "})); comboBox.setBounds(293, 54, 138, 39); contentPane.add(comboBox);
JTable的建立
table = new JTable();
table.setRowHeight(50);
scrollPane.setViewportView(table);
Object[][] datas={
};
String[] names={
};
table.setModel(new DefaultTableModel(datas,names) {
boolean[] columnEditables = new boolean[] {
false, false, false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
model=(DefaultTableModel) table.getModel();
//得到表格控制器
table.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
ubmV0L3BvbGl0ZWJveQ==,size_27,color_FFFFFF,t_70)