Bản quyền thuộc về TITV.vn,
vui lòng không đăng tải lại nội dung từ trang này.
Video giải thích chi tiết
Code chi tiết
package model;
import java.util.StringTokenizer;
public class ThucDonModel {
private String luaChon_MonChinh;
private String luaChon_MonPhu;
private double tongTien;
public ThucDonModel() {
this.luaChon_MonChinh = "";
this.luaChon_MonPhu = "";
this.tongTien = 0;
}
public String getLuaChon_MonChinh() {
return luaChon_MonChinh;
}
public void setLuaChon_MonChinh(String luaChon_MonChinh) {
this.luaChon_MonChinh = luaChon_MonChinh;
}
public String getLuaChon_MonPhu() {
return luaChon_MonPhu;
}
public void setLuaChon_MonPhu(String luaChon_MonPhu) {
this.luaChon_MonPhu = luaChon_MonPhu;
}
public double getTongTien() {
return tongTien;
}
public void setTongTien(double tongTien) {
this.tongTien = tongTien;
}
public void tinhTongTien() {
this.tongTien = 0;
if(this.luaChon_MonChinh.equals("CƠM")) {
tongTien+= 20000;
}else if(this.luaChon_MonChinh.equals("PHỞ")) {
tongTien+= 50000;
}else if(this.luaChon_MonChinh.equals("BÁNH MÌ")) {
tongTien+= 15000;
}
StringTokenizer stk = new StringTokenizer(this.luaChon_MonPhu, ";");
while(stk.hasMoreElements()) {
String monPhu = stk.nextToken();
monPhu = monPhu.trim();
if(monPhu.equals("TRÀ SỮA")) {
tongTien+= 5000;
}else if(monPhu.equals("COCACOLA")) {
tongTien+= 10000;
}else if(monPhu.equals("BÁNH NGỌT")) {
tongTien+= 15000;
}else if(monPhu.equals("NEM")) {
tongTien+= 20000;
}
}
}
}
package view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import control.ThucDonController;
import model.ThucDonModel;
public class ThucDonView extends JFrame {
public ThucDonModel thucDonModel;
public JRadioButton jRadioButton_Com;
public JRadioButton jRadioButton_Pho;
public JRadioButton jRadioButton_BanhMi;
public ButtonGroup buttonGroup_MonChinh;
public JCheckBox jCheckBox_TraSua;
public JCheckBox jCheckBox_CocaCola;
public JCheckBox jCheckBox_BanhNgot;
public JCheckBox jCheckBox_Nem;
public JLabel jLabel_ThongTin;
public ArrayList<JCheckBox> list_buttonGroup_MonPhu;
public ThucDonView() {
this.thucDonModel = new ThucDonModel();
init();
}
private void init() {
this.setSize(600, 600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
Font font = new Font("Arial", Font.BOLD, 40);
JLabel header = new JLabel("THỰC ĐƠN NHÀ HÀNG TITV");
header.setFont(font);
JPanel jPanel_TieuDe = new JPanel();
jPanel_TieuDe.add(header);
this.add(jPanel_TieuDe, BorderLayout.NORTH);
JPanel jPanel_MonChinh = new JPanel();
jPanel_MonChinh.setLayout(new GridLayout(1, 3));
jRadioButton_Com = new JRadioButton("CƠM");
jRadioButton_Com.setFont(font);
jRadioButton_Pho = new JRadioButton("PHỞ");
jRadioButton_Pho.setFont(font);
jRadioButton_BanhMi = new JRadioButton("BÁNH MÌ");
jRadioButton_BanhMi.setFont(font);
buttonGroup_MonChinh = new ButtonGroup();
buttonGroup_MonChinh.add(jRadioButton_Com);
buttonGroup_MonChinh.add(jRadioButton_Pho);
buttonGroup_MonChinh.add(jRadioButton_BanhMi);
jPanel_MonChinh.add(jRadioButton_Com);
jPanel_MonChinh.add(jRadioButton_Pho);
jPanel_MonChinh.add(jRadioButton_BanhMi);
JPanel jPanel_MonPhu = new JPanel();
jPanel_MonPhu.setLayout(new GridLayout(2, 2));
jCheckBox_TraSua = new JCheckBox("TRÀ SỮA");
jCheckBox_TraSua.setFont(font);
jCheckBox_CocaCola = new JCheckBox("COCACOLA");
jCheckBox_CocaCola.setFont(font);
jCheckBox_BanhNgot = new JCheckBox("BÁNH NGỌT");
jCheckBox_BanhNgot.setFont(font);
jCheckBox_Nem = new JCheckBox("NEM");
jCheckBox_Nem.setFont(font);
list_buttonGroup_MonPhu = new ArrayList<JCheckBox>();
list_buttonGroup_MonPhu.add(jCheckBox_TraSua);
list_buttonGroup_MonPhu.add(jCheckBox_CocaCola);
list_buttonGroup_MonPhu.add(jCheckBox_BanhNgot);
list_buttonGroup_MonPhu.add(jCheckBox_Nem);
jPanel_MonPhu.add(jCheckBox_TraSua);
jPanel_MonPhu.add(jCheckBox_CocaCola);
jPanel_MonPhu.add(jCheckBox_BanhNgot);
jPanel_MonPhu.add(jCheckBox_Nem);
JPanel jPanel_LuaChon = new JPanel();
jPanel_LuaChon.setLayout(new GridLayout(2, 1));
jPanel_LuaChon.add(jPanel_MonChinh);
jPanel_LuaChon.add(jPanel_MonPhu);
this.add(jPanel_LuaChon, BorderLayout.CENTER);
JPanel jPanel_ThanhToan = new JPanel();
jPanel_ThanhToan.setLayout(new GridLayout(1, 2));
jLabel_ThongTin = new JLabel();
jLabel_ThongTin.setFont(new Font("Arial",Font.BOLD, 20));
JButton jButton_ThanhToan = new JButton("Thanh Toán");
jButton_ThanhToan.setFont(font);
ActionListener thucDonController = new ThucDonController(this);
jButton_ThanhToan.addActionListener(thucDonController);
jPanel_ThanhToan.add(jLabel_ThongTin);
jPanel_ThanhToan.add(jButton_ThanhToan);
this.add(jPanel_ThanhToan, BorderLayout.SOUTH);
this.setVisible(true);
}
public void hienThiKetQua() {
String kq = "Món chính: " + this.thucDonModel.getLuaChon_MonChinh()
+ "; Món phụ: " + this.thucDonModel.getLuaChon_MonPhu()
+ "; Tổng tiền: " + this.thucDonModel.getTongTien();
this.jLabel_ThongTin.setText(kq);
}
}
package control;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.AbstractButton;
import javax.swing.JRadioButton;
import view.ThucDonView;
public class ThucDonController implements ActionListener{
private ThucDonView thucDonView;
public ThucDonController(ThucDonView thucDonView) {
this.thucDonView = thucDonView;
}
@Override
public void actionPerformed(ActionEvent e) {
String monChinh = "";
Enumeration<AbstractButton> buttons_monChinh = thucDonView.buttonGroup_MonChinh.getElements();
while(buttons_monChinh.hasMoreElements()) {
AbstractButton b = buttons_monChinh.nextElement();
if(b.isSelected()) {
monChinh = b.getText();
}
}
String monPhu = "";
for (AbstractButton b : thucDonView.list_buttonGroup_MonPhu) {
if(b.isSelected()) {
monPhu = monPhu+b.getText()+"; ";
}
}
thucDonView.thucDonModel.setLuaChon_MonChinh(monChinh);
thucDonView.thucDonModel.setLuaChon_MonPhu(monPhu);
thucDonView.thucDonModel.tinhTongTien();
thucDonView.hienThiKetQua();
}
}
package Test;
import javax.swing.UIManager;
import view.ThucDonView;
public class TestThucDon {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new ThucDonView();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Không có nhận xét nào:
Đăng nhận xét