欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java电话本管理系统数组版

程序员文章站 2024-03-06 08:31:37
...

电话本管理系统

1.主界面
Java电话本管理系统数组版
2.添加
Java电话本管理系统数组版
2.修改
1)无信息
Java电话本管理系统数组版
2)正常修改
Java电话本管理系统数组版

3 . 打印所有电话本
Java电话本管理系统数组版
4.删除
Java电话本管理系统数组版
再次选择4,查询所有,张三的信息已删除
Java电话本管理系统数组版
0.退出系统

代码如下

JDK1.8版本,最后附完整代码
主界面

import java.util.Scanner;

public class mainweb {
	public static void main(String[] args) {
		TelePhoneBook book = new TelePhoneBook();
		Scanner scanner = new Scanner(System.in);
		while (true) {
			ui();
			int choose = scanner.nextInt();
			switch (choose) {
			case 1:
				System.out.println("选择了添加");
				System.out.print("姓名:");
				String nameadd = scanner.next();
				if (book.selctbyname(nameadd) != null) {
					System.out.println("已经存在此人信息");
					break;
				}
				System.out.print("性别:");
				String sexadd = scanner.next();
				if ((sexadd.equals("男") || sexadd.equals("女")) == false) {
					System.out.println("请输入正确的性别(男,女)");
					break;
				}
				System.out.print("年龄:");
				int ageadd = scanner.nextInt();
				if (ageadd > 150 || ageadd <= 0) {
					System.out.println("请输入正确的年龄");
					break;
				}
				System.out.print("电话:");
				String phoneadd = scanner.next();
				System.out.print("QQ:");
				String QQadd = scanner.next();
				System.out.print("地址:");
				String addressadd = scanner.next();
				TelePhoneItem itemadd = new TelePhoneItem();
				itemadd.setName(nameadd);
				itemadd.setSex(sexadd);
				itemadd.setAge(ageadd);
				itemadd.setPhonenumber(phoneadd);
				itemadd.setQQnumber(QQadd);
				itemadd.setAddress(addressadd);
				boolean result = book.add(itemadd);
				if (result == true) {
					System.out.println("添加成功");
					System.out.println(itemadd);
				} else {
					System.out.println("电话本已满,添加失败");
				}
				break;
			case 2:
				System.out.println("选择了删除");
				System.out.print("请输入要删除的姓名:");
				String namedelete = scanner.next();
				boolean deleteresult = book.delete(namedelete);
				if (deleteresult == true) {
					System.out.println("删除成功");
				} else {
					System.out.println("没有此人");
				}

				break;
			case 3:
				System.out.println("选择了修改信息");
				System.out.print("请输入要修改人的姓名:");
				String nameupdate = scanner.next();
				TelePhoneItem selectdelete = book.selctbyname(nameupdate);
				if (selectdelete != null) {
					System.out.println("请输入修改后的信息:");
					System.out.print("姓名:");
					String nameupdate2 = scanner.next();
					if (nameupdate2.equals(nameupdate)) {
						System.out.print("性别:");
						String sexupdate = scanner.next();
						if ((sexupdate.equals("男") || sexupdate.equals("女")) == false) {
							System.out.println("请输入正确的性别(男,女)");
							break;
						}
						System.out.print("年龄:");
						int ageupdate = scanner.nextInt();
						if (ageupdate > 150 || ageupdate <= 0) {
							System.out.println("请输入正确的年龄");
							break;
						}
						System.out.print("电话:");
						String phoneupdate = scanner.next();
						System.out.print("QQ:");
						String QQupdate = scanner.next();
						System.out.print("地址:");
						String addressupdate = scanner.next();
						TelePhoneItem itemupdate = new TelePhoneItem();
						itemupdate.setName(nameupdate);
						itemupdate.setSex(sexupdate);
						itemupdate.setAge(ageupdate);
						itemupdate.setPhonenumber(phoneupdate);
						itemupdate.setQQnumber(QQupdate);
						itemupdate.setAddress(addressupdate);
						boolean resultupdate = book.add(itemupdate);
						if (resultupdate == true) {
							System.out.println("修改成功");
							System.out.println(itemupdate);
						}
					}
				} else {
					System.out.println("电话本中没有此人");
					break;
				}

				break;
			case 4:
				System.out.println("选择了查询所有");
				book.selectall();
				break;
			case 5:
				System.out.println("根据姓名查询");
				System.out.print("要查询的姓名:");
				String selectname = scanner.next();
				TelePhoneItem itemselect = book.selctbyname(selectname);

				if (itemselect != null) {
					System.out.println(itemselect);
				} else {
					System.out.println("没有查询到此人");
				}
				break;
			case 0:
				System.out.println("系统退出!");
				System.exit(0);
				break;
			default:
				System.out.println("没有此项");
				break;
			}
		}
	}

	public static void ui() {
		System.out.println("---------------电话本管理系统---------------");
		System.out.println("1.添加  2.删除  3.修改  4.查询所有  5.根据姓名查询 0.退出");
		System.out.println("---------------电话本管理系统---------------");
		System.out.print("请选择业务:");
	}
}

电话本方法


/**
 * 电话本 用数组表示
 * 
 * @author Administrator
 *
 */
public class TelePhoneBook {
	TelePhoneItem[] telephonebook = new TelePhoneItem[100];

	// 根据姓名查找
	public TelePhoneItem selctbyname(String name) {

		for (int i = 0; i < telephonebook.length; i++) {
			if (telephonebook[i] != null && telephonebook[i].getName().equals(name)) {
				return telephonebook[i];
			}

		}
		return null;
	}
	// 添加

	public boolean add(TelePhoneItem item) {
		for (int i = 0; i < telephonebook.length; i++) {
			if (telephonebook[i] == null) {
				telephonebook[i] = item;
				return true;
			}
		}
		return false;

	}

	// 删除
	public boolean delete(String name) {
		for (int i = 0; i < telephonebook.length; i++) {
			if (telephonebook[i].getName().equals(name)) {
				telephonebook[i] = null;
				return true;
			}
		}
		return false;
	}

	// 修改
	public boolean update(TelePhoneItem item) {
		for (int i = 0; i < telephonebook.length; i++) {
			if (telephonebook[i] == null) {
				telephonebook[i] = item;
				return true;
			}
		}
		return false;
	}

	// 查询所有
	public void selectall() {
		for (int i = 0; i < telephonebook.length; i++) {
			if (telephonebook[i] != null) {
				System.out.println(telephonebook[i]);
			}
		}
	}

}

属性类


/**
 * 一条记录的属性
 * 
 * @author Administrator
 *
 */
public class TelePhoneItem {
	// 姓名
	private String name;
	// 性别
	private String sex;
	// 年龄
	private int age;
	// 电话
	private String phonenumber;
	// Qq
	private String QQnumber;
	// 地址
	private String address;

	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 getPhonenumber() {
		return phonenumber;
	}

	public void setPhonenumber(String phonenumber) {
		this.phonenumber = phonenumber;
	}

	public String getQQnumber() {
		return QQnumber;
	}

	public void setQQnumber(String qQnumber) {
		QQnumber = qQnumber;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "TelePhoneItem [name=" + name + ", sex=" + sex + ", age=" + age + ", phonenumber=" + phonenumber
				+ ", QQnumber=" + QQnumber + ", address=" + address + "]";
	}

}

源码链接
https://download.csdn.net/download/qq_40982398/12181823

相关标签: java