问题描述
importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;importjava.io.Serializable;importjava.util.ArrayList;importjava.util.Scanner;publicclassTest{publicstaticvoidmain(String[]args)throwsException{Manager.start();}}classManager{privatefinalstaticManagerm=newManager();privateManager(){}publicManagergetMan(){returnm;}privatestaticArrayList<Person>list;//启动首页publicstaticvoidstart()throwsException{for(inti=1;i<51;i++){System.out.print("=");}System.out.println();System.out.println("[1]查询所有联系人");System.out.println("[2]增加联系人信息");System.out.println("[3]删除联系人信息");System.out.println("[4]修改联系人信息");for(inti=1;i<51;i++){System.out.print("=");}System.out.println();while(true){System.out.println("请选择:");Scannersc=newScanner(System.in);intsel=sc.nextInt();switch(sel){case1:look();break;case2:addCon();break;case3:del();break;case4:res();break;default:continue;}}}staticObjectOutputStreamoos;staticObjectInputStreamois;//增加联系人信息privatestaticvoidaddCon()throwsException{list.clear();Scannersc=newScanner(System.in);Personp=newPerson();Filefile=newFile("Contacts.txt");oos=newObjectOutputStream(newFileOutputStream(file));ois=newObjectInputStream(newFileInputStream(file));if(file.length()!=0){list=(ArrayList<Person>)ois.readObject();for(Personp1:list){System.out.println(p1);}}//联系人信息:编号、姓名、年龄、QQ、电话、邮箱while(true){System.out.println("请输入编号:");p.setId(sc.next());System.out.println("请输入姓名:");//。。。。。。。。。。。。。。。。。p.setName(sc.next());while(true){System.out.println("请输入年龄:");p.setAge(sc.nextInt());if(p.getAge()>0&&p.getAge()<130){break;}else{System.out.println("输入有误,请重新输入年龄!");continue;}}System.out.println("请输入QQ:");p.setQq(sc.nextLong());System.out.println("请输入联系号码:");p.setTel(sc.next());while(true){System.out.println("请输入邮箱:");p.setEmail(sc.next());if(p.getEmail().matches("\w+@\w+\.\w+")){break;}else{System.out.println("邮箱输入有误,请重新输入!");continue;}}System.out.println(p);while(true){System.out.println("是否保存该联系人信息:【1】是【2】否");intsele=sc.nextInt();if(sele==1){list.add(p);break;}elseif(sele==2){System.out.print("取消保存");sleep();}else{System.out.println("输入有误!");continue;}}System.out.println("是否继续添加联系人?");System.out.println("【1】是【2】否");intcon=sc.nextInt();if(con==1){continue;}else{oos.writeObject(list);System.out.println("退出至首页");sleep();oos.close();ois.close();start();}}}//查询privatestaticvoidlook(){}//删除privatestaticvoiddel(){}//修改privatestaticvoidres(){}//sleepprivatestaticvoidsleep(){for(inti=1;i<7;i++){System.out.print(".");try{Thread.sleep(100);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblockthrownewRuntimeException(e);}}System.out.println();}}classPersonimplementsSerializable{privateStringid;privateStringname;privateintage;privatelongqq;privateStringtel;privateStringemail;Person(){}publicPerson(Stringid,Stringname,intage,longqq,Stringtel,Stringemail){super();this.id=id;this.name=name;this.age=age;this.qq=qq;this.tel=tel;this.email=email;}publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publiclonggetQq(){returnqq;}publicvoidsetQq(longqq){this.qq=qq;}publicStringgetTel(){returntel;}publicvoidsetTel(Stringtel){this.tel=tel;}publicStringgetEmail(){returnemail;}publicvoidsetEmail(Stringemail){this.email=email;}@OverridepublicStringtoString(){return"Person[id="+id+",name="+name+",age="+age+",qq="+qq+",tel="+tel+",email="+email+"]";}}