qq_57415822
2021-12-10 15:06
采纳率: 50%
浏览 31

读写数据错误,无法将类读入文件中,也无法读出文件

package lx;
import java.util.*;
import java.io.*;
import java.util.Calendar;
import java.util.regex.*;
import java.io.Serializable;

class Staff implements Serializable{
    private static final long serialVersionUID=1L;
    Scanner in=new Scanner(System.in);
    Scanner inStr=new Scanner(System.in);
    String staffNo;
    String staffName;
    int staffClass;
    String staffPosition;
    double basicSalary;
    
    public static int staticNo;
    int salaryNum;
    public Staff(int maxNum) {
        staticNo=maxNum;
        staticNo++;
        staffNo=Integer.toString(staticNo/100)
                +Integer.toString(staticNo%100/10)
                +Integer.toString(staticNo%10);
    
        salaryNum=-1;
    }
    public String getStaffNo() {
        return staffNo;
    }
    public void setStaffNo(String staffNo) {
        this.staffNo=staffNo;
    }
    public String getStaffName() {
        return staffName;
    }
    public int getStaffClass() {
        return staffClass;
    }
    public void setStaffClass(int staffClass) {
        this.staffClass=staffClass;
    }
    public String getStaffClass(int staffClass) {
        return staffPosition;
    }
    public void setStaffName(String staffName) {
        this.staffName=staffName;
    }
    public void setStaffPosition(String staffPosition) {
        this.staffPosition=staffPosition;
    }
    public double getBasicSalary() {
        return basicSalary;
    }
    public void setBasicSalary(double basicSalary) {
        this.basicSalary=basicSalary;
    }
    public int getSalaryNum() {
        return salaryNum;
    }
    public void setSalaryNum(int salaryNum) {
        this.salaryNum=salaryNum;
    }
    public void shuru() {
        
        System.out.print("请输入员工姓名");
        staffName=inStr.nextLine();
        System.out.print("请输入员工类别(1-行政人员;2-技术人员;3-销售人员)");
        while(true) {
            try {
        Scanner in=new Scanner(System.in);
        staffClass=in.nextInt();
        if(staffClass!=1&&staffClass!=2&&staffClass!=3) 
            System.out.print("无效操作,请重新输入");
            
        else break;
            }catch(Exception e) {
                System.out.print("岗位输入有误,请重新输入");
            }
        }
        System.out.print("请输入员工职位");
        staffPosition=inStr.nextLine();
        System.out.print("请输入员工基本工资");
        while(true) {
            int i=0;
            try {
                
            Scanner in=new Scanner(System.in);
        basicSalary=in.nextDouble();
        
            }catch(Exception e) {
                System.out.print("工资输入异常,请重新输入");
                i=1;
            }
            if(i==1) ;
            else break;
        }
    }
    public void showqk() {
        System.out.println("工号   姓名、岗位类别、员工职位、基本工资");
        System.out.println(staffNo+"\t"+staffName+"\t"+staffClass+"\t"+staffPosition+"\t"+basicSalary);
    }
    
}

class Menu{
public int menu() {
    
    Scanner in=new Scanner(System.in);
    System.out.println("新科技公司员工工资管理系统");
    System.out.println("********************************************************************");
    System.out.println("1.基本信息增加  6.工资信息增加");
    System.out.println("2.基本信息删除  7.工资信息删除");
    System.out.println("3.基本信息修改  8.工资信息修改");
    System.out.println("4.基本信息查询  9.工资信息查询");
    System.out.println("5.基本信息显示  10.工资信息显示");
    System.out.println("0.退出");
    System.out.println("********************************************************************");
    System.out.println("请选择操作编号:");
    int choice=in.nextInt();
    while(choice<0||choice>10) {
        System.out.println("无效操作,请重新输入操作编号:");
        choice=in.nextInt();
    }
    return choice;
    }
public  void selectOperat(StaffSet staffSet) {
    int choice;
    choice=menu();
    switch(choice) {
    case 1:{staffSet.addStaff();break;
    }
    case 2:{staffSet.deleteStaff();break;
    }
    case 3:{staffSet.editStaff();break;
    }
    case 4:{staffSet.queryStaff();break;
    }
    case 5:{staffSet.showStaff();break;
    }
    case 6:{staffSet.addStaffSalary();break;
    }
    case 7:{staffSet.deleteStaffSalary();break;
    }
    case 8:{staffSet.editStaffSalary();break;
    }
    case 9:{staffSet.queryStaffSalary();break;
    }
    case 10:{staffSet.showStaffSalary();break;
    }
    case 0:{staffSet.writeData(staffSet);
        System.out.println("感谢使用本系统");
    System.exit(0);
    }
    }
    System.out.println("按任意键继续");
    Scanner in=new Scanner(System.in);
    in.nextLine();
    selectOperat(staffSet);
}
}
class StaffSet implements Serializable{
    private static final long serialVersionUID=1L;
    Staff staff[];
    int staffNum;
    int staffmax;
    public StaffSet() {
        staff=new Staff[100];
        staffNum=-1;
        staffmax=0;
    }
    public void writeData (StaffSet staffSet) {
        
        try{
            ObjectOutputStream os=null;
            os=new ObjectOutputStream(
                    new FileOutputStream("staffData.txt"));
            os.writeObject(staffSet);
            os.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public StaffSet readData(StaffSet tempStaffSet) {
        ObjectInputStream is=null;
        Object temp=null;
        File file=null;
        try {
            file=new File("staffData.txt");
                is=new ObjectInputStream(new FileInputStream(file));
                        temp=is.readObject();
                        if(temp!=null)
                            tempStaffSet=(StaffSet)temp;
                        if (is!=null) is.close();
            
        }catch(IOException e) {
        e.printStackTrace();
        }catch(ClassNotFoundException e) {
            System.out.println("数据格式不对!");
        }
        return tempStaffSet;
    }
    public void addStaffSalary() {
        
    }
    public void deleteStaffSalary() {
        
    }
    public void editStaffSalary() {
    
    }
    public void queryStaffSalary() {
    
    }
    public void showStaffSalary() {
        
    }
    public void addStaff() {
        boolean flag=true;
        while(flag&&staffNum<staff.length)
         {
            staffNum++;
            staff[staffNum]=new Staff(staffmax);
            staff[staffNum].shuru();
            staffmax++;
            System.out.println("按0键退出,其他键继续");
            Scanner inStr=new Scanner(System.in);
            String s=inStr.nextLine();
            if(s.length()==0) flag=true;
            else
                if((s.charAt(0)=='0')) flag=false;
                else flag=true;
        
        }
    }
    
    public void deleteStaff() {
        Scanner inStr=new Scanner(System.in);
        System.out.println("请输入要删除的工号:");
        String staffNo=inStr.nextLine();
        int index=indexOf(staffNo);
        if(index==-1) {
            System.out.println("不存在员工工号为"+staffNo+"的员工");
            return;
        }
        for(int i=index;i<=staffNum;i++) {
            staff[i]=staff[i+1];
        }
        System.out.println("员工工号"+staffNo+"已被删除");
        staffNum--;
    }
    public void editStaff() {
        Scanner inStr=new Scanner(System.in);
        Scanner in=new Scanner(System.in);
        System.out.print("请输入要修改的员工工号:");
        String staffNo=inStr.nextLine();
        int index=indexOf(staffNo);
        if(index==-1) {
            System.out.println("不存在员工工号为:"+staffNo+"的员工");
            return ;
        }
        staff[index].showqk();
        System.out.println("请修改相应属性值,如果不修改,则直接回车");
        System.out.println("姓名:");
        String staffName=inStr.nextLine();
        if(!staffName.isEmpty()) staff[index].staffName=staffName;
        System.out.println("岗位类别:(1-行政人员;2-技术人员;3-销售人员");
        String staffClass=inStr.nextLine();
        if(!staffClass.isEmpty()) {
            while(staffClass.charAt(0)!='1'&&staffClass.charAt(0)!='2'&&staffClass.charAt(0)!='3') {
                System.out.println("无效操作,请重新输入");
                staffClass=inStr.nextLine();
            }
            staff[index].staffClass=staffClass.charAt(0)-48;
        }
        System.out.println("员工职位:");
        String staffPosition=inStr.nextLine();
        if(!staffPosition.isEmpty())
            staff[index].staffPosition=staffPosition;
        String basicSalary=inStr.nextLine();
        if(!basicSalary.isEmpty()) {
            while(Double.parseDouble(basicSalary)<0) {
                System.out.println("基本工资不能小于0!请多给点");
                basicSalary=inStr.nextLine();
            }
            staff[index].basicSalary=Double.parseDouble(basicSalary);
            
        }
        
    }
    public void queryStaff() {
        Scanner inStr=new Scanner(System.in);
        System.out.println("请输入员工工号:");
        String staffNo=inStr.nextLine();
        int index=indexOf(staffNo);
        if(index==-1) {
            System.out.println("不存在员工工号为"+staffNo+"的员工");
            return ;
        }
        staff[index].showqk();
    }
    public void showStaff() {
        System.out.println("员工信息:");
        for(int i=0;i<=staffNum;i++) {
            staff[i].showqk();
        }
    }
    public int indexOf(String staffNo) {
        int t;
        int i;
        for( i=0;i<=staffNum;i++) {
            if(staff[i].staffNo.equals(staffNo)) {
                return i;

            }
        }
        return -1;
    }
}
public class 系统{
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StaffSet staffSet=new StaffSet();
        staffSet=staffSet.readData(staffSet);
        Menu menu=new Menu();
        menu.selectOperat(staffSet);
    
 }
}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题