相关顺序表代码链接:Java数据结构与算法-线性表_ly_better的博客-CSDN博客
设有一个公司职员薪水管理系统,要求完成:
- 可以增加新员工
- 可以根据员工的工号查找员工的详细信息
- 可以限时全部的员工信息
- 可以根据给定的员工修改员工的薪水
- 当员工离职时,将该员工从管理系统中删除
package experiment1;
import java.util.Scanner;
public class staff {
private int id;//声明员工工号
private String name;//声明员工姓名
private int salary;//声明员工工资
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
//无参数时的构造函数
public staff(){
this(0,null,0);
}
//有参数时的构造函数
public staff(int id,String name,int salary){
this.id =id;
this.name = name;
this.salary = salary;
}
public staff(Scanner sc){
this(sc.nextInt(),sc.next(),sc.nextInt());
}
}
package experiment1;
import java.util.Scanner;
import experiment1.staff;
public class StaffManageMent extends SqList{
//按顺序表构造顺序表,其中参数maxSize指的是顺序表的最大储存空间容量
public StaffManageMent(int maxSize,int n)throws Exception {
super(maxSize);
Scanner sc =new Scanner(System.in);
for(int i=1;i<=n;i++){//创建含有n个数据元素的顺序表
staff node=new staff(sc);
if(node!=null)
insert(node);//将新的数据元素插入顺序表的表尾
else
i--;//若不成功,则不计数
}
}
//覆盖父类的get方法,可以根据员工的工号查找员工的详细信息
public staff get(int number)throws Exception{
for(int i=0;i<length();i++){//遍历顺序表
staff node=(staff) super.get(i);//调用父类的get方法
if(node.id==number)
return node;//包含指定的工号,返回该员工的信息
}
throw new Exception("学号"+number+"不存在");//抛出异常
}
//重载了父类的insert方法,可以增加新员工
public void insert (staff node)throws Exception{
super.insert(this.length(),node);//调用父类的insert方法
}
//覆盖父类的remove方法,当员工离职时,将该员工从管理系统中删除
public void remove(int number)throws Exception{
for (int i =0;i<length();i++){//遍历顺序表
staff node =(staff)super.get(i);//取出第i项
if(node.id==number){
super.remove(i);去除第i项
return;
}
}
throw new Exception("工号"+number+"不存在");
}
//重载父类display方法,输出顺序表中的所有数据元素
public void display(){
for(int i=0;i<length();i++){//遍历顺序表
try{
staff node=(staff)super.get(i);
displayNode(node);
}catch(Exception e){}
}
}
//输出一个数据元素的信息,输出全部的员工信息
public void displayNode(staff node){
System.out.println("学号:"+node.id+"姓名:"+node.name+"薪水:"+node.salary
);
}
//给指定员工修改员工薪水
public void xiugai(int number) throws Exception{
for (int i =0;i<length();i++){
staff node =(staff)super.get(i);
if(node.getId()==number){
Scanner sc=new Scanner(System.in);
System.out.println("输入修改员工的薪水:");
int s=sc.nextInt();
node.setSalary(s);
return;
}}
throw new Exception("工号"+number+"不存在");
}}
package experiment1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception {
int maxSize =1000;
Scanner sc=new Scanner(System.in);
System.out.println("请输入员工的总数:");
int n=sc.nextInt();
System.out.println("工号、姓名、薪水的顺序输入员工信息:");
StaffManageMent L=new StaffManageMent(maxSize,n);
L.display();
System.out.println("请输入需要增加的员工的信息:");
L.insert(new staff(sc));
L.display();
System.out.println("请输入需要查找员工的工号:");
L.displayNode(L.get(sc.nextInt()));
System.out.println("请输入需要删除员工的工号:");
L.remove(sc.nextInt());
System.out.println("删除成功");
L.display();
}
}
System.out.println("请输入需要修改员工的工资的工号:");
L.xiugai(sc.nextInt());
System.out.println("修改成功");
L.display();}
运行结果如下: