赞
踩
任务1:ArrayList类的应用 (1)创建一个职工类Employee,其属性有职工编号id(String),姓名name(String),性别sex(char),薪水salary(float),职工编号不能重复。定义属性的get和set的方法。 程序文件名为: Employee.java (2)建立职工类的操作接口EmployeeDao,接口提供下功能: 程序文件名为: EmployeeDao.java 1)添加职工的功能,方法原型为: public boolean addEmployee(Employee employee); 功能是:增加职工对象employee。 返回值:若添加成功,返回true,否则返回false。 2)删除职工的功能,方法原型为: public boolean delEmployee(String id); 功能是:将职工编号为id的职工删除。 返回值:若删除成功,返回true,否则返回false。 3)修改职工的功能,方法原型为: public boolean updateEmployee(String id , Employee employee); 功能是:将职工编号为id的职工对象修改成employee对象。 返回值:若修改成功,返回true,否则返回false。 4)查找职工的功能,方法原型为: public Employee selectById(String id); 功能是:根据职工编号id查找职工。 返回值:若找到,返回该职工对象,否则返回null。 5)检查职工是否存在,方法原型为: public boolean isExist(Employee employee); 功能是:检查该职工是否存在。 返回值:若已经存在,返回true,否则返回null。 6)显示单个职工信息的功能,方法原型为: public void showSinglel(Employee employee) 功能是:显示指定职工employee的信息。 返回值:无。 7)显示所有职工的功能,方法原型为: public void showAll(); 功能是:显示所有职工信息。 返回值:无。 (3)建立职工类操作接口EmployeeDao的实现类EmployeeDaoImpUseOfArrayList,在实现类中用ArrayList类的成员 empList保存职工的信息,实现接口提供下功能。 程序文件名为: EmployeeDaoImpUseOfArrayList.java public EmployeeDaoImp implements EmployeeDao{ private ArrayList empList; //成员变量的get与set方法; //构造方法;初始化成员 empList //接口方法实现...... } (4)职工管理的主程序类EmployeeManager,实现对职工信息的管理。 程序文件名为: EmployeeManager.java 程序功能包括:建立功能菜单;实现功能选择管理;建立职工对象的增加、修改、删除、查询等功能,这些功能提供数据输入与封装,然后调用EmployeeDaoImpUseOfArrayList类的具体方法实现相应功能,显示所有职工等功能 |
- package com;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
-
- class Employee {
- private String id;
- private String name;
- private char sex;
- private float salary;
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public char getSex() {
- return sex;
- }
-
- public void setSex(char sex) {
- this.sex = sex;
- }
-
- public float getSalary() {
- return salary;
- }
-
- public void setSalary(float salary) {
- this.salary = salary;
- }
- }
-
- interface EmployeeDao {
- public boolean addEmployee(Employee employee);
-
- public boolean delEmployee(String id);
-
- public boolean updateEmployee(String id, Employee employee);
-
- public Employee selectById(String id);
-
- public boolean isExist(Employee employee);
-
- public void showSinglel(Employee employee);
-
- public void showAll();
- }
-
- public class EmployeeMessage {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- EmployeeDaoImp daoImp = new EmployeeDaoImp();
- other: while (true) {
- System.out.println("*******************");
- System.out.println("* 欢迎您使用职工管理系统 *");
- System.out.println("* *");
- System.out.println("* 1:添加职工信息 *");
- System.out.println("* 2:删除职工信息 *");
- System.out.println("* 3:修改职工信息 *");
- System.out.println("* 4:查询职工信息 *");
- System.out.println("* 5:显示所有信息 *");
- System.out.println("* 6:结束运行系统 *");
- System.out.println("*******************");
- System.out.print("请选择要操作的功能:");
- int key = in.nextInt();
- System.out.println();
- switch (key) {
- case 1:
- Employee employee = new Employee();
- System.out.println("请输入职工编号:");
- employee.setId(in.next());
- System.out.println("请输入职工姓名:");
- employee.setName(in.next());
- System.out.println("请输入职工性别(M/F):");
- employee.setSex(in.next().charAt(0));
- System.out.println("请输入职工工资:");
- employee.setSalary(in.nextFloat());
- System.out.println(daoImp.addEmployee(employee) ? "添加成功" : "添加失败");
- break;
- case 2:
- System.out.println("请输入要删除的职工编号:");
- System.out.println(daoImp.delEmployee(in.next()) ? "删除成功" : "删除失败");
- break;
- case 3:
- System.out.println("请输入要修改的职工编号:");
- Employee QueryEmployee = daoImp.selectById(in.next());
- if (QueryEmployee == null) {
- System.out.println("该员工不存在!");
- System.out.println("添加失败");
- System.out.println("update");
- } else {
- daoImp.showSinglel(QueryEmployee);
- Employee updateEmployee = new Employee();
- System.out.println("请输入职工信息要修改的信息:");
- System.out.println("请输入修改职工姓名:");
- updateEmployee.setName(in.next());
- System.out.println("请输入修改职工性别:");
- updateEmployee.setSex(in.next().charAt(0));
- System.out.println("请输入修改职工薪水:");
- updateEmployee.setSalary(in.nextFloat());
- System.out.println(daoImp.updateEmployee(QueryEmployee.getId(), updateEmployee) ? "修改成功" : "修改失败");
- System.out.println("update");
- }
- break;
- case 4:
- System.out.println("请输入要查询的职工编号:");
- Employee Queremployee = daoImp.selectById(in.next());
- if (Queremployee == null) {
- System.out.println("该员工不存在!");
- } else {
- daoImp.showSinglel(Queremployee);
- }
- break;
- case 5:
- daoImp.showAll();
- break;
- case 6:
- System.out.println("欢迎您再次使用此程序 Bye~");
- in.close();
- break other;
- }
- }
-
- }
- }
-
- class EmployeeDaoImp implements EmployeeDao {
- private List<Employee> employeeList = new ArrayList<Employee>();
-
- @Override
- public boolean addEmployee(Employee employee) {
- // TODO Auto-generated method stub
- if (!isExist(employee)) {
- employeeList.add(employee);
- return true;
- }
- return false;
-
- }
-
- @Override
- public boolean delEmployee(String id) {
- // TODO Auto-generated method stub
- for (int i = 0; i < employeeList.size(); i++) {
- if (employeeList.get(i).getId().equals(id)) {
- employeeList.remove(i);
- return true;
- }
- }
- return false;
- }
-
- @Override
- public boolean updateEmployee(String id, Employee employee) {
- // TODO Auto-generated method stub
- for (int i = 0; i < employeeList.size(); i++) {
- if (employeeList.get(i).getId().equals(id)) {
- employeeList.get(i).setName(employee.getName());
- employeeList.get(i).setSex(employee.getSex());
- employeeList.get(i).setSalary(employee.getSalary());
- return true;
- }
- }
- return false;
- }
-
- @Override
- public Employee selectById(String id) {
- // TODO Auto-generated method stub
- for (int i = 0; i < employeeList.size(); i++) {
- if (employeeList.get(i).getId().equals(id)) {
- Employee employee = new Employee();
- employee.setId(employeeList.get(i).getId());
- employee.setName(employeeList.get(i).getName());
- employee.setSex(employeeList.get(i).getSex());
- employee.setSalary(employeeList.get(i).getSalary());
- return employee;
- }
- }
- return null;
- }
-
- @Override
- public boolean isExist(Employee employee) { // 判断是否存在
- // TODO Auto-generated method stub
- for (int i = 0; i < employeeList.size(); i++)
- if (employeeList.get(i).getId().equals(employee.getId()))
- return true;
- return false;
- }
-
- @Override
- public void showSinglel(Employee employee) {
- // TODO Auto-generated method stub
- System.out.println("职工编号 职工姓名 职工性别 薪水 ");
- System.out.println(employee.getId() + " " + employee.getName() + " " + employee.getSex() + " "
- + employee.getSalary());
- }
-
- @Override
- public void showAll() {
- // TODO Auto-generated method stub
- System.out.println("职工编号 姓名 性别 薪水");
- for (Employee employee : employeeList)
- System.out.println(employee.getId() + " " + employee.getName() + " " + employee.getSex() + " "
- + employee.getSalary());
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。