急急急,跪求帮忙,周五交作业。真的搞不定。求高手帮忙。。万分感谢。。。

2024年11月19日 15:14
有1个网友回答
网友(1):

我简单的给你写了个,(两个类Employer.java--员工信息类,Manager.java--功能处理)
我给你粘在这了,如果你弄不好,我把项目打包发给你。
Manager:
public class Manager {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("-------欢迎使用本系统--------");
System.out.println("输入员工信息(1);保存信息(2);查看信息(3);退出(4)");
Employer emp = new Employer();
while(true){
System.out.println("请选择对应数字进行操作:");
int num = sc.nextInt();
switch(num){
case 1:emp = insertInfo();break;
case 2:saveInfo(emp);break;
case 3:showInfo();break;
case 4:quitSystem();break;
default:return;
}
}

}

private static Employer insertInfo() {
Employer emp = new Employer();
Scanner sc = new Scanner(System.in);
System.out.println("输入员工名:");
String str = sc.nextLine();
emp.setName(str);
System.out.println("输入职位:");
str = sc.nextLine();
emp.setPosition(str);
System.out.println("输入薪资:");
str = sc.nextLine();
emp.setSalary(str);
return emp;
}

private static void saveInfo(Employer emp) {
try {
String path = Manager.class.getClassLoader().getResource(
"employer.txt").getPath();
BufferedWriter out = new BufferedWriter(new FileWriter(path, true));
out.write(emp.getName() + "," + emp.getPosition() + ","
+ emp.getSalary());
out.newLine();// 换行
out.flush();
out.close();
System.out.println("保存成功!");
} catch (IOException e) {
e.printStackTrace();
}
}

private static void showInfo() {
String path = Manager.class.getClassLoader().getResource(
"employer.txt").getPath();
try {
BufferedReader in = new BufferedReader(new FileReader(path));
String empStr = "";
System.out.println("姓名\t\t职位\t\t薪资");
while((empStr=in.readLine())!=null){
String[] emps = empStr.split(",");
System.out.println(emps[0]+"\t\t"+emps[1]+"\t\t"+emps[2]);
};
} catch (Exception e) {
e.printStackTrace();
}
}

private static void quitSystem() {
System.out.println("已退出系统!");
System.exit(0);
}
}

Employer:
public class Employer {
private String name;
private String position;
private String salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}

}