外卖订餐系统
本系统采用了抽象工厂的用例模式
饮料抽象类->四种饮料类
食物抽象类->三种食物类
抽象工厂类->七种工厂类
客户类
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);
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));