⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 txl.cpp

📁 建立一个通讯录的类note
💻 CPP
字号:

//////////////////////////通讯录类,
class note implements Serializable{
	 String name,phone,MobilePhone,zip,address,Email;
	public void Setnote(String name,String phone,String MobilePhone,String zip,String address,String Email)
	{
		this.name=name;
		this.phone=phone;
		this.MobilePhone=MobilePhone;
		this.zip=zip;
		this.address=address;
		this.Email=Email;
	}
	public void show()
	{
		System.out.println(name+"  "+phone+"  "+MobilePhone+"   "+zip+"   "+address+"   "+Email+"   ");
	}
	
}
 //程序的公共类实现如下:
/////////////////完成系统功能的类。
public class notebook{
        public  static void main(String args[])throws IOException
        {
 		System.out.print("**************通讯录系统**************\n");
 		System.out.print("1--------------显示通讯录信息!---------\n"
 			            +"2--------------添加通讯记录 ! ---------\n"
 	                	+"3--------------查找记录!      ---------\n"
 	                	+"4--------------删除一条记录    ---------\n"
 	                	+"5--------------删除通讯录的全部记录!---\n"
 	                	+"6--------------退出            ---------\n");	
 	     System.out.print("请选择操作:");  
 	      String s;
          int Cto;       	
 	   BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
 	   s=in.readLine();
 	   Cto=Integer.parseInt(s);
 	  while(Cto>5&&Cto<1){
 	  System.out.print("选择错误的操作,请重新选择!");	
 	  s=in.readLine();
 	  Cto=Integer.parseInt(s);
 	  }
 	  while(Cto>=1&&Cto<=5){
 	  	switch(Cto)
 	  	{
 	  		case 1: 
 ////////////////////////下为显示记录的代码*********/////////////////////////  		
          { 
           int i;
    	   ArrayList Notelist=new ArrayList();	
     try{
        ObjectInputStream infile=new ObjectInputStream(new FileInputStream("note.dat"));
     	Notelist=(ArrayList)infile.readObject();
     	infile.close();	
     	}catch(Exception ex){}
      if(Notelist.size()==0)
      System.out.println("通讯录没有记录");
      else
      { 
  System.out.println("姓名"+"  "+"固定电话"+"  "+"手机      "+"    "+"邮编    "+" "+"地址       "+"    "+"电子邮件地址"+"  ");
	for(i=0;i<Notelist.size();i++)
{
	        note Notebook=new note();  
            Notebook=(note)Notelist.get(i);
	        Notebook.show(); 
}
}	
}	
break;	
///////////////////////////////////////////////////////////////// 	  		
 	  		
/////////////////////////以下为添加通讯记录的的代*///////////////////////////////////
 	  			case 2:   
                       {/////////////////////////////// 
                       ArrayList Notelist=new ArrayList();
                       //////////获得记录文件	                     					
					try{
						ObjectInputStream file=new ObjectInputStream(new FileInputStream("note.dat"));
						Notelist=(ArrayList)file.readObject();
						file.close();
					}catch(Exception ex){}
		int n;
		System.out.print("请输入添加个数n:");
          String s1=in.readLine();
		  n=Integer.parseInt(s1);
		for(int i=1;i<=n;i++)
		{
/////////////////每次添加记录时建立一个对象文件。			
	    FileOutputStream myfile=new FileOutputStream("note.dat");
        ObjectOutputStream Ofile=new ObjectOutputStream(myfile);	
		System.out.println("记录"+i+"的信息");
        String name,phone,MobilePhone,zip,address,Email;
		System.out.print("请输入姓名:");
		name=in.readLine();
		System.out.print("请输如电话号码:");
		phone=in.readLine();
		System.out.print("请输入手机号码:");
		MobilePhone=in.readLine();
		System.out.print("请输入邮编:");
        zip=in.readLine();
		System.out.print("请输入地址:");
        address=in.readLine();
        System.out.print("请输入电子邮箱地址:");
        Email=in.readLine();
		note AddNote=new note();
		AddNote.Setnote(name,phone,MobilePhone,zip,address,Email);
		try{
	                 Notelist.add(AddNote);
				     Ofile.writeObject(Notelist);
		} catch   (Exception ex)    {ex.printStackTrace();}
		Ofile.close();
		myfile.close();	
       	System.out.print("成功添加一条记录!!\n");   
         } 
        
	
		} /////////////////////////////////////
        break;
//////////////////////////////////////////////////////////////////////////
//////////////////////////以下为查找记录的代码//////////////////////////////////                   
 	  			  case 3:
///////////////////
{
	ArrayList Notelist=new ArrayList();	
     String Sname;
 		System.out.print("请输入要查找的姓名:");
		Sname=in.readLine();
		try{
						ObjectInputStream fin=new ObjectInputStream(new FileInputStream("note.dat"));
						Notelist=(ArrayList)fin.readObject();
						fin.close();
					}catch(Exception e){}	
					note SeletNote=new note();
					int i;
					for(i=0;i<Notelist.size();i++)
					{
						SeletNote=(note)Notelist.get(i);
						if(SeletNote.name.equalsIgnoreCase(Sname))
						break;		
					}
					if(Notelist.size()!=0&&i!=Notelist.size())
					{
			 System.out.println("姓名"+"  "+"固定电话"+"  "+"手机      "+"    "+"邮编    "+" "+"地址       "+"    "+"电子邮件地址"+"  ");
				SeletNote.show();
					}
				        else if(i==Notelist.size())
					    System.out.println("通讯录中没有你要的纪录!!");
					    }
/////////////////// 	  			  
                         break;
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////以下为删除一条记录的代码。////////////////////////// 	  	
 	  					case 4:
 ///////////////////
 {
 	int k;
	    ArrayList Notelist=new ArrayList();	
         String Dname;
 		System.out.print("请输入要删除的姓名:");
		Dname=in.readLine();	
	           try{
				  	ObjectInputStream fin=new ObjectInputStream(new FileInputStream("note.dat"));
				  	Notelist=(ArrayList)fin.readObject();
				  	fin.close();
				  }catch(Exception e){}	  
				  note DeletNote=new note();
				  	ObjectOutputStream fout=new ObjectOutputStream(new FileOutputStream("note.dat"));
				  	for(k=0;k<Notelist.size();k++)
				  	{
				  		DeletNote=(note)Notelist.get(k);
				  		if(DeletNote.name.equalsIgnoreCase(Dname))
				  		break;
				  	}
				  	if(Notelist.size()!=0&&k!=Notelist.size())
					  {
					  	Notelist.remove(k);
					   	fout.writeObject(Notelist);
					  	System.out.println("删除成功!");
					  }
					  else if(k==Notelist.size())
					  System.out.println("没有你要删除的记录!!");			  	
			  	} break;			
///////////////////////////////以下为删除全部记录的代码。//////////////////	
case 5:
{
	ArrayList Notelist=new ArrayList();	
try{
	ObjectInputStream deletall=new ObjectInputStream(new FileInputStream("note.dat"));
Notelist=(ArrayList)deletall.readObject();	
deletall.close();
}catch(Exception e){}
try{
ObjectOutputStream deletall=new ObjectOutputStream(new FileOutputStream("note.dat"));
Notelist.clear();
deletall.close();
}catch(Exception e){}
	System.out.println("所有信息已清除!");
			  	break;
			  	}
//////////////////////////////////////////////////////////////
}	////////////switch选择结束	                       	
 	  		System.out.print("请选择新的操作:");  
 	  	s=in.readLine();
 	   Cto=Integer.parseInt(s);	
 	  }
   if(Cto==6)
	 System.out.print("你将退出系统!!");
		}
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -