day01-3-界面显示&用户登录&餐桌状态显示

乎语百科 239 0

满汉楼01-3

4.功能实现02

4.2菜单界面显示

4.2.1功能说明

显示主菜单、二级菜单和退出系统功能

day01-3-界面显示&用户登录&餐桌状态显示 day01-3-界面显示&用户登录&餐桌状态显示

4.2.2代码实现

先搭建界面显示的大体框架,具体的功能后面再实现

创建MHLView类:

package com.li.mhl.view;

import com.li.mhl.utils.Utility;

/**
 * @author 李
 * @version 1.0
 * 这是主界面
 */
public class MHLView {

    //控制是否退出菜单
    private boolean loop = true;
    private String key = "";//接收用户的输入

    public static void main(String[] args) {
        new MHLView().mainMenu();
    }

    //显示主菜单
    public void mainMenu() {
        while (loop) {
            System.out.println("==================满汉楼==================");
            System.out.println("\t\t 1 登录满汉楼");
            System.out.println("\t\t 2 退出满汉楼");
            System.out.print("请输入你的选择:");
            key = Utility.readString(1);//接收用户选择
            switch (key) {
                case "1":
                    System.out.print("请输入员工号: ");
                    String id=Utility.readString(50);
                    System.out.print("请输入密  码: ");
                    String pws = Utility.readString(50);
                    //到数据库去判断,这里先简单判断一下
                    if ("123".equals(pws)) {
                        System.out.println("==================登录成功=================\n");
                        //显示二级菜单,这里也和一级菜单一样是循环操作,也放在循环语句中
                        while (loop){
                            System.out.println("==================满汉楼(二级菜单)==================");
                            System.out.println("\t\t 1 显示餐桌状态");
                            System.out.println("\t\t 2 预定餐桌");
                            System.out.println("\t\t 3 显示所有菜品");
                            System.out.println("\t\t 4 点餐服务");
                            System.out.println("\t\t 5 参看账单");
                            System.out.println("\t\t 6 结账");
                            System.out.println("\t\t 9 退出满汉楼");
                            System.out.print("请输入你的选择:");
                            key = Utility.readString(1);//接收用户的选择
                            switch(key){
                                case "1":
                                    System.out.println("显示餐桌状态");
                                    break;
                                case "2":
                                    System.out.println("预定餐桌");
                                    break;
                                case "3":
                                    System.out.println("显示所有菜品");
                                    break;
                                case "4":
                                    System.out.println("点餐服务");
                                    break;
                                case "5":
                                    System.out.println("参看账单");
                                    break;
                                case "6":
                                    System.out.println("结账");
                                    break;
                                case "9":
                                    //因为loop同时控制两层循环,因此当loop设为false时,
                                    // 里层循环退出之后,外层循环也会随之退出
                                    loop=false;
                                    break;
                                default:
                                    System.out.println("输入有误,请重新输入");
                                    break;
                            }
                        }
                    }else {
                        System.out.println("==================登录失败==================");
                    }
                    break;
                case "2":
                    loop = false;//退出一级菜单的循环
                    break;
                default:
                    System.out.println("输入有误,重新输入..");
            }
        }
        System.out.println("你退出了满汉楼系统~");
    }
}

4.3用户登录功能

4.3.1功能说明

day01-3-界面显示&用户登录&餐桌状态显示 day01-3-界面显示&用户登录&餐桌状态显示

4.3.2思路分析

在数据库创建一张employee表,创建对应的Javabean,创建对应的EmployeeDAO,service层...

(大体上就是根据框架图从下至上编写代码)

4.3.3代码实现

4.3.3.1创建数据库mhl和表employee
-- 创建满汉楼的数据库
CREATE DATABASE mhl;
-- 创建表employee(主键id,empId,name,pwd,job等)
CREATE TABLE employee(
	id INT PRIMARY KEY AUTO_INCREMENT,#自增
	empId VARCHAR(50) UNIQUE NOT NULL DEFAULT '',#员工号
	pwd CHAR(32) NOT NULL DEFAULT '',#密码md5
	NAME VARCHAR(50) NOT NULL DEFAULT '',#姓名
	job VARCHAR(50) NOT NULL DEFAULT ''#岗位
)CHARSET=utf8

-- 插入测试数据
INSERT INTO employee VALUES(NULL,'6668612',MD5('123456'),'张三丰','经理');
INSERT INTO employee VALUES(NULL,'6668622',MD5('123456'),'小龙女','服务员');
INSERT INTO employee VALUES(NULL,'6668633',MD5('123456'),'张无忌','收银员');
INSERT INTO employee VALUES(NULL,'666666',MD5('123456'),'olien','经理');

SELECT * FROM employee;

day01-3-界面显示&用户登录&餐桌状态显示

4.3.3.2创建Employee类

创建和表employee对应的Javabean-Employee

package com.li.mhl.domain;

/**
 * @author 李
 * @version 1.0
 * 这是一个Javabean ,和表employee对应
 */
public class Employee {
    /**
     * +-------+-------------+------+-----+---------+----------------+
     * | Field | Type        | Null | Key | Default | Extra          |
     * +-------+-------------+------+-----+---------+----------------+
     * | id    | int(11)     | NO   | PRI | NULL    | auto_increment |
     * | empId | varchar(50) | NO   |     |         |                |
     * | pwd   | char(32)    | NO   |     |         |                |
     * | name  | varchar(50) | NO   |     |         |                |
     * | job   | varchar(50) | NO   |     |         |                |
     * +-------+-------------+------+-----+---------+----------------+
     */
    private Integer id;
    private String empId;
    private String pwd;
    private String name;
    private String job;

    public Employee() {//无参构造器,底层的Apache-DBUtils反射需要
    }

    public Employee(Integer id, String empId, String pwd, String name, String job) {
        this.id = id;
        this.empId = empId;
        this.pwd = pwd;
        this.name = name;
        this.job = job;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", empId='" + empId + '\'' +
                ", pwd='" + pwd + '\'' +
                ", name='" + name + '\'' +
                ", job='" + job + '\'' +
                '}';
    }
}
4.3.3.3创建EmployeeDAO类
package com.li.mhl.dao;

import com.li.mhl.domain.Employee;

/**
 * @author 李
 * @version 1.0
 */
public class EmployeeDAO extends BasicDAO<Employee>{
    //这里还可以执行特有的操作

}
4.3.3.4创建EmployeeService类

编写一个getEmployeeByIdAndPwd方法,来验证账号密码,并返回相应值

package com.li.mhl.service;

import com.li.mhl.dao.EmployeeDAO;
import com.li.mhl.domain.Employee;

/**
 * @author 李
 * @version 1.0
 * 该类完成对employee表的各种操作(通过调用EmployeeDAO对象完成)
 */
public class EmployeeService {

    //定义一个EmployeeDAO属性
    private EmployeeDAO employeeDAO = new EmployeeDAO();

    //登录校验方法
    //根据empId和pwd返回一个Employee对象,如果查询不到,就返回null
    public Employee getEmployeeByIdAndPwd(String empId, String pwd) {
        //注意密码使用md5加密后再查询比较
        Employee employee =
                employeeDAO.querySingle("select * from employee where empId=? and pwd=md5(?) ", Employee.class, empId, pwd);
        return employee;
    }

}
4.3.3.5修改MHLView类

在MHLView类中的里层循环中,调用方法getEmployeeByIdAndPwd,如果返回的employee对象非空,则说明存在该用户,登录成功

修改处1:增加EmployeeService属性

//定义EmployeeService属性
private EmployeeService employeeService=new EmployeeService();

day01-3-界面显示&用户登录&餐桌状态显示

修改处2:

day01-3-界面显示&用户登录&餐桌状态显示

4.4显示餐桌状态

4.4.1功能说明

当用户登录成功之后,选择显示餐桌状态,可以看到所有的餐桌编号以及对应的餐桌状态

day01-3-界面显示&用户登录&餐桌状态显示

4.4.2思路分析

创建表diningTable,创建对应的Javabean,创建对应的DAO,service层...

4.4.3代码实现

4.4.3.1创建diningTable表
-- 创建表diningTable(主键id,empId,name,pwd,job等)
CREATE TABLE diningTable(
	id INT PRIMARY KEY AUTO_INCREMENT,#自增,表示餐桌编号
	state VARCHAR(20)NOT NULL DEFAULT '',#餐桌状态
	orderName VARCHAR(50) NOT NULL DEFAULT '',#预定人的名字
	orderTel VARCHAR(20) NOT NULL DEFAULT ''#预定人的电话
)CHARSET=utf8

-- 插入测试数据
INSERT INTO diningTable VALUES(NULL,'空','','');
INSERT INTO diningTable VALUES(NULL,'空','','');
INSERT INTO diningTable VALUES(NULL,'空','','');

SELECT * FROM diningTable;

day01-3-界面显示&用户登录&餐桌状态显示

4.4.3.2创建DiningTable类
package com.li.mhl.domain;
/**
 * @author 李
 * @version 1.0
 * 这是一个Javabean ,和表 diningTable对应
 */
public class DiningTable {
    /**
     * Field      Type         Null    Key     Default  Extra
     * ---------  -----------  ------  ------  -------  ----------------
     * id         int(11)      NO      PRI     (NULL)   auto_increment
     * state      varchar(20)  NO
     * orderName  varchar(50)  NO
     * orderTel   varchar(20)  NO
     */
    private Integer id;
    private String state;
    private String orderName;
    private String orderTel;

    public DiningTable() {//无参构造器,反射需要
    }

    public DiningTable(Integer id, String state, String orderName, String orderTel) {
        this.id = id;
        this.state = state;
        this.orderName = orderName;
        this.orderTel = orderTel;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    public String getOrderTel() {
        return orderTel;
    }

    public void setOrderTel(String orderTel) {
        this.orderTel = orderTel;
    }

    @Override
    public String toString() {
        return  id + "\t\t\t" + state;
    }
}
4.4.3.3创建DiningTableDAO类
package com.li.mhl.dao;

import com.li.mhl.domain.DiningTable;

/**
 * @author 李
 * @version 1.0
 */
public class DiningTableDAO extends BasicDAO<DiningTable>{
    //如果有特别的操作,可以写在DiningTableDAO中
}
4.4.3.4创建DiningTableService类
package com.li.mhl.service;

import com.li.mhl.dao.DiningTableDAO;
import com.li.mhl.domain.DiningTable;

import java.util.List;

/**
 * @author 李
 * @version 1.0
 * 该类完成对 diningTable表的各种操作(通过调用DiningTableDAO对象完成)
 */
public class DiningTableService {//业务层
    //定义一个DiningTableDAO对象
    private DiningTableDAO diningTableDAO = new DiningTableDAO();

    //返回所有餐桌的信息
    public List<DiningTable> list(){
        List<DiningTable> diningTables =
                diningTableDAO.queryMulti("select id,state from diningTable", DiningTable.class);
        return diningTables;
    }
}
4.4.3.5修改MHLView类

修改处1:增加DiningTableService属性

//定义DiningTableService属性
private DiningTableService diningTableService=new DiningTableService();

修改处2:增加listDiningTable()方法,显示餐桌信息

//显示餐桌状态
public void listDiningTable(){
    List<DiningTable> list = diningTableService.list();
    System.out.println("\n餐桌编号\t\t餐桌状态");
    for (DiningTable diningTable:list) {
        System.out.println(diningTable);
    }
    System.out.println("============显示完毕============");
}

修改处3:在内层循环中调用方法listDiningTable

day01-3-界面显示&用户登录&餐桌状态显示

标签:

留言评论

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~