電話本管理系統 ArrayList版

題目參考: https://blog.csdn.net/Amber_technology/article/details/103818151 本文代碼是在其基礎上進行的改進。

主要修改:使用ArrayList替換了數組,簡化了代碼量,提高了代碼的可用性。

public class Telephone {
	//變量
	private String name;
	private String sex;
	private int age;
	private String tel;
	private String QQ;
	private String addr;
	
	//方法
	
	//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;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Telephone other = (Telephone) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Telephone [name=" + name + ", sex=" + sex + ", age=" + age + ", tel=" + tel + ", QQ=" + QQ + ", addr="
				+ addr + "]";
	}
	public Telephone(String name, String sex, int age, String tel, String qQ, String addr) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.tel = tel;
		QQ = qQ;
		this.addr = addr;
	}
	public Telephone() {
		super();
	}
	
	

}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Test {

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

	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) {
		Telephone telephone = new Telephone();
		telephone.setName(name);
		if (telephones.contains(telephone)) {
			return telephones.indexOf(telephone);
		}
		return -1;

	}

	private static void search() {
		System.out.println("請輸入要查找的姓名");
		String name = sc.next();
		int index = search(name);
		if (index == -1) {
			System.out.println("未找到相應記錄");
		} else {
			print(telephones.get(index));
		}
	}

	private static synchronized void selctAll() {
		System.out.println("---------------打印所有電話本--------------");
		if (telephones.size() == 0) {
			System.out.println("無記錄");
		} else {
			Iterator<Telephone> iterator = telephones.iterator();

			while (iterator.hasNext()) {
				System.out.println(iterator.next());
			}
		}
	}

	private static void modify() {
		System.out.println("請輸入要修改的姓名");
		String name = sc.next();
		int index = search(name);
		if (index == -1) {
			System.out.println("無信息");
		} else {
			print(telephones.get(index));
			System.out.println("請重新輸入信息");
			Telephone telephone = input();
			telephones.remove(index);
			telephones.add(telephone);
			print(telephone);
		}
	}

	private static void del() {
		System.out.println("請輸入要刪除的姓名");
		String name = sc.next();
		int index = search(name);

		if (index == -1) {
			System.out.println("未找到相應信息");
			return;
		} else {
			print(telephones.get(index));
			System.out.println("確定要刪除嗎?1(是)0(否)");
			int num = sc.nextInt();
			if (num == 1) {
				telephones.remove(index);
			}
		}
	}

	private static void add() {
		System.out.println("----------------添加電話本---------------");
		Telephone telephone = input();
		telephones.add(telephone);
		print(telephone);
		System.out.println("添加成功");
	}

	// 打印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 Telephone input() {
		Telephone telephone = new 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());
		return telephone;

	}

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章