* 新增的需求:
 * 1.新增的用户密码的修改***
 * 2.新增一个部门选项,员工要对应有部门信息***
 *
 *
 * 3.思想:能不能优化一下?界面操作,能不能想一些办法提取成方法?
 * 4.选做:部门管理
 * 5.选做:根据薪水排序
 *
 * 员工和部门之间是有关联关系?
 *
 * 在给员工指定部门的时候,如果部门不存在。

 

用户属性界面:

public class User {

    private String username;
    private String password;

    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;
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
}

部门属性界面:

public class Dept {

    private Integer id;
    private String name;

    public Dept() {
    }

    public Dept(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Dept{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

员工属性界面:

import java.math.BigDecimal;

public class Employee {

        private Integer empId;
        private String name;
        private BigDecimal salary;

        //
        private Dept dept;

        public Dept getDept() {
            return dept;
        }

        public void setDept(Dept dept) {
            this.dept = dept;
        }

        public Integer getEmpId() {
            return empId;
        }

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

        public String getName() {
            return name;
        }

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

        public BigDecimal getSalary() {
            return salary;
        }

        public void setSalary(BigDecimal salary) {
            this.salary = salary;
        }

        public Employee() {
        }

        public Employee(Integer empId, String name, BigDecimal salary,Dept dept) {
            this.empId = empId;
            this.name = name;
            this.salary = salary;
            this.dept = dept;
        }

        @Override
        public String toString() {
            return "Employee{" +
                    "empId=" + empId +
                    ", name='" + name + '\'' +
                    ", salary=" + salary +
                    ", dept=" + dept +
                    '}';
        }
    }

用户注册方法:

import java.util.ArrayList;
import java.util.List;

public class UserService {

    private List<User> users = new ArrayList<>();

    {
        users.add(new User("admin","123456"));
    }

    // 判断用户名和密码对不对
    public boolean login(User user) {

        for (int i = 0; i < users.size(); i++) {
            if(user.getUsername().equals(users.get(i).getUsername()) &&
                    user.getPassword().equals(users.get(i).getPassword())){
                return true;
            }
        }
        return false;
    }

    // 判断用户名是否存在
    public boolean register(User user){
        for (int i = 0; i < users.size(); i++) {
            if(user.getUsername().equals(users.get(i).getUsername())){

                return true;
            }
        }
        users.add(user);
        return false;
    }

    /*
        1.用户名是否存在
        2.验证原始密码对不对
        3.修改密码
     */
    public boolean updatePassword(User user,String newPassword){

        int index = -1;
        User target = null;

        for (int i = 0; i < users.size(); i++) {
            if(user.getUsername().equals(users.get(i).getUsername()) &&
                    user.getPassword().equals(users.get(i).getPassword())){
                index = i;
                target = users.get(i);
                break;
            }
        }
//        if(validateUsername(user.getUsername())){
//
//        }
        if(index != -1) {
            target.setPassword(newPassword);
            users.set(index,target);
            return true;
        }
        // 如果是return false,原始用户名或密码错误
        return false;
    }

    // 判断用户名是否存在
    public boolean validateUsername(String username){

        for (int i = 0; i < users.size(); i++) {
            if(username.equals(users.get(i).getUsername())){

                return true;
            }
        }

        return false;
    }

}

员工登录方法:

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


public class EmployeeService {

    private List<Employee> emps = new ArrayList<>(16);

    {
        emps.add(new Employee(1001, "张三", new BigDecimal("10000"),new Dept(1001,"开发部")));
        emps.add(new Employee(1002, "李四", new BigDecimal("5000"),new Dept(1001,"开发部")));
        emps.add(new Employee(1003, "王五", new BigDecimal("3000"),new Dept(1002,"测试部")));
        emps.add(new Employee(1004, "张三", new BigDecimal("1000"),new Dept(1003,"运维部")));
    }

    public void setEmps(List<Employee> emps) {
        this.emps = emps;
    }

    // 新增员工,新增成功后,会展示新增的员工信息。
    // 用集合来保存员工
    public Employee addEmployee(Employee employee) {
        // 1.拿到最后一个员工
        Employee lastEmp = emps.get(emps.size() - 1);
        // 2.拿到最后一个员工的工号
        Integer lastId = lastEmp.getEmpId();
        // 3.工号自增
        lastId++;
        // 4.设置要保存的员工的工号
        employee.setEmpId(lastId);
        // 5.把员工添加到员工列表
        emps.add(employee);
        return employee;

    }

    // 查询所有
    public List<Employee> getEmps() {

        return emps;
    }

    // 根据工号查询
    public Employee getEmpById(Integer id){
        for (int i = 0; i < emps.size(); i++) {
            if(id.equals(emps.get(i).getEmpId())){
                return emps.get(i);
            }
        }
        return null;
    }

    // 修改
    public Employee updateEmpById(Integer id,Employee employee){
        // 先查询员工是否存在
        int index = -1;
        for (int i = 0; i < emps.size(); i++) {
            if(id.equals(emps.get(i).getEmpId())){
                index = i;
                break;
            }
        }
        if(index != -1){
            emps.set(index,employee);
            return employee;
        }
        return null;
    }

    // 根据id删除
    public Employee deleteEmpById(Integer id){

        Employee empById = getEmpById(id);

        return emps.remove(empById) ? empById : null;
    }

    // 根据姓名删除
    public List<Employee> deleteEmpByName(String name) {
        // 根据姓名查询,有可能查到的是多个人
        // 这个集合要存的信息就是根据姓名找到的员工们
        List<Employee> target = new ArrayList<>(16);
        for (int i = 0; i < emps.size(); i++) {
            if(name.equals(emps.get(i).getName())){
                target.add(emps.get(i));
            }
        }
        if(target.size() != 0){
            emps.removeAll(target);
            return target;
        }
        return null;
    }

}

部门的选择:

import java.util.ArrayList;
import java.util.List;

public class DeptService {

    private List<Dept> depts = new ArrayList<>();

    {
        depts.add(new Dept(1001,"开发部"));
        depts.add(new Dept(1002,"测试部"));
        depts.add(new Dept(1003,"运维部"));
    }

    public List<Dept> getDepts() {
        return depts;
    }

    public void setDepts(List<Dept> depts) {
        this.depts = depts;
    }
}

菜单的两个界面,登录还有注册,键盘输入。

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public class GUI {

    private static EmployeeService employeeService = new EmployeeService();
    private static UserService userService = new UserService();

    private static DeptService deptService = new DeptService();

    public static void userMenu(Scanner sc) {
        main:while(true){
            System.out.println("请选择操作:1.登录\t2.注册\t3.修改个人密码\t4.退出系统");
            String flag = sc.next();
            switch (flag){
                case "1":
                    while(true){
                        System.out.println("请输入用户名:");
                        String username = sc.next();
                        System.out.println("请输入密码:");
                        String password = sc.next();
                        // 比对
                        boolean login = userService.login(new User(username, password));
                        if(login){
                            System.out.println("登录成功!");
                            // 加载员工管理模块
                            employeeMenu(sc);
                            continue main;
                        }else {
                            System.out.println("用户名或密码错误...");
                            continue;
                        }
                    }
                case "2":
                    ru:while(true){
                        System.out.println("请输入用户名:");
                        String username1 = sc.next();
                        if(userService.validateUsername(username1)){
                            System.out.println("用户名已存在,请重新输入...");
                            continue ru;
                        }else {
                            System.out.println("请输入密码:");
                            String password1 = sc.next();
                            userService.register(new User(username1,password1));
                            System.out.println("注册成功!请登录...");
                            continue main;
                        }
                    }
                case "3":
                    while(true){
                        System.out.println("请输入用户名:");
                        String username2 = sc.next();
                        System.out.println("请输入原始密码:");
                        String password2 = sc.next();
                        System.out.println("请输入新始密码:");
                        String newPassword = sc.next();
                        boolean b = userService.updatePassword(new User(username2, password2), newPassword);
                        if(b) {
                            System.out.println("修改密码成功,请登录...");
                            continue main;
                        }
                        System.out.println("原始用户名或密码错误,请重新输入...");
                    }
                case "4":
                    System.out.println("系统退出中...");
                    break main;
            }
        }
    }

    private static void employeeMenu(Scanner sc) {
        main:while(true){
            System.out.println("欢迎来到员工管理:1.新增员工\t2.查询员工\t3.修改员工\t4.删除员工\t5.返回上一层");
            String flag = sc.next();
            switch (flag) {
                case "1":
                    System.out.println("请输入员工姓名:");
                    String name = sc.next();
                    System.out.println("请输入员工的薪水:");
                    String salaryStr = sc.next();
                    BigDecimal salary = new BigDecimal(salaryStr);
                    System.out.print("请选择部门:\t");
                    List<Dept> depts = deptService.getDepts();
                    for (int i = 0; i < depts.size(); i++) {
                        String deptName = depts.get(i).getName();
                        System.out.print((i+1) + "、" + deptName + "\t");
                    }
                    System.out.println();
                    String deptFlag = sc.next();
                    Employee employee = new Employee();
                    employee.setName(name);
                    employee.setSalary(salary);
                    for (int i = 0; i < depts.size(); i++) {
                        // 如果i=0
                        // deptFlag = 1
                        if(deptFlag.equals(String.valueOf(i+1))) {
                            employee.setDept(depts.get(i));
                        }
                    }
                    Employee employee1 = employeeService.addEmployee(employee);
                    System.out.println("新增员工成功,员工的信息为:" + employee1);
                    continue main;
                case "2":
                    System.out.println("请选择:1、查询所有\t2、根据工号查询");
                    String s = sc.next();
                    if(s.equals("1")){
                        System.out.println(employeeService.getEmps());
                    }else if(s.equals("2")){
                        System.out.println("请输入工号:");
                        String empId = sc.next();
                        Employee empById = employeeService.getEmpById(Integer.parseInt(empId));
                        if (Objects.isNull(empById)) {
                            System.out.println("查无此人...");
                        }else {
                            System.out.println(empById);
                        }
                    }
                    continue main;
                case "3":
                    System.out.println("请输入要修改的员工的工号:");
                    String empId = sc.next();
                    System.out.println("请输入员工的姓名:");
                    String name3 = sc.next();
                    System.out.println("请输入员工的薪水:");
                    String salaryStr3 = sc.next();
                    BigDecimal salary3 = new BigDecimal(salaryStr3);
                    System.out.print("请选择部门:\t");
                    List<Dept> depts1 = deptService.getDepts();
                    for (int i = 0; i < depts1.size(); i++) {
                        String deptName = depts1.get(i).getName();
                        System.out.print((i+1) + "、" + deptName + "\t");
                    }
                    System.out.println();
                    String deptFlag1 = sc.next();
                    Employee employee3 = new Employee();
                    employee3.setEmpId(Integer.parseInt(empId));
                    employee3.setName(name3);
                    employee3.setSalary(salary3);
                    for (int i = 0; i < depts1.size(); i++) {
                        // 如果i=0
                        // deptFlag = 1
                        if(deptFlag1.equals(String.valueOf(i+1))) {
                            employee3.setDept(depts1.get(i));
                        }
                    }
                    Employee result = employeeService.updateEmpById(Integer.parseInt(empId), employee3);
                    if(Objects.isNull(result)){
                        System.out.println("查无此人,修改失败...");
                        continue main;
                    }
                    System.out.println("修改成功!修改后的信息为:" + result);
                    continue main;
                case "4":
                    System.out.println("请选择:1.根据工号删除\t2.根据姓名删除");
                    String flag4 = sc.next();
                    switch (flag4) {
                        case "1":
                            System.out.println("请输入工号:");
                            String empId4 = sc.next();
                            Employee employee2 = employeeService.deleteEmpById(Integer.parseInt(empId4));
                            if(Objects.isNull(employee2)){
                                System.out.println("删除失败,查无此人...");
                            }else {
                                System.out.println("删除成功...");
                            }
                            continue main;
                        case "2":
                            System.out.println("请输入姓名:");
                            String name4 = sc.next();
                            List<Employee> employees = employeeService.deleteEmpByName(name4);
                            // 如果集合为空,查无此人
                            // 如果集合不为空,删除的人的信息
                            if(employees.size() == 0){
                                System.out.println("查无此人...");
                            }else {
                                System.out.println("删除成功,删除的人的信息如下:");
                                System.out.println(employees);
                            }
                            continue main;
                    }
                case "5":
                    return;
            }
        }
    }
}

运行:

 * 用list集合和面向对象写一个员工管理
 * 包括登录!!!
import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;

/***/
public class Demo {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("-----欢迎使用本系统-----");
        GUI.userMenu(sc);

        // ArrayList内部结构数组
        // 内部的数组的长度

//        List<Employee> list = new ArrayList<>(16);
//        Employee e = new Employee(1001, "张三", new BigDecimal("10000"));
//        list.add(e);
//
//        list.contains();
//        list.add(11);

        // null异常
//        System.out.println(list.get(0).toString());
//        EmployeeService employeeService = new EmployeeService();
//        List<Employee> emps = employeeService.getEmps();
        // contains判断的如果是基本数据类型 比值
        // 如果判断的是引用数据类型 地址
//        System.out.println(emps.contains(e));
    }
}

    

 

 

------------------------------------

------------------------------------

posted on 2022-08-06 21:15  骐琳  阅读(42)  评论(0)    收藏  举报

你点我就回上面去了ヾ(≧O≦)〃嗷~