电话本管理系统(使用数组)

来源:互联网 发布:占领中环 知乎 编辑:程序博客网 时间:2024/05/23 19:13

import java.util.Scanner;

public class Main {
private static Person[] persons = new Person[100];
private static Scanner input = new Scanner(System.in);
static boolean exist = true;
public static void main(String[] args) {
boolean isExit = false;
while (!isExit) {
System.out.println("-------------电话本管理系统--------------");
System.out.println("1.添加2.修改3.删除4.查询所有5.根据姓名查询0.退出");
System.out.println("-------------电话本管理系统--------------");
System.out.println("请输入选项:");
int choose = input.nextInt();
switch (choose) {
case 1:
add();
break;
case 2:
System.out.println("请输入要修改的姓名:");
reset(input.next());
break;
case 3:
System.out.println("请输入要删除的姓名:");
del(input.next());
break;
case 4:
show();
break;
case 5:
System.out.println("请输入要查询的姓名:");
find(input.next());
break;
case 0:
isExit = true;
break;
default:
break;
}
}
}


public static void add() {
Person person = new Person();
System.out.println("姓名:");
person.setName(input.next());
System.out.println("性别:");
person.setSex(input.next());
System.out.println("年龄:");
person.setAge(input.nextInt());
System.out.println("电话:");
person.setTellphone(input.nextLong());
System.out.println("qq:");
person.setQq(input.nextLong());
System.out.println("地址:");
person.setAddress(input.next());
for (int i = 0; i < persons.length; i++) {
if (persons[i] == null) {
persons[i] = person;
System.out.println(persons[i].toString());
break;
}
}
}


public static void reset(String name) {
for (int i = 0; i < persons.length; i++) {
if (persons[i] != null) {
if (persons[i].getName().equals(name)) {
exist = false;
System.out.println("请重新输入:");
Person person = new Person();
System.out.println("姓名:");
person.setName(input.next());
System.out.println("性别:");
person.setSex(input.next());
System.out.println("年龄:");
person.setAge(input.nextInt());
System.out.println("电话:");
person.setTellphone(input.nextLong());
System.out.println("qq:");
person.setQq(input.nextLong());
System.out.println("地址:");
person.setAddress(input.next());
persons[i] = person;
System.out.println(persons[i].toString());
break;
}
}
}
if (exist) {
System.out.println("查无此人");
}
exist = true;
}


public static void del(String name) {
for (int i = 0; i < persons.length; i++) {
if (persons[i] != null) {
if (persons[i].getName().equals(name)) {
exist = false;
System.out.println("确认要删除吗?1.确认2.取消");
int j = input.nextInt();
if (j == 1) {
persons[i] = null;
System.out.println("删除成功");
break;
}
}
}
}
if (exist) {
System.out.println("查无此人");
}
exist = true;
}


public static void show() {
for (int i = 0; i < persons.length; i++) {
if (persons[i] != null) {
System.out.println(persons[i].toString());
}
}
}


public static void find(String name) {
for (int i = 0; i < persons.length; i++) {
if (persons[i] != null) {
if (persons[i].getName().equals(name)) {
exist = false;
System.out.println(persons[i].toString());
break;
}
}
}
if (exist) {
System.out.println("查无此人");
}
exist = true;
}

}


 class Person {

private String name ; 
private String sex;
private int age;
private long tellphone;
private long qq;
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 long getTellphone() {
return tellphone;
}
public void setTellphone(long tellphone) {
this.tellphone = tellphone;
}
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;
}
public String toString() {
return "姓名:" + name + ", 性别" + sex + ", 年龄:" + age
+ ", 电话:" + tellphone + ", qq:" + qq + ", 地址:"
+ address ;
}

}

0 0