java原电话本管理系统+改方法用系统给的ArrayList类的方法实现。
程序员文章站
2024-03-06 09:48:49
...
1.算是父类(因为功能少没子类)
public class TelephoneBook {
private String name;
private String sex;
private String age;
private String telephone;
private String qq;
private String address;
public TelephoneBook(String name, String sex, String age, String telephone,
String qq, String address) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.telephone = telephone;
this.qq = qq;
this.address = address;
}
public TelephoneBook() {
super();
}
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 String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void display() {
System.out.println( "姓名:" + getName() +",性别:"
+ getSex()+ ",年龄:" + getAge() + ",电话:"
+ getTelephone()+",QQ:"+getQq()+",地址:"+getAddress());
}
}
2.主界面(测试类)
public class TelTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 开始界面
// 业务选择
while (true) {
System.out.println("|-------------------电话本管理系统-------------|");
System.out.println("1.添加 2.删除 3.修改 4.查询所有 5.根据姓名查询 0.退出");
System.out.println("|-------------------电话本管理系统-------------|");
System.out.println("请选择业务:");
String choice = sc.nextLine();
switch (choice) {
case "1": // 添加电话本
TelMethod.addTel();
break;
case "2": // 删除
TelMethod.delTel();
break;
case "3": // 修改
TelMethod.updateTel();
break;
case "4": // 查找
TelMethod.queryAll();
break;
case "5": // 根据姓名查找雇员信息
TelMethod.queryTel();
break;
case "0": // 退出
System.out.println("退出成功!");
System.exit(0);
break;
default:
System.out.println("您输入的数字不正确!");
break;
}
}
}
}
3.方法实现类
import java.util.Scanner;
public class TelMethod {
static Scanner sc = new Scanner(System.in);
// 存放员工信息数组
static TelephoneBook[] tels = new TelephoneBook[100];
static int maxIndex = -1;// 已经存放元素的最大索引
public static TelephoneBook createTelephoneBook() {
System.out.print("姓名:");
String name = sc.nextLine();
System.out.print("性别:");
String sex = sc.nextLine();
System.out.print("年龄:");
String age = sc.nextLine();
System.out.print("电话:");
String telephone = sc.nextLine();
System.out.print("QQ:");
String qq = sc.nextLine();
System.out.print("地址:");
String address = sc.nextLine();
TelephoneBook tel = null;
tel=new TelephoneBook(name, sex, age, telephone, qq, address);
return tel;
}
public static int getIndexByName(String name) {
for (int i = 0; i <= maxIndex; i++) {
if (name.equals(tels[i].getName())) {
// 找到了
return i;
}
}
return -1;
}
public static void addTel() {
System.out.println("------添加电话本------");
TelephoneBook tel = createTelephoneBook();
tels[++maxIndex] = tel;
tel.display();
System.out.println("添加成功!");
}
public static void delTel() {
System.out.println("------删除电话本------");
System.out.println("请输入删除人的姓名:");
String name = sc.nextLine();
// 查询所删除的用户是否存在,如果存在就删除(删除元素的下标)
int index = getIndexByName(name);
if (index != -1) {
tels[index].display();
System.out.println("确定吗?1(是) 0(否):");
int a = sc.nextInt();
if (a == 1) {
// 删除
for (int i = index; i < maxIndex; i++) {
tels[index] = tels[index + 1];
}
maxIndex--;
System.out.println("删除成功");
return;// 结束该方法
}
if (a == 0) {
System.out.println("取消删除成功");
return;
}
}else{
System.out.println("此人不存在");
}
}
public static void updateTel() {
System.out.println("------修改电话本------");
System.out.println("请输入姓名:");
String name = sc.nextLine();
int index = getIndexByName(name);
if (index == -1) {
System.out.println("此人不存在");
return;
}
TelephoneBook tel = createTelephoneBook();
tels[maxIndex] = tel;
System.out.println("修改成功");
tel.display();
}
public static void queryAll() {
System.out.println("------打印所有电话本------");
// 得到数组[]中有值的元素
for (int i = 0; i <= maxIndex; i++) {
// 得到
TelephoneBook tel = tels[i];
tel.display();
}
}
public static void queryTel() {
System.out.println("------查找电话本------");
System.out.println("请输入姓名:");
String name = sc.nextLine();
int index = getIndexByName(name);
if (index == -1) {
System.out.println("此人不存在");
return;
}
tels[index].display();
}
}
4.改,用Arraylist类内方法实现的实现类(简化)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TelMethod {
static Scanner sc = new Scanner(System.in);
// 存放员工信息数组
// static TelephoneBook[] tels = new TelephoneBook[100];
static List<TelephoneBook> tels = new ArrayList<>();
public static TelephoneBook createTelephoneBook() {
System.out.print("姓名:");
String name = sc.nextLine();
System.out.print("性别:");
String sex = sc.nextLine();
System.out.print("年龄:");
String age = sc.nextLine();
System.out.print("电话:");
String telephone = sc.nextLine();
System.out.print("QQ:");
String qq = sc.nextLine();
System.out.print("地址:");
String address = sc.nextLine();
TelephoneBook tel = null;
tel=new TelephoneBook(name, sex, age, telephone, qq, address);
return tel;
}
public static int getIndexByName(String name) {
for(int i=0;i<tels.size();i++){
if(tels.get(i).getName().equals(name)){
//找到了
return i;
}
}
return -1;
}
public static void addTel() {
System.out.println("------添加电话本------");
TelephoneBook tel = createTelephoneBook();
/*Person person = new Person(age, name);
persons.add(person);*/
tels.add(tel);
tel.display();
System.out.println("添加成功!");
}
public static void delTel() {
System.out.println("------删除电话本------");
System.out.println("请输入删除人的姓名:");
String name = sc.nextLine();
// 查询所删除的用户是否存在,如果存在就删除(删除元素的下标)
int index = getIndexByName(name);
if (index != -1) {
System.out.println("确定吗?1(是) 0(否):");
int a = sc.nextInt();
if (a == 1) {
// 删除
tels.remove(index);
System.out.println("删除成功");
return;// 结束该方法
}
if (a == 0) {
System.out.println("取消删除成功");
return;
}
}else{
System.out.println("此人不存在");
}
}
public static void updateTel() {
System.out.println("------修改电话本------");
System.out.println("请输入姓名:");
String name = sc.nextLine();
int index = getIndexByName(name);
if (index == -1) {
System.out.println("此人不存在");
return;
}
TelephoneBook tel = createTelephoneBook();
tels.set(index, tel);
System.out.println("修改成功");
tel.display();
}
public static void queryAll() {
System.out.println("------打印所有电话本------");
// 得到数组[]中有值的元素
for (int i = 0; i <tels.size(); i++) {
// 得到
TelephoneBook tel = tels.get(i);
tel.display();
}
}
public static void queryTel() {
System.out.println("------查找电话本------");
System.out.println("请输入姓名:");
String name = sc.nextLine();
int index = getIndexByName(name);
if (index == -1) {
System.out.println("此人不存在");
return;
}
tels.get(index).display();
}
}
上一篇: java 实现定时的方法及实例代码
下一篇: CentOS安装jdk的三种方法