未加入数据库,为用数组实现,也未加入界面显示,用的均为控制台操作!
第一部分,该部分为储存人员信息的类。
package xinxi;
/**
* 入职人信息
*/
public class xinxi {
private String number; //编号
private String name; //姓名
private String sex; //性别
private int age; //年龄
private double wages; //工资
private String address; //住址
public xinxi(String number, String name, String sex, int age, double wages, String address) {
super();
this.number = number;
this.name = name;
this.sex = sex;
this.age = age;
this.wages = wages;
this.address = address;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWages() {
return wages;
}
public void setWages(double wages) {
this.wages = wages;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
第二部分,该部分储存一个xinxi类和姓名编号两个变量。
package xinxi;
/**
* 构筑name->xinxi
* number->xinxi
* 的对应关系
*
*/
public class user{
private String name; //姓名
private String number; //编号
private xinxi xinxi;
public user(String name, String number, xinxi xinxi) {
super();
this.name = name;
this.number = number;
this.xinxi = xinxi;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public xinxi getXinxi() {
return xinxi;
}
public void setXinxi(xinxi xinxi) {
this.xinxi = xinxi;
}
}
第三部分,这部分为工具类,提供搜索和数组拷贝功能。
package tool;
/**
* 底层工具类,服务于管理层
* 工具类一般使用static静态声明
* static 声明的方法可以不通过实例化对象调用
*/
import xinxi.user;
public class tool {
//返回对象的索引位置,未找到返回-1
public static int index(user [] a,String number) {
int i = 0;
for(user t : a) {
if(t==null) {
return -1;
}
if(number.equals(t.getNumber())||number.equals(t.getName())) {
return i;
}
i++;
}
return -1;
}
//数组拷贝
public static user[] copy(user [] x,int t,user [] y,int q,int length) {
for(int i = t;i
第四部分,该部分为实现增删改查的底层代码,提供了添加时的自动扩容功能,默认初始数组大小为10,也可自己指定大小。
package guanli;
/**
* 程序执行的核心
*/
import tool.tool;
import xinxi.user;
public class guanli {
private user [] list;
private int size;
//返回员工总人数
public int size() {
return this.size;
}
//构造器
public guanli() {
this(10);
}
public guanli(int x) {
list=new user[x];
}
//添加
public void add(user x) {
//扩容
if(size>=list.length) {
user [] Alist = new user [size*2+1];
Alist = tool.copy(list, 0, Alist, 0, list.length);
list = Alist;
}
int i = tool.index(list, x.getNumber());
if(i==-1) {
list[size++] =x;
}
else {
System.out.println("该员工已被记录!");
}
}
//查找
public user index(String number) {
int x = tool.index(list, number);
if(x!=-1)
return list[x];
else {
return null;
}
}
//删除
public boolean remove(String number) {
int x = tool.index(list, number);
if(x==-1)
return false;
else {
list = tool.copy(list, x+1, list, x, size-x);
size--;
return true;
}
}
//更改
public boolean set(user x,String number) {
int i = tool.index(list, number);
if(i==-1)
return false;
else {
list[i]=x;
return true;
}
}
}
第五部分,提供控制台显示功能
package tool;
/**
* 基于底层实现增查改删,并提供显示提示
*/
import java.util.Scanner;
import guanli.guanli;
import xinxi.user;
import xinxi.xinxi;
public class Uitool {
guanli guanli = new guanli();
Scanner cin = new Scanner(System.in);
//增
public void add() {
System.out.println("请输入录入员工信息:");
System.out.print("请输入员工编号:");
String number = cin.next();
user flag = guanli.index(number);
if(flag!=null) {
System.out.println("该员工已被记录!");
return;
}
System.out.print("请输入员工姓名:");
String name = cin.next();
System.out.print("请输入员工性别:");
String sex = cin.next();
System.out.print("请输入员工年龄:");
int age = cin.nextInt();
System.out.print("请输入员工月工资:");
double wages = cin.nextDouble();
System.out.print("请输入员工住址:");
String address = cin.next();
guanli.add(new user(name,number,new xinxi(number,name,sex,age,wages,address)));
System.out.println("添加成功!");
}
//查
public void index() {
System.out.print("请输入需要查询员工的编号:");
String number = cin.next();
user x = guanli.index(number);
if(x==null) {
System.out.println("无此职员信息!");
}
else {
System.out.println("该员工信息为:");
System.out.println("编号:"+x.getNumber());
System.out.println("姓名:"+x.getName());
System.out.println("性别:"+x.getXinxi().getSex());
System.out.println("年龄:"+x.getXinxi().getAge());
System.out.println("月工资:"+x.getXinxi().getWages());
System.out.println("住址:"+x.getXinxi().getAddress());
}
}
//显示提醒
public void nuem() {
System.out.println("\t\t\t员工管理系统");
System.out.println("------------------------------------------------------------");
System.out.println("1.添加员工(a)" +"\t" + "2.查询员工(b)"+ "\t" +"3.更改员工信息(c) " + "4.删除员工(d)");
System.out.print("请输入选择:");
}
//改
public void set() {
System.out.print("请输入你需要更改员工的编号:");
String number = cin.next();
user x = guanli.index(number);
if(x==null) {
System.out.println("无此职员信息!");
}
else {
System.out.println("请输入更改后的员工信息:");
System.out.print("请输入员工新编号:");
String number1 = cin.next();
System.out.print("请输入员工姓名:");
String name = cin.next();
System.out.print("请输入员工性别:");
String sex = cin.next();
System.out.print("请输入员工年龄:");
int age = cin.nextInt();
System.out.print("请输入员工月工资:");
double wages = cin.nextDouble();
System.out.print("请输入员工新住址:");
String address = cin.next();
guanli.set(new user(name,number,new xinxi(number1,name,sex,age,wages,address)), number);
System.out.println("更改成功!");
}
}
//删
public void remove() {
System.out.print("请输入你需要删除的员工的编号:");
String number = cin.next();
user x = guanli.index(number);
if(x==null) {
System.out.println("无此职员信息!");
}
else {
guanli.remove(number);
System.out.println("删除成功!");
}
}
}
第六部分,为程序的入口。
package UI;
/**
* UI界面显示,面向使用者
*/
import java.util.Scanner;
import tool.Uitool;
public class UI {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Uitool t = new Uitool();
while(true) {
t.nuem();
String x = cin.next();
if(x.equals("a")) {
t.add();
}
else if(x.equals("b")) {
t.index();
}
else if(x.equals("c")) {
t.set();
}
else if(x.equals("d")) {
t.remove();
}
}
}
}