电话本管理系统 基础版

电话本管理系统 ArrayList版见:https://blog.csdn.net/Amber_technology/article/details/103977752

一、电话本管理系统

  1.主界面

2.添加

2.修改

 1)无信息

2)正常修改

 

3 . 打印所有电话本

4.删除

再次选择4,查询所有,张三的信息已删除

0.退出系统

 

 


import java.util.Scanner;


public class Test {

	static Scanner sc = new Scanner(System.in);
	static Telephone[] telephones = new Telephone[100];

	public static void main(String[] args) {
		//主界面
		while (true) {
			System.out.println("----------------电话本管理系统-------------");
			System.out.println("1.添加 2.删除 3.修改 4.查询所有 5.根据姓名查询 0.退出 ");
			System.out.println("----------------电话本管理系统-------------");
			System.out.println("请选择业务");
			int num = sc.nextInt();
			if (num == 0) {
				break;
			}
			switch (num) {
			case 1:
				add();
				break;
			case 2:
				del();
				break;
			case 3:
				modify();
				break;
			case 4:
				selctAll();
				break;
			case 5:
				search();
				break;
			default:
				System.out.println("请输入1~5之间的数字");
				break;
			}
			
		}

	}



	private static int search(String name) {
		
		for (int i = 0; i < telephones.length; i++) {
			if (telephones[i] == null) {
				break;
			}
			if (telephones[i].getName().equals(name)) {
				return i;
			}
		}
		return -1;
		
	}
	private static void search() {
		System.out.println("请输入要查找的姓名");
		String name = sc.next();
		int i = search(name);
		if (i == -1) {
			System.out.println("未找到相应记录");
		} else {
			print(telephones[i]);
		}
	}



	private static void selctAll() {
		System.out.println("---------------打印所有电话本--------------");
		if (telephones[0]==null) {
			System.out.println("无记录");
		}else {
			for (int i = 0; i < telephones.length;i++) {
				if (telephones[i]==null) {
					break;
				}else {
					print(telephones[i]);

				}
			}
		}
	}

	private static void modify() {
		System.out.println("请输入要修改的姓名");
		boolean flag =false;
		String name = sc.next();
		int i = search(name);
		if (i == -1) {
			System.out.println("无信息");
		}else{
			print(telephones[i]);
			System.out.println("请重新输入信息");
			input(telephones[i]);
			print(telephones[i]);
			flag = true;
			}
	}


	private static void del() {
		System.out.println("请输入要删除的姓名");
		String name = sc.next();
		int i = search(name);
		
		if (i == -1) {
			System.out.println("未找到相应信息");
			return;
		}else{
			print(telephones[i]);
			System.out.println("确定要删除吗?1(是)0(否)");
			int num = sc.nextInt();
			if (num == 1) {
				for (int j = i; j < telephones.length -1; j++) {
					telephones[i]=telephones[i+1];
				}
			}
		}
	}




	private static void add() {
		for (int i = 0; i < telephones.length; i++) {
			if (telephones[i]!=null) {
				continue;
			}else {
				telephones[i] = new Telephone();
				System.out.println("----------------添加电话本---------------");
				input(telephones[i]);
				print(telephones[i]);
				System.out.println("添加成功");
				break;
			}
		}
	}
	
	//打印telephone[i]
	private static void print(Telephone telephone) {
			System.out.print("姓名:");
			System.out.print(telephone.getName());
			System.out.print("性别:");
			System.out.print(telephone.getSex());
			System.out.print("年龄:");
			System.out.print(telephone.getAge());
			System.out.print("电话:");
			System.out.print(telephone.getTel());
			System.out.print("QQ:");
			System.out.print(telephone.getQQ());
			System.out.print("地址:");
			System.out.print(telephone.getAddr());
			System.out.println();
	}
	//输入telephone[i]
	private static void input(Telephone telephone) {
		System.out.println("姓名:");
		telephone.setName(sc.next());
		System.out.println("性别:");
		telephone.setSex(sc.next());
		System.out.println("年龄:");
		telephone.setAge(sc.nextInt());
		System.out.println("电话:");
		telephone.setTel(sc.next());
		System.out.println("QQ:");
		telephone.setQQ(sc.next());
		System.out.println("地址:");
		telephone.setAddr(sc.next());

	}
	
}

public class Telephone {
	//变量
	private String name;
	private String sex;
	private int age;
	private String tel;
	private String QQ;
	private String addr;
	
	//方法
	//添加
	public void add() {
		
	}
	//修改
	public void modify() {
		
	}
	//删除
	public void del() {
		
	}
	
	//set & get方法
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getQQ() {
		return QQ;
	}
	public void setQQ(String QQ) {
		this.QQ = QQ;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	
	

}

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值