一、创建包
二、编写代码
1、编写员工实体类(Employee)
package com.tencent.entity;
/**
* 员工的实体类
*/
public class Employee {
// 编号
private String empId;
// 姓名
private String empName;
// 性别
private String Gender;
// 年龄
private int Age;
// 籍贯
private String home;
// 月薪
private double monthSalary;
public Employee() {
}
public Employee(String empId, String empName, String gender, int age, String home, double monthSalary) {
this.empId = empId;
this.empName = empName;
Gender = gender;
Age = age;
this.home = home;
this.monthSalary = monthSalary;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getGender() {
return Gender;
}
public void setGender(String gender) {
Gender = gender;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
public double getMonthSalary() {
return monthSalary;
}
public void setMonthSalary(double monthSalary) {
this.monthSalary = monthSalary;
}
@Override
public String toString() {
return "Employee{" +
"empId='" + empId + '\'' +
", empName='" + empName + '\'' +
", Gender='" + Gender + '\'' +
", Age=" + Age +
", home='" + home + '\'' +
", monthSalary=" + monthSalary +
'}';
}
}
2、编写账户实体类(Account)
package com.tencent.entity;
/**
* 账户实体类
*/
public class Account {
// 帐号
private String id;
// 账号名称
private String username;
// 账号密码
private String password;
public Account() {
}
public Account(String id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Account{" +
"id='" + id + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
3、编写数据源(EmployeeDataSource)
package com.tencent.data;
import com.tencent.entity.Account;
import com.tencent.entity.Employee;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 数据源:存储员工和员工信息
*/
public class EmployeeDataSource {
// 创建一个员工的数据源
static List<Employee> list = new ArrayList<Employee>();
// 创建一个账号的数据源
static Map<String, Account> map = new HashMap<String, Account>();
// 初始化员工的数据源
public static List<Employee> initEmpSource(){
list.add(new Employee("tst2005010001","王 明","男",26,"河南省许昌市",6000.0));
list.add(new Employee("tst2005020006","张馨予","男",27,"河南省平顶山市",5800.0));
return list;
}
// 初始化账户数据源
public static Map<String,Account> initAccountSource(){
map.put("sdd440606810001",new Account("sdd440606810001","TOM","1233456"));
map.put("sdd667739403221",new Account("sdd667739403221","Jerry","1357902"));
return map;
}
}
4、编写账户的访问接口(AccountDao)
package com.tencent.dao;
/**
* Account账户的访问接口AccountDao,定义了和账户接口相关的一些基本操作(CRUD)
*/
public interface AccountDao {
// 验证用户是否合法
public boolean checkAccountByUsernameAndPassword(String username ,String password);
}
5、在访问层(dao)中创建包Impl并编写账户的访问接口的实现类(AccountDaoImpl)
package com.tencent.dao.Impl;
import com.tencent.dao.AccountDao;
import com.tencent.data.EmployeeDataSource;
import com.tencent.entity.Account;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
// AccountDao接口的实现类
public class AccountDaoImpl implements AccountDao {
// 账户的数据源
Map<String, Account> map = new HashMap<String, Account>();
// 在创建AccountDaoImpl的时候初始化Account的数据源
public AccountDaoImpl(){
map = EmployeeDataSource.initAccountSource();
}
@Override
public boolean checkAccountByUsernameAndPassword(String username, String password) {
// 标志位标识是否合法用户,默认值false
boolean flag = false;
Collection<Account> accounts = map.values();
for (Account account : accounts){
if (username.equals(account.getUsername()) && password.equals((account.getPassword()))){
// 合法
flag = true;
}
}
return flag;
}
}
6、编写员工的访问接口(EmployeeDao)
package com.tencent.dao;
import com.tencent.entity.Employee;
import java.util.List;
/**
* Employee员工的访问接口EmployeeDao,定义了和员工接口相关的一些基本操作(CRUD)
*/
public interface EmployeeDao {
// 查询所有员工信息
public List<Employee> getEmpList();
// 根据empId编号查询一条员工信息
public Employee getEmpByEmpId(String empId);
// 添加一条员工信息
public void addEmp(Employee employee);
// 根据empId修改员工信息
public void updateEmp(Employee employee);
// 根据empId删除员工信息
public void deleteEmpByEmpId(String empId);
}
7、 编写员工的访问接口的实现类(EmployeeDaoImpl)
package com.tencent.dao.Impl;
import com.tencent.dao.EmployeeDao;
import com.tencent.data.EmployeeDataSource;
import com.tencent.entity.Employee;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class EmployeeDaoImpl implements EmployeeDao {
// 员工的数据类
List<Employee> list = new ArrayList<Employee>();
// 在创建EmployeeDaoImpl的时候初始化Employee的数据源
public EmployeeDaoImpl(){
list = EmployeeDataSource.initEmpSource();
}
@Override
public List<Employee> getEmpList() {
return list;
}
@Override
public Employee getEmpByEmpId(String empId) {
Employee employee = null;
for (Iterator<Employee> iterator = list.iterator();iterator.hasNext();){
Employee employee1 = iterator.next();
if (empId.equals(employee1.getEmpId())){
employee = employee1;
}
}
return employee;
}
@Override
public void addEmp(Employee employee) {
list.add(employee);
}
@Override
public void updateEmp(Employee employee) {
for (Employee employee1 : list){
if (employee.getEmpId().equals(employee1.getEmpId())){
employee1.setEmpId(employee.getEmpId());
employee1.setEmpName(employee.getEmpName());
employee1.setGender(employee.getGender());
employee1.setAge(employee.getAge());
employee1.setHome(employee.getHome());
employee1.setMonthSalary(employee.getMonthSalary());
}
}
}
@Override
public void deleteEmpByEmpId(String empId) {
for (Iterator<Employee> iterator = list.iterator();iterator.hasNext();){
Employee employee = iterator.next();
if (empId.equals(employee.getEmpId())){
iterator.remove();
}
}
}
}
8、编写账户的业务类(AccountService)【内容可复制账户的访问接口】
package com.tencent.service;
/*
* 账户的业务类
*/
public interface AccountService {
// 验证用户是否合法
public boolean checkAccountByUsernameAndPassword(String username ,String password);
}
9、在服务层(service)中创建包Impl并编写账户的业务类的实现类(AccountServiceImpl)
package com.tencent.service.Impl;
import com.tencent.dao.AccountDao;
import com.tencent.dao.Impl.AccountDaoImpl;
import com.tencent.service.AccountService;
public class AccountServiceImpl implements AccountService {
// 定义一个数据库访问对象AccountDaoImpl
AccountDao accountDao = new AccountDaoImpl();
@Override
public boolean checkAccountByUsernameAndPassword(String username, String password) {
return accountDao.checkAccountByUsernameAndPassword(username, password);
}
}
10、编写员工的业务类(EmployeeService)
package com.tencent.service;
import com.tencent.entity.Employee;
public interface EmployeeService {
// 查询所有员工信息
public void getEmpList();
// 根据empId编号查询一条员工信息
public Employee getEmpByEmpId(String empId);
// 添加一条员工信息
public void addEmp(Employee employee);
// 根据empId修改员工信息
public void updateEmp(Employee employee);
// 根据empId删除员工信息
public void deleteEmpByEmpId(String empId);
}
11、 编写员工的业务类的实现类(EmployeeServiceImpl)
package com.tencent.service.Impl;
import com.tencent.dao.EmployeeDao;
import com.tencent.dao.Impl.EmployeeDaoImpl;
import com.tencent.entity.Employee;
import com.tencent.service.EmployeeService;
import java.util.List;
public class EmployeeServiceImpl implements EmployeeService {
// 定义一个数据库访问对象
EmployeeDao employeeDao = new EmployeeDaoImpl();
@Override
public void getEmpList() {
// 获取所有员工的集合
List<Employee> empList = employeeDao.getEmpList();
System.out.println("\t\t【编号】\t【姓名】\t【性别】\t【年龄】\t【籍贯】\t【月薪】");
for (Employee employee : empList){
System.out.print("\t\t" + employee.getEmpId());
System.out.print("\t\t" + employee.getEmpName());
System.out.print("\t\t" + employee.getGender());
System.out.print("\t\t" + employee.getAge());
System.out.print("\t\t" + employee.getHome());
System.out.print("\t\t" + employee.getMonthSalary() + "\n");
}
}
/**
* 根据输入的编号获取一个员工的信息
* @param empId 员工编号
* @return 根据员工编号查询出的一条员工记录
* */
@Override
public Employee getEmpByEmpId(String empId) {
// 根据empId获取对应的一个员工信息
Employee emp = employeeDao.getEmpByEmpId(empId);
System.out.print("\t\t" + emp.getEmpId());
System.out.print("\t\t" + emp.getEmpName());
System.out.print("\t\t" + emp.getGender());
System.out.print("\t\t" + emp.getAge());
System.out.print("\t\t" + emp.getHome());
System.out.print("\t\t" + emp.getMonthSalary() + "\n");
return emp;
}
@Override
public void addEmp(Employee employee) {
employeeDao.addEmp(employee);
}
@Override
public void updateEmp(Employee employee) {
employeeDao.updateEmp(employee);
}
@Override
public void deleteEmpByEmpId(String empId) {
employeeDao.deleteEmpByEmpId(empId);
}
}
12、编写控制层(controller)
package com.tencent.controller;
import com.tencent.entity.Employee;
import com.tencent.service.EmployeeService;
import com.tencent.service.Impl.EmployeeServiceImpl;
import java.util.Scanner;
public class ControllerCenter {
// 创建一个业务实现类
static EmployeeService employeeService = new EmployeeServiceImpl();
// 主菜单
public static void printMainMenu(){
System.out.println("\t\t操作码\t操作名称\n");
System.out.println("\t\t1------添加员工");
System.out.println("\t\t2------删除员工");
System.out.println("\t\t3------修改员工");
System.out.println("\t\t4------查询员工");
System.out.println("\t\t5------退出系统");
}
// 返回主菜单
public static String returnMainMenu(){
Scanner input = new Scanner(System.in);
printMainMenu();
System.out.print("请输入操作码:【提示:必须是数字,如:1】---:");
String operNum = input.next();
return operNum;
}
// 启动主程序
public static void start(){
// 验证登录判断
// 显示系统主菜单
Scanner input = new Scanner(System.in);
System.out.println("***************欢迎登录员工信息管理系统***************");
printMainMenu();
// 定义一个操作码
System.out.print("请输入操作码:【提示:必须是数字,如:1】---:");
String operNum = input.next();
// 判断用户输入的操作码是什么操作?
while(!"5".equals(operNum)){
if("1".equals(operNum)){
// 添加员工
System.out.print("请输入新录入的员工编号:");
String empId = input.next();
System.out.print("请输入新录入的员工姓名:");
String empName = input.next();
System.out.print("请输入新录入的员工性别:");
String gender = input.next();
System.out.print("请输入新录入的员工年龄:");
int age = input.nextInt();
System.out.print("请输入新录入的员工籍贯:");
String home = input.next();
System.out.print("请输入新录入的员工月薪:");
double monthSalary = Double.parseDouble(input.next());
// 封装成对象
Employee employee = new Employee(empId,empName,gender,age,home,monthSalary);
// 调用添加员工业务类
employeeService.addEmp(employee);
System.out.println("添加成功!");
System.out.println("员工列表如下:");
// 查询所有员工信息
employeeService.getEmpList();
System.out.println("\t\t操作码\t操作名称\n");
System.out.println("\t\t1------继续添加");
System.out.println("\t\t2------返回上级菜单");
System.out.print("请输入操作码:【提示:必须是数字,如:1】---:");
operNum = input.next();
// 判断操作
if ("1".equals(operNum)){
continue;
}else if ("2".equals(operNum)){
// 显示主菜单
operNum = returnMainMenu();
}
}else if ("2".equals(operNum)){
employeeService.getEmpList();
System.out.print("请输入要删除的员工对应的【员工编号】:");
String empId = input.next();
System.out.print("你是否确认要删除?(y/n):");
String deleteFlag = input.next();
if ("y".equals(deleteFlag)){
// 删除员工
employeeService.deleteEmpByEmpId(empId);
System.out.println("删除成功!");
System.out.println("员工列表如下:");
// 查询所有员工信息
employeeService.getEmpList();
System.out.println("\t\t操作码\t操作名称\n");
System.out.println("\t\t1------继续删除");
System.out.println("\t\t2------返回上级菜单");
System.out.print("请输入操作码:【提示:必须是数字,如:1】---:");
operNum = input.next();
// 判断操作
if ("1".equals(operNum)){
operNum = "2";
continue;
}else if ("2".equals(operNum)){
// 显示主菜单
operNum = returnMainMenu();
}
}
}else if ("3".equals(operNum)){
employeeService.getEmpList();
System.out.print("请输入要修改的员工对应的【员工编号】:");
String empId = input.next();
// 根据empId查询一条员工记录
Employee emp = employeeService.getEmpByEmpId(empId);
System.out.print("原有员工姓名是:【" + emp.getEmpName() + "】 请输入新的员工姓名:");
String newEmpName = input.next();
System.out.print("原有员工性别是:【" + emp.getGender() + "】 请输入新的员工性别:");
String newGender = input.next();
System.out.print("原有员工年龄是:【" + emp.getAge() + "】 请输入新的员工年龄:");
int newAge = input.nextInt();
System.out.print("原有员工籍贯是:【" + emp.getHome() + "】 请输入新的员工籍贯:");
String newHome = input.next();
System.out.print("原有员工月薪是:【" + emp.getMonthSalary() + "】 请输入新的员工月薪:");
double newMonthSalary = Double.parseDouble(input.next());
emp.setEmpId(empId);
emp.setEmpName(newEmpName);
emp.setGender(newGender);
emp.setAge(newAge);
emp.setHome(newHome);
emp.setMonthSalary(newMonthSalary);
// 修改员工记录
employeeService.updateEmp(emp);
System.out.println("修改成功!");
System.out.println("员工列表如下:");
// 查询所有员工信息
employeeService.getEmpList();
System.out.println("\t\t操作码\t操作名称\n");
System.out.println("\t\t1------继续修改");
System.out.println("\t\t2------返回上级菜单");
System.out.print("请输入操作码:【提示:必须是数字,如:1】---:");
operNum = input.next();
// 判断操作
if ("1".equals(operNum)){
operNum = "3";
continue;
}else if ("2".equals(operNum)){
// 显示主菜单
operNum = returnMainMenu();
}
}else if ("4".equals(operNum)){
// 查询所有员工信息
employeeService.getEmpList();
System.out.println("\t\t操作码\t操作名称\n");
System.out.println("\t\t1------返回上级菜单");
System.out.print("请输入操作码:【提示:必须是数字,如:1】---:");
operNum = input.next();
if ("1".equals(operNum)){
operNum = returnMainMenu();
continue;
}else {
System.out.println("输入编号不正确!");
operNum = "4";
}
}
}
System.out.println("您已退出操作系统!");
}
}
13 、编写完毕进行测试(test)
package com.tencent.test;
import com.tencent.controller.ControllerCenter;
public class TestSystem {
public static void main(String[] args) {
ControllerCenter.start();
}
}
:
01、开始运行
02、输入4查询员工
03、输入1返回上级菜单
04、输入1添加员工(以添加员工路人甲为例)
05、输入1继续添加(以继续添加路人乙为例)
06、输入2返回上级菜单
07、输入2删除员工(以删除员工王明为例)
08、输入1继续删除(以继续删除路人乙为例)
09、输入2返回上级菜单
10、输入3修改员工(已修改张馨予信息为例)
11、输入2继续修改(以继续修改路人甲的信息为例)
12、输入2返回上级菜单
13、输入5退出系统