package project;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
ArrayList array = new ArrayList<>();
load(array);
while (true) {
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("7 退出系统");
//输入选项
String choice = input.nextLine();
switch (choice) {
case "1" -> query(array);
case "2" -> {
add(array);
storeData(array);//修改数据之后立即更新硬盘文件
}
case "3" -> {
modify(array);
storeData(array);//修改数据之后立即更新硬盘文件
}
case "4" -> {
delete(array);
storeData(array);//修改数据之后立即更新硬盘文件
}
case"5" ->{
search(array);
}
case "6"->{
sort(array);
}
case "7" -> {
System.out.println("感谢您的使用,再见!");
System.exit(0);
}
default -> System.out.println("您的输入有误,请重新选择!");
}
}
}
private static void load(ArrayList array) throws IOException {
//初次打开应用会创建文件
File file = new File("D://tea.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("初次启动初始化完成!");
}
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String[] str = line.split("&");
employee s = new employee();
s.setName(str[0]);
s.setSex(str[1]);
s.setBirth(str[2]);
s.setKanum(str[3]);
s.setNum(str[4]);
s.setPhone(str[5]);
s.setDepart(str[6]);
s.setTitle(str[7]);
array.add(s);
}
br.close();
}
//储存数据到文件中
private static void storeData(ArrayList array)throws
IOException {
File file = new File("D://tea.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//直接将数据全部存储到文件中,不用追加方法,也方便增删查改
//如何信息量比较大就不适应
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
//将数据存储进文件
for (employee s : array) {
bw.write(s.getName(), 0, s.getName().length());//导出姓名
bw.write('&');//间隔不同数据
bw.write(s.getSex(), 0, s.getSex().length());//导出性别
bw.write('&');//间隔不同数据
bw.write(s.getBirth(), 0, s.getBirth().length());//导出生日
bw.write('&');//间隔不同数据
bw.write(s.getKanum(), 0, s.getKanum().length());//导出身份证号
bw.write('&');
bw.write(s.getNum(), 0, s.getNum().length());//导出工号
bw.write('&');
bw.write(s.getPhone(), 0, s.getPhone().length());//导出手机号
try {
bw.write('&');
} catch (IOException e) {
e.printStackTrace();
}
bw.write(s.getDepart(), 0, s.getDepart().length());//导出部门
bw.write('&');
bw.write(s.getTitle(), 0, s.getTitle().length());//导出职称55
bw.newLine();
}
bw.flush();
bw.close();
}
private static void query(ArrayList array) {
if (array.size() == 0) {
System.out.println("暂时未存储任何职工信息,请重新选择。");
return;
}
for (employee s : array) {
System.out.println("工号:"+s.getNum()+" "+"姓名:"+s.getName()+" "+"性别:"+s.getSex()+" "+"出生日期:"+s.getBirth()+" "+"身份证号码:"+s.getKanum()+" "+"电话号码:"+s.getPhone()+" "+"部门:"+s.getDepart()+" "+"职称:"+s.getTitle());
}
}
private static void add(ArrayList array) {
Scanner input = new Scanner(System.in);
String kanum;
while (true) {
System.out.println("请输入新职工的身份证号");
kanum = input.nextLine();
//判断是否重复职工
boolean flag = false;//默认未重复
for (employee s : array) {
if (s.getKanum().equals(kanum)) {
System.out.println("该职工已经存在,请重新输入!");
flag = true;
break;
}
}
if (!flag) {
break;
}
}
System.out.println("请输入新职工姓名:");
String name = input.nextLine();
System.out.println("请输入新职工性别:");
String sex = input.nextLine();
System.out.println("请输入新职工出生日期:");
String birth = input.nextLine();
System.out.println("请输入新职工工号:");
String Num = input.nextLine();
System.out.println("请输入新职工手机号:");
String phone = input.nextLine();
System.out.println("请输入新职工部门:");
String depart = input.nextLine();
System.out.println("请输入新职工职称:");
String title = input.nextLine();
employee s = new employee();
s.setKanum(kanum);
s.setName(name);
s.setSex(sex);
s.setBirth(birth);
s.setNum(Num);
s.setPhone(phone);
s.setDepart(depart);
s.setTitle(title);
array.add(s);
System.out.println("添加职工信息成功!");
}
private static void modify(ArrayList array) {
Scanner input = new Scanner(System.in);
int index = -1;//查找标志
System.out.println("请输入需要修正的职工的工号:");
String num = input.nextLine();
for (int i = 0; i < array.size(); i++) {
employee s = array.get(i);
if (s.getNum().equals(num)) {
index = i;
break;
}
}
if (index == -1) {//未找到学生直接返回
System.out.println("未找到相应工号所对应职工!");
} else {
System.out.println("请输入新职工姓名:");
String name = input.nextLine();
System.out.println("请输入新职工性别:");
String sex = input.nextLine();
System.out.println("请输入新职工出生日期:");
String birth = input.nextLine();
System.out.println("请输入新职工身份证号:");
String kanum = input.nextLine();
System.out.println("请输入新职工手机号:");
String phone = input.nextLine();
System.out.println("请输入新职工部门:");
String depart = input.nextLine();
System.out.println("请输入新职工职称:");
String title = input.nextLine();
employee s = array.get(Integer.parseInt(String.valueOf(index)));
s.setKanum(kanum);
s.setName(name);
s.setSex(sex);
s.setBirth(birth);
s.setNum(num);
s.setPhone(phone);
s.setDepart(depart);
s.setTitle(title);
System.out.println("职工信息更新成功!");
}
}
private static void delete(ArrayList array) {
Scanner input = new Scanner(System.in);
System.out.println("请输入需要删除的职工工号:");
String num = input.nextLine();
for (int i = 0; i < array.size(); i++) {
employee s = array.get(i);
if (s.getNum().equals(num)) {
System.out.println("请确认删除信息(Y/N):");
System.out.println("工号:"+s.getNum()+" "+"姓名:"+s.getName()+" "+"性别:"+s.getSex()+" "+"出生日期:"+s.getBirth()+" "+"身份证号码:"+s.getKanum()+" "+"电话号码:"+s.getPhone()+" "+"部门:"+s.getDepart()+" "+"职称:"+s.getTitle());
String confirm = input.nextLine();
//判断确认删除
if (confirm.equals("y") || confirm.equals("Y")) {
if (array.remove(s)) {
System.out.println("删除成功!");
return;
}
} else {
System.out.println("已取消删除!");
return;
}
}
}
System.out.println("未找到所对应职工工号,请确认职工信息是否有误!");
}
private static void search(ArrayList array){
Scanner input=new Scanner(System.in);
while(true){
int i;
System.out.println("-------------------------");
System.out.println("| 1.工号查询 |");
System.out.println("| 2.姓名查询 |");
System.out.println("| 3.部门查询 |");
System.out.println("| 4.计算各部各职称的人数 |");
System.out.println("| 0.退出查询 |");
System.out.println("-------------------------");
switch(i=input.nextInt()){
case 1:Acg(array);break;
case 2:Acx(array);break;
case 3:Ack(array);break;
case 4:Acp(array);break;
case 0:
try {
main(null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
protected static void Acg(ArrayList array){
Scanner input = new Scanner(System.in);
int index = -1;//查找标志
System.out.println("请输入需要查找职工的工号:");
String num = input.nextLine();
for (int i = 0; i < array.size(); i++) {
employee s = array.get(i);
if (s.getName().equals(num)) {
index = i;
break;
}
}
if (index == -1) {//未找到职工直接返回
System.out.println("未找到相应工号所对应职工!");
} else {
employee s = array.get(index);
System.out.println("工号:"+s.getNum()+" "+"姓名:"+s.getName()+" "+"性别:"+s.getSex()+" "+"出生日期:"+s.getBirth()+" "+"身份证号码:"+s.getKanum()+" "+"电话号码:"+s.getPhone()+" "+"部门:"+s.getDepart()+" "+"职称:"+s.getTitle());
}
}
protected static void Acx(ArrayList array){
Scanner input = new Scanner(System.in);
int index = -1;//查找标志
System.out.println("请输入需要查找职工的姓名:");
String name= input.nextLine();
for (int i = 0; i < array.size(); i++) {
employee s = array.get(i);
if (s.getName().equals(name)) {
index = i;
break;
}
}
if (index == -1) {//未找到职工直接返回
System.out.println("未找到相应姓名所对应职工!");
} else {
employee s = array.get(index);
System.out.println("工号:"+s.getNum()+" "+"姓名:"+s.getName()+" "+"性别:"+s.getSex()+" "+"出生日期:"+s.getBirth()+" "+"身份证号码:"+s.getKanum()+" "+"电话号码:"+s.getPhone()+" "+"部门:"+s.getDepart()+" "+"职称:"+s.getTitle());
}
}
protected static void Ack(ArrayList array){
Scanner input = new Scanner(System.in);
System.out.println("请输入需要查找职工的部门:");
String depart= input.nextLine();
for (int i = 0; i < array.size(); i++) {
employee s = array.get(i);
if (s.getDepart().equals(depart)) {
System.out.println("工号:" + s.getNum() + " " + "姓名:" + s.getName() + " " + "性别:" + s.getSex() + " " + "出生日期:" + s.getBirth() + " " + "身份证号码:" + s.getKanum() + " " + "电话号码:" + s.getPhone() + " " + "部门:" + s.getDepart() + " " + "职称:" + s.getTitle());
}else{
System.out.println("没有找到该部门,已返回界面·");
}
}
}
protected static void Acp(ArrayList array){
Scanner input = new Scanner(System.in);
int k=0;
System.out.println("请输入需要统计职工的部门:");
String depart= input.nextLine();
System.out.println("请输入需要统计的该部门的职称:");
String title=input.nextLine();
for (int i = 0; i < array.size(); i++) {
employee s = array.get(i);
if (s.getDepart().equals(depart)&&s.getTitle().equals(title)) {
k++;
}
}
System.out.println("该部门的"+title+"有"+k+"人");
}
protected static void sort(ArrayListarray){
for (int i = 0; i < array.size(); i++) {
employee s = array.get(i);
if (s.getTitle().equals("教授")) {
System.out.println("工号:" + s.getNum() + " " + "姓名:" + s.getName() + " " + "性别:" + s.getSex() + " " + "出生日期:" + s.getBirth() + " " + "身份证号码:" + s.getKanum() + " " + "电话号码:" + s.getPhone() + " " + "部门:" + s.getDepart() + " " + "职称:" + s.getTitle());
}}
for (int i = 0; i < array.size(); i++) {
employee s = array.get(i);
if (s.getTitle().equals("副教授")){
System.out.println("工号:" + s.getNum() + " " + "姓名:" + s.getName() + " " + "性别:" + s.getSex() + " " + "出生日期:" + s.getBirth() + " " + "身份证号码:" + s.getKanum() + " " + "电话号码:" + s.getPhone() + " " + "部门:" + s.getDepart() + " " + "职称:" + s.getTitle());
}}
for (int i = 0; i < array.size(); i++) {
employee s = array.get(i);
if (s.getTitle().equals("讲师")){
System.out.println("工号:" + s.getNum() + " " + "姓名:" + s.getName() + " " + "性别:" + s.getSex() + " " + "出生日期:" + s.getBirth() + " " + "身份证号码:" + s.getKanum() + " " + "电话号码:" + s.getPhone() + " " + "部门:" + s.getDepart() + " " + "职称:" + s.getTitle());
} }
for (int i = 0; i < array.size(); i++) {
employee s = array.get(i);
if (s.getTitle().equals("助教")){
System.out.println("工号:" + s.getNum() + " " + "姓名:" + s.getName() + " " + "性别:" + s.getSex() + " " + "出生日期:" + s.getBirth() + " " + "身份证号码:" + s.getKanum() + " " + "电话号码:" + s.getPhone() + " " + "部门:" + s.getDepart() + " " + "职称:" + s.getTitle());
}}
}
}
class data {
private String time;
public data(String a) {
time = a;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
class person {
private String name;
private String sex;
private String birth;
private String kanum;
public person() {
}
public person(String name, String sex, String birth, String kanum) {
this.name = name;
this.sex = sex;
this.birth = birth;
this.kanum = kanum;
}
public void setBirth(String birth) {
this.birth = birth;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirth() {
return birth;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public String getKanum() {
return kanum;
}
public void setKanum(String kanum) {
this.kanum = kanum;
}
}
class employee extends person {
private String num;
private String phone;
private String depart;
private String title;
public void setDepart(String depart) {
this.depart = depart;
}
public void setTitle(String title) {
this.title = title;
}
public void setNum(String num) {
this.num = num;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getDepart() {
return depart;
}
public String getNum() {
return num;
}
public String getPhone() {
return phone;
}
public String getTitle() {
return title;
}
}