Java小程序------电话本管理系统(数组)
程序员文章站
2022-05-26 16:21:51
...
Java小程序------电话本管理系统(数组)
使用Java写一个电话本管理系统,利用数组,将输入的联系人信息封装到对象中,再将对象存入数组。
实现添加,删除,修改,查询数组中所有联系人,按名字查询,退出等功能。下面是代码
1.首先建立一个PhoneBook类
public class PhoneBook {
private String name;
private char sex;
private int age;
private String phone;
private long qq;
private String address;
public PhoneBook(String name,char sex,int age,String phone,long qq,String address) {
this.name = name;
this.sex = sex;
this.age = age;
this.phone = phone;
this.qq = qq;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public long getQq() {
return qq;
}
public void setQq(long qq) {
this.qq = qq;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override//用字符串表示
public String toString() {
return "姓名:" + name + ", 性别:" + sex + ", 年龄:" + age + ", 电话:" + phone + ", QQ:" + qq
+ ", 地址:" + address;
}
}
2.方法
import java.util.Scanner;
public class Methods {
PhoneBook[] pb = new PhoneBook[100];
//PhoneBook数组用来存放联系人信息
int num = 0;//索引
Scanner sc = new Scanner(System.in);
//添加联系人
public void add() {
System.out.println("----------添加电话本----------");
System.out.print("姓名:");
String name = sc.nextLine();
System.out.print("性别:");
char sex = sc.next().charAt(0);
System.out.print("年龄:");
int age = sc.nextInt();
sc.nextLine();
System.out.print("电话:");
String phone = sc.nextLine();
System.out.print("QQ:");
long qq = sc.nextLong();
sc.nextLine();
System.out.print("地址:");
String address = sc.nextLine();
PhoneBook phoneBook = new PhoneBook(name, sex, age, phone, qq, address);//新建一个对象
pb[num++] = phoneBook;//将对象存入数组
System.out.println(phoneBook.toString());//输出对象元素
System.out.println("添加成功!");
System.out.println("**************************************");//分割线
}
//修改联系人
public void update() {
boolean flag = false;//初始化flag,用于判断是否有此人
System.out.println("----------修改电话本----------");
System.out.println("请输入要修改的姓名:");
String name1 = sc.nextLine();
for (int i = 0; i < num; i++) {
if (name1.equals(pb[i].getName())) {
flag = true;
System.out.println(pb[i].toString());
System.out.println("请重新输入信息");
System.out.print("姓名:");
String name = sc.nextLine();
System.out.print("性别:");
char sex = sc.next().charAt(0);
System.out.print("年龄:");
int age = sc.nextInt();
sc.nextLine();
System.out.print("电话:");
String phone = sc.nextLine();
System.out.print("QQ:");
long qq = sc.nextLong();
sc.nextLine();
System.out.print("地址:");
String address = sc.nextLine();
PhoneBook phoneBook = new PhoneBook(name, sex, age, phone, qq, address);//新建一个对象
pb[i] = phoneBook;
System.out.println(phoneBook.toString());
System.out.println("修改成功");
System.out.println("**************************************");//分割线
break;
}
if (flag == false) {
System.out.println("无信息");
System.out.println("**************************************");//分割线
}
}
}
//打印所有电话本
public void selectAll() {
System.out.println("----------打印所有电话本----------");
if (num == 0) {
System.out.println("电话本中无信息");
System.out.println("**************************************");
} else {
for (int i = 0; i < num; i++) {
System.out.println(pb[i].toString());
System.out.println("**************************************");
}
}
}
//删除联系人
public void del() {
boolean flag = false;
System.out.println("----------删除电话本----------");
System.out.println("请输入要删除的姓名");
String name1 = sc.nextLine();
for (int i = 0; i < num; i++) {
if (name1.equals(pb[i].getName())) {
flag = true;
System.out.println("确认要删除吗?1(是)0(否)");
int n = sc.nextInt();
if (n == 1) {
for (int j = i; j < num - 1; j++) {
if (j == num - 1) {
pb[j] = null;
} else {
pb[j] = pb[j + 1];
}
}
num--;
System.out.println("删除成功");
System.out.println("**************************************");
}
}
if (flag == false) {
System.out.println("无信息");
System.out.println("**************************************");
}
}
}
//根据姓名查询
public void selectName() {
boolean flag = false;
System.out.println("----------根据姓名查询----------");
System.out.println("请输入要查询的姓名:");
String name1 = sc.nextLine();
for (int i = 0; i < num; i++) {
if (name1.equals(pb[i].getName())) {
flag = true;
System.out.println(pb[i].toString());
System.out.println("**************************************");
}
}
if (flag == false) {
System.out.println("查无此人");
System.out.println("**************************************");
}
}
//退出系统
public void exit() {
System.out.println("退出系统");
}
}
3.主函数,实现功能
import java.util.Scanner;
public class Phone {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Methods ms = new Methods();
while(true){
System.out.println("----------电话本管理系统----------");
System.out.println("1.添加 2.删除 3.修改 4.查询所有 5.根据姓名查询 0.退出");
System.out.println("----------电话本管理系统----------");
//输入序号,实现对应的功能
System.out.println("请选择业务:");
int n = sc.nextInt();
if (n == 1) {
ms.add();
}else if (n == 2) {
ms.del();
}else if (n == 3) {
ms.update();
}else if (n == 4) {
ms.selectAll();
}else if (n == 5) {
ms.selectName();
}else {
ms.exit();
break;
}
}
}
}
运行结果