Java作业 简单通讯录

这篇博客介绍了一个Java作业——创建一个简单的通讯录系统。系统包括储存、访问、修改、删除和导出通讯录功能。博客中定义了一个名片类,包含电话、姓名等属性,并实现了相关方法。此外,还使用ArrayList存储名片类实例,并通过file类处理文件读写。博主表示这是初次尝试,后续会继续学习和改进。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java作业 简单通讯录

#作业要求作业要求
1.需求分析

  • 储存 :将使用者输入的字符或数字储存到对应变量里/且将这些字符或数字封装到一个类里并成为类的属性(数据域)

  • 访问 :可以让使用者通过某个检索关键字检索到目标,检索到后进行输出

  • 修改 :使用者输入特定的关键字进行检索,确定修改项后令使用者进行修改

  • 删除 :使用者输入特定的关键字进行检索,确定删除项后令使用者进行删除操作

  • 导出至文件 :使用者根据提示后选择进行导出,此时系统根据使用者已输入的通讯录各个数据创建一个储存这些数据的文件在特定的目录下

2.设计
💦定义一个【名片类】把号码[long 类,int存不下]、姓名[string 类]、性别[string 类]、工作地点[string 类]、通讯地址[string 类]、生日[string 类]、邮箱[string 类]、备注[string 类] 这些属性封装,然后构造了各个属性对应的储存[set]与输出方法[get],以便名片类里的数据储存与调用。接着定义一个名片类的有参构造,让通讯录主程序可以对名片类进行操作。重写ToString方法满足通讯录主程序的输出需求。
💦一个【file 类】,对应通讯录读写文件的需求
💦一个【ArrayList】储存【名片类】

3.代码
(1)名片类

package 通讯录;
public class 名片 {
	//声明名片的各个属性,储存号码用的Number也可以声明为String类型
	private long[] Number=new long[10];
	private String Name="无",Workad="无",Conect="无",Youxiang="无",Firstday="无",Others="无",sex="无";

	//赋值与打印各个属性的方法
	public long getCode1() {
		return Number[0];
	}
	public void setCode1(long code1) {
		this.Number[0] = code1;
	}
	public long getCode2() {
		return Number[1];
	}
	public void setCode2(long code2) {
		this.Number[1] = code2;
	}
	public long getCode3() {
		return Number[2];
	}
	public void setCode3(long code3) {
		this.Number[2] = code3;
	}
	public long getCode4() {
		return Number[3];
	}
	public void setCode4(long code4) {
		this.Number[3] = code4;
	}
	
	public long getCode5() {
		return Number[4];
	}
	public void setCode5(long code5) {
		this.Number[4] = code5;
	}
	public long getCode6() {
		return Number[5];
	}
	public void setCode6(long code6) {
		this.Number[5] = code6;
	}
	public long getCode7() {
		return Number[6];
	}
	public void setCode7(long code7) {
		this.Number[6] = code7;
	}
	public long getCode8() {
		return Number[7];
	}
	public void setCode8(long code8) {
		this.Number[7] = code8;
	}
	public long getCode9() {
		return Number[8];
	}
	public void setCode9(long code9) {
		this.Number[8] = code9;
	}
	public long getCode10() {
		return Number[9];
	}
	public void setCode10(long code10) {
		this.Number[9] = code10;
	}
	//10 个电话号码
	
	public String getFirstday() {
		return Firstday;
	}
	public void setBrithday(String Firstday) {
		this.Firstday = Firstday;
	}//about birthday
	
	public String getWorkplacce() {
		return Workad;
	}
	public void setWorkplacce(String Workad) {
		this.Workad = Workad;
	}//工作地址
	
	
	public String getTxdz() {
		return Conect;
	}
	public void setTxdz(String Conect) {
		this.Conect = Conect;
	}//通讯地址
	
	
	public String getbeizhu() {
		return Others;
	}
	public void setbeizhu(String Others) {
		this.Others = Others;
	}//备注项
	
	public  String getname() {
		return Name;
	}
	public void Setname(String name ) {
		this.Name=name;
	}
	//获取名字
	
	public void setsex (String sex) {
		this.sex=sex;
	}
	public  String getsex() {
		return sex;
	}//获取性别
	
	public void setYouxiang (String youxiang) {
		this.Youxiang=youxiang;
	}
	public  String getYouxiang() {
		return Youxiang;
	}//获取邮箱
	
	public 名片(String name,long number,String sex,String workspace,String conect,String youxiang,String firstday,String others) {
		this.Name=name;
		this.Number[0]=number;
		this.sex=sex;
		this.Workad=workspace;
		this.Conect=conect;
		this.Youxiang=youxiang;
		this.Others=others;
	}
	public String ToString() {
		return "姓名:"+Name+"\n"+"性别:"+sex+"\n"+"工作地址:"+Workad+"\n"+"通讯地址:"+Conect+"\n"+"生日:"+Firstday+"\n"+"备注:"+Others+"\n";
				
	}
}

(2)主程序

package 通讯录;

import java.util.*;
import java.io.*;
public class TelePhone {

	public static void main(String[] args)throws java.io.IOException {
		java.io.File file =new java.io.File("d://程序设计作业通讯录.txt");
		if(!file.exists()) 
		{
			file.createNewFile();
		}
		
		Scanner haha=new Scanner(file);
		Scanner input= new Scanner(System.in);
		ArrayList<名片> LB=new  ArrayList<名片>();
		if(file.length()!=0) 
		{
			while(haha.hasNext()) 
			{
				long[] Number=new long[10];
				String s=haha.nextLine();
				String [] CHUCUN=s.split(",");//切割
				String name=CHUCUN[0];
				String sex=CHUCUN[1];				
				String workspace=CHUCUN[2];
				String conect=CHUCUN[3];
				String firstday=CHUCUN[4];
				String others=CHUCUN[5];
				String youxiang=CHUCUN[6];
				名片 MP=new  名片(name,Number[0],sex,workspace,conect,youxiang,firstday,others);
				MP.setbeizhu(others);
				MP.setBrithday(firstday);
				MP.setCode1(Number[0]);//要为多个号码进行操作
				MP.Setname(name);
				MP.setYouxiang(youxiang);
				MP.setWorkplacce(workspace);
				MP.setTxdz(conect);
				MP.setsex(sex);
				LB.add(MP);
				
			}
			haha.close();
		}
      
		
      
		int choose=233;
		while(choose!=0)
		{
			System.out.println("这是一个通讯录,有以下功能");
			System.out.println("-------------------------------------------------------------------------------------------------");
			System.out.println("【1 增加】    【2 删除】   【3 更改】  【4 查找】  【5 全部联系人】   【6 导出通讯录】   【0 退出】   ");
			System.out.println("-------------------------------------------------------------------------------------------------");
			
			
			choose=input.nextInt();
			if(choose==1)
			{
				System.out.println("【1 新增名片】    【2 新增号码】   【3 新增其他】   ");
				int XZ=input.nextInt();
				if(XZ==1) 
				{
					while(XZ==1) 
					{
						int JI;
						System.out.println("姓名:");
						String name=input.next();
						
						System.out.println("输入号码");
						System.out.println("输入要加入的号码个数【最多10个】");
						long[] Number=new long[10];
						JI=input.nextInt();
						int K=0;
						for(int j=0;j<JI;j++)
						{
							K++;
							Number[j]=input.nextLong();
							System.out.println("您已添加"+K+"个");
						}
						System.out.println("输入性别");
						String sex=input.next();
						System.out.println("输入工作地址");
						String workspace=input.next();
						System.out.println("输入通讯地址");
						String conect=input.next();
						System.out.println("输入生日");
						String firstday=input.next();
						System.out.println("输入邮箱");
						String youxiang=input.next();
						System.out.println("输入备注");
						String others=input.next();
						
						名片 c=new  名片(name,Number[0],sex,workspace,conect,youxiang,firstday,others);
						LB.add(c);
						c.setbeizhu(others);
						c.setBrithday(firstday);
						c.setCode1(Number[0]);//要为多个号码进行操作
						c.setCode2(Number[1]);
						c.setCode3(Number[2]);
						c.setCode4(Number[3]);
						c.setCode5(Number[4]);
						c.setCode6(Number[5]);
						c.setCode7(Number[6]);
						c.setCode8(Number[7]);
						c.setCode9(Number[8]);
						c.setCode10(Number[9]);
						c.Setname(name);
						c.setYouxiang(youxiang);
						c.setWorkplacce(workspace);
						c.setTxdz(conect);
						c.setsex(sex);
						System.out.println("【1 继续添加名片】 【0 返回】"); 	
						int a=input.nextInt();
						if(a==0)
							break;
					}
				}
				
				if(XZ==2) 
				{	
					System.out.println("输入要添加号码联系人的姓名");
					String deletename=input.next();
					int WE=0;
					for(int i=0;i<LB.size();i++) 
					{
						
						名片 book=LB.get(i);
						if(book.getname().equals(deletename))
						{
							WE++;
							System.out.println("输入号码");
							System.out.println("输入要加入的号码个数【同一联系人下最多10个号码】");
							int JU;
							JU=input.nextInt();
							for(int j=0;j<JU;j++)
							{
								if(book.getCode1()==0)
								book.setCode1(input.nextLong());
							else if(book.getCode2()==0)
								book.setCode2(input.nextLong());
							else if(book.getCode3()==0)
								book.setCode3(input.nextLong());
							else if(book.getCode4()==0)
								book.setCode4(input.nextLong());
							else if(book.getCode5()==0)
								book.setCode5(input.nextLong());
							else if(book.getCode6()==0)
								book.setCode6(input.nextLong());
							else if(book.getCode7()==0)
								book.setCode7(input.nextLong());
							else if(book.getCode8()==0)
								book.setCode8(input.nextLong());
							else if(book.getCode9()==0)
								book.setCode9(input.nextLong());
							else if(book.getCode10()==0)
								book.setCode9(input.nextLong());
  		    	   			System.out.println("添加成功");
							}
						}
						
						
					}
					if(WE==0)
						{
							System.out.println("通讯录中没有此人");
						}
    	  
				}
				if(XZ==3)
				{
					System.out.println("输入需添信息联系人的姓名");
					String deletename=input.next();
					int n2=0;
					for(int i=0;i<LB.size();i++)
					{
						名片 book=LB.get(i);
						if(book.getname().equals(deletename))
						{
							int A=2;
							System.out.println("是性别吗?【YES 1】/【NO 0】");
							A=input.nextInt();
							if(A==1)
							{
								System.out.println("请输入性别");
								String sex=input.next();
								
							}
							System.out.println("是工作地址吗?【YES 1】/【NO 0】");
							A=input.nextInt();
							if(A==1)
							{
								System.out.println("输入工作地址");
								String workspace=input.next();
								
							}
							System.out.println("是通讯地址吗?【YES 1】/【NO 0】");
							A=input.nextInt();
							if(A==1)
							{
								System.out.println("输入通讯地址");
								String conect=input.next();
								
							}
							System.out.println("是生日吗?【YES 1】/【NO 0】");
							A=input.nextInt();
							if(A==1)
							{
								System.out.println("输入生日");
								String firstday=input.next();
							}
							System.out.println("是邮箱吗?【YES 1】/【NO 0】");
							A=input.nextInt();
							if(A==1)
							{
								System.out.println("输入邮箱");
								String youxiang=input.next();
							}
							System.out.println("是备注吗?【YES 1】/【NO 0】");
							A=input.nextInt();
							if(A==1)
							{
								System.out.println("输入备注");
								String others=input.next();
							}
						}
					}
				}
			}
			
			if(choose==2) {
		    	  System.out.println("输入要删除联系人的姓名");
		    	  String deletename=input.next();
		    	  int n2=0;
		    	  for(int i=0;i<LB.size();i++) {
		    		  名片 book=LB.get(i);
		    		  if(book.getname().equals(deletename))
		    		  {
		    			  LB.remove(book);
		    			  n2++;
		    	   		  System.out.println("删除成功");
		    		  }
		    	  }
		    	  if(n2==0)
		    			 System.out.println("查无此人");
		      }
		    if(choose==3) {
		    	System.out.println("输入要修改联系人的已存姓名");
		    	String  name=input.next();
		    	int n3=0;
		    	for(名片 s: LB) {
		   		  if(s.getname().equals(name)) {
		   			  n3++;
		   			int A=2;
			    	System.out.println("要修改姓名吗? 【YES 1】/【NO 0】");
					A=input.nextInt();
					if(A==1)
					{
						System.out.println("请输入新名字");
						String rename=input.next();
						s.Setname(rename);
						
					}
					System.out.println("要修改性别吗?【YES 1】/【NO 0】");
					A=input.nextInt();
					if(A==1)
					{
						System.out.println("请输入性别");
						String resex=input.next();
						s.setsex(resex);
						
					}
					System.out.println("要修改工作地址吗?【YES 1】/【NO 0】");
					A=input.nextInt();
					if(A==1)
					{
						System.out.println("输入工作地址");
						String reworkspace=input.next();
						s.setWorkplacce(reworkspace);
					}
					System.out.println("要修改通讯地址吗?【YES 1】/【NO 0】");
					A=input.nextInt();
					if(A==1)
					{
						System.out.println("输入通讯地址");
						String reconect=input.next();
						s.setTxdz(reconect);
					}
					System.out.println("要修改生日吗?【YES 1】/【NO 0】");
					A=input.nextInt();
					if(A==1)
					{
						System.out.println("输入生日");
						String refirstday=input.next();
						s.setBrithday(refirstday);
					}
					System.out.println("要修改邮箱吗?【YES 1】/【NO 0】");
					A=input.nextInt();
					if(A==1)
					{
						System.out.println("输入邮箱");
						String reyouxiang=input.next();
						s.setYouxiang(reyouxiang);
					}
					System.out.println("要修改备注吗?【YES 1】/【NO 0】");
					A=input.nextInt();
					if(A==1)
					{
						System.out.println("输入备注");
						String reothers=input.next();
						s.setbeizhu(reothers);
					}
					System.out.println("要修改号码吗?【YES 1】/【NO 0】");
					A=input.nextInt();
					if(A==1)
					{
						System.out.println("此联系人现有号码为:");
						System.out.println("********");
						if(s.getCode1()!=0)
						{
							System.out.println("号码1:"+s.getCode1());
						}
						if(s.getCode2()!=0)
						{
							System.out.println("号码2:"+s.getCode2());
						}
						if(s.getCode3()!=0)
						{
							System.out.println("号码3:"+s.getCode3());
						}
						if(s.getCode4()!=0)
						{
							System.out.println("号码4:"+s.getCode4());
						}
						if(s.getCode5()!=0)
						{
							System.out.println("号码5:"+s.getCode5());
						}
						if(s.getCode6()!=0)
						{
							System.out.println("号码6:"+s.getCode6());
						}
						if(s.getCode7()!=0)
						{
							System.out.println("号码7:"+s.getCode7());
						}
						if(s.getCode8()!=0)
						{
							System.out.println("号码8:"+s.getCode8());
						}
						if(s.getCode9()!=0)
						{
							System.out.println("号码9:"+s.getCode9());
						}
						if(s.getCode10()!=0)
						{
							System.out.println("号码10:"+s.getCode10());
						}
						System.out.println("********");
						System.out.println("");
						int AAA=1;
						while(AAA==1)
						{
							int SHU;
							System.out.println("请输入您要修改的号码数【注:号码1 输入 1】");
							SHU=input.nextInt();
							System.out.println("请输入新号码:");
							if(SHU==1)
							{
								long III = input.nextLong();
								s.setCode1(III);
							}
							if(SHU==2)
							{
								long III = input.nextLong();
								s.setCode2(III);
							}
							if(SHU==3)
							{
								long III = input.nextLong();
								s.setCode3(III);
							}
							if(SHU==4)
							{
								long III = input.nextLong();
								s.setCode4(III);
							}
							if(SHU==5)
							{
								long III = input.nextLong();
								s.setCode5(III);
							}
							if(SHU==6)
							{
								long III = input.nextLong();
								s.setCode6(III);
							}
							if(SHU==7)
							{
								long III = input.nextLong();
								s.setCode7(III);
							}
							if(SHU==8)
							{
								long III = input.nextLong();
								s.setCode8(III);
							}
							if(SHU==9)
							{
								long III = input.nextLong();
								s.setCode9(III);
							}
							if(SHU==10)
							{
								long III = input.nextLong();
								s.setCode10(III);
							}
							System.out.println("还要修改其他号码吗?【YES 1】/【NO 0】");
							System.out.println("此联系人现有号码为:");
							System.out.println("********");
							if(s.getCode1()!=0)
							{
								System.out.println("号码1:"+s.getCode1());
							}
							if(s.getCode2()!=0)
							{
								System.out.println("号码2:"+s.getCode2());
							}
							if(s.getCode3()!=0)
							{
								System.out.println("号码3:"+s.getCode3());
							}
							if(s.getCode4()!=0)
							{
								System.out.println("号码4:"+s.getCode4());
							}
							if(s.getCode5()!=0)
							{
								System.out.println("号码5:"+s.getCode5());
							}
							if(s.getCode6()!=0)
							{
								System.out.println("号码6:"+s.getCode6());
							}
							if(s.getCode7()!=0)
							{
								System.out.println("号码7:"+s.getCode7());
							}
							if(s.getCode8()!=0)
							{
								System.out.println("号码8:"+s.getCode8());
							}
							if(s.getCode9()!=0)
							{
								System.out.println("号码9:"+s.getCode9());
							}
							if(s.getCode10()!=0)
							{
								System.out.println("号码10:"+s.getCode10());
							}
							System.out.println("********");
							System.out.println("");
							AAA=input.nextInt();
						}
						
					}
	
		   		  }	  
		   		 
		      }
		    	 if(n3==0)
		   			 System.out.println("查无此人");
		}
		      
		      if(choose==4) {
		    	  long keyword1 = 0;String keyword2 = null;
		    	  int A=2;
					System.out.println("是号码吗?【YES 1】/【NO 0】");
					A=input.nextInt();
					if(A==1)
					{
						System.out.println("请输入号码");
						keyword1=input.nextLong();
						
					}
					System.out.println("是除号码外的其他关键字吗?【YES 1】/【NO 0】");
					A=input.nextInt();
					if(A==1)
					{
						System.out.println("请输入");
						keyword2=input.next();
						
					}
		    	  
					keyword1 = 2;//防止号码关键字为0导致错误的判断
		    	  
		    	  int n4=0;
		    	  for(名片 s: LB) {
		    		  if(s.getname().equals(keyword2)||s.getCode1()==keyword1||s.getCode2()==keyword1||s.getCode3()==keyword1||s.getCode10()==keyword1||s.getCode4()==keyword1||s.getCode5()==keyword1||s.getCode6()==keyword1||s.getCode7()==keyword1||s.getCode8()==keyword1||s.getCode9()==keyword1) {
		    			  n4++;
		    			  System.out.println("你要查找的联系人信息为");
		    			  System.out.println(s.ToString());
		    		  }
		    	  }
		    	  if(n4==0)
					  System.out.println("查无此人");
		      }
		      
		      if(choose==5) {
		    	  for(名片 s: LB) {
		    		  System.out.println("***********");
		    		  System.out.println(s.ToString());
		    		  if(s.getCode1()!=0)
						{
							System.out.println("号码1:"+s.getCode1());
						}
						if(s.getCode2()!=0)
						{
							System.out.println("号码2:"+s.getCode2());
						}
						if(s.getCode3()!=0)
						{
							System.out.println("号码3:"+s.getCode3());
						}
						if(s.getCode4()!=0)
						{
							System.out.println("号码4:"+s.getCode4());
						}
						if(s.getCode5()!=0)
						{
							System.out.println("号码5:"+s.getCode5());
						}
						if(s.getCode6()!=0)
						{
							System.out.println("号码6:"+s.getCode6());
						}
						if(s.getCode7()!=0)
						{
							System.out.println("号码7:"+s.getCode7());
						}
						if(s.getCode8()!=0)
						{
							System.out.println("号码8:"+s.getCode8());
						}
						if(s.getCode9()!=0)
						{
							System.out.println("号码9:"+s.getCode9());
						}
						if(s.getCode10()!=0)
						{
							System.out.println("号码10:"+s.getCode10());
						}
		    		  System.out.println("***********");
		    	  }
		      }
		      if(choose==6) {
		    		java.io.PrintWriter output =new java.io.PrintWriter(file);
		      		for(名片 s: LB) {
		      		
		      		output.print(s.getname()+",");
		      		output.print(s.getsex()+",");
		      		output.print(s.getWorkplacce()+",");
		      		output.print(s.getTxdz()+",");
		      		output.print(s.getFirstday()+",");
		      		output.print(s.getbeizhu()+",");
		      		if(s.getCode1()!=0)
					{
		      			output.print(s.getCode1()+",");
					}
					if(s.getCode2()!=0)
					{
						output.print(s.getCode2()+",");
					}
					if(s.getCode3()!=0)
					{
						output.print(s.getCode3()+",");
					}
					if(s.getCode4()!=0)
					{
						output.print(s.getCode4()+",");
					}
					if(s.getCode5()!=0)
					{
						output.print(s.getCode5()+",");
					}
					if(s.getCode6()!=0)
					{
						output.print(s.getCode6()+",");
					}
					if(s.getCode7()!=0)
					{
						output.print(s.getCode7()+",");
					}
					if(s.getCode8()!=0)
					{
						output.print(s.getCode8()+",");
					}
					if(s.getCode9()!=0)
					{
						output.print(s.getCode9()+",");
					}
					if(s.getCode10()!=0)
					{
						output.print(s.getCode10()+",");
					}
					
		      		output.println();
		      		}
		      		output.close();
		      		System.out.println("联系人已存入D盘中【程序设计作业通讯录.txt】"
		      				+ "");
		      }
		}
	}
}//还有不合规的输入要返回一个异常告诉用户【麻烦,再说了】
	


第一次做,菜的很,有学新的东西或更熟练后会更新
题中的特色功能暂时没写
本代码最初框架由用户guotailin66提供

为了在Windows安装ADB工具,你可以按照以下步骤进行操作: 1. 首先,下载ADB工具包并解压缩到你自定义的安装目录。你可以选择将其解压缩到任何你喜欢的位置。 2. 打开运行窗口,可以通过按下Win+R键来快速打开。在运行窗口中输入"sysdm.cpl"并按下回车键。 3. 在系统属性窗口中,选择"高级"选项卡,然后点击"环境变量"按钮。 4. 在环境变量窗口中,选择"系统变量"部分,并找到名为"Path"的变量。点击"编辑"按钮。 5. 在编辑环境变量窗口中,点击"新建"按钮,并将ADB工具的安装路径添加到新建的路径中。确保路径正确无误后,点击"确定"按钮。 6. 返回到桌面,打开命令提示符窗口。你可以通过按下Win+R键,然后输入"cmd"并按下回车键来快速打开命令提示符窗口。 7. 在命令提示符窗口中,输入"adb version"命令来验证ADB工具是否成功安装。如果显示版本信息,则表示安装成功。 这样,你就成功在Windows安装ADB工具。你可以使用ADB工具来执行各种操作,如枚举设备、进入/退出ADB终端、文件传输、运行命令、查看系统日志等。具体的操作方法可以参考ADB工具的官方文档或其他相关教程。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [windows环境安装adb驱动](https://blog.youkuaiyun.com/zx54633089/article/details/128533343)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Windows安装使用ADB简单易懂教程](https://blog.youkuaiyun.com/m0_37777700/article/details/129836351)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值