电话本管理系统
1.主界面
大体就是要实现对电话本的增删改查:
因为不清楚要存储的数据的数量,所以可以使用ArrayList不定长数组去实现,也可以使用集合类去做,此题我用Map集合来做。
废话不多说,直接上代码:
PhoneInfo类定义了一些属性,变量及构造方法
public class PhoneInfo {
private String name;
private String sex;
private String age;
private String tel;
private String 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 String getAge() {
return age;
}
public void setAge(String 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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public PhoneInfo(String name, String sex, String age, String tel, String qq, String address) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.tel = tel;
this.qq = qq;
this.address = address;
}
@Override
public String toString() {
return "PhoneInfo [name=" + name + ", sex=" + sex + ", age=" + age + ", tel=" + tel + ", qq=" + qq
+ ", address=" + address + "]";
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class TelBook {
static Scanner sc = new Scanner(System.in);
static TelBook tb = new TelBook();
Map map = new HashMap<>();
public static void main(String[] args) {
System.out.println("————————————————————电话本管理系统——————————————————");
System.out.println("1.添加 2.删除 3.修改 4.查询所有 5.根据姓名查询 0.退出");
System.out.println("————————————————————电话本管理系统——————————————————");
int option;
do {
System.out.println("请选择业务");
option = sc.nextInt();
switch (option) {
case 1:
tb.add();
break;
case 2:
tb.delete();
break;
case 3:
tb.modify();
break;
case 4:
tb.find();
break;
case 5:
tb.findName();
break;
case 0:
System.out.println("电话本系统结束");
break;
default:
}}
while(option!=0);
}
public void add() {
System.out.println("———————————添加电话本————————————");
shuru();
Set set = map.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry mapEntry = (Map.Entry) it.next();
System.out.print(mapEntry.getKey() + " " + mapEntry.getValue());
}
System.out.println();
System.out.println("添加成功");
}
public void delete() {
System.out.println("————————————删除电话本——————————————");
System.out.println("请输入要删除的姓名");
String deleteName = sc.next();
if(map.containsValue(deleteName)) {
map.remove(deleteName);
System.out.println("删除成功");
}else
System.out.println("用户不存在!");
}
public void modify() {
System.out.println("————————————修改电话本——————————————");
System.out.println("请输入要修改的姓名");
String modifyName = sc.next();
if(map.containsValue(modifyName)) {
map.remove(modifyName);
shuru();
Set set = map.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry mapEntry = (Map.Entry) it.next();
System.out.print(mapEntry.getKey() + " " + mapEntry.getValue());
}
System.out.println();
}
else {
System.out.println("用户不存在");
}
}
public void find() {
System.out.println("——————————————查询所有——————————————");
/* Set set = map.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry mapEntry = (Map.Entry) it.next();
System.out.print(mapEntry.getKey() + " " + mapEntry.getValue());
}*/
Set<String> set = map.keySet();
for(String s:set) {
System.out.println(map.get(s).toString());
}
}
public void findName() {
System.out.println("————————————根据姓名查询——————————————");
System.out.println("请输入要查询的姓名");
String findName = sc.next();
if(map.containsValue(findName)) {
Set set = map.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry mapEntry = (Map.Entry) it.next();
System.out.print(mapEntry.getKey() + " " + mapEntry.getValue());
}
System.out.println();
}
else {
System.out.println("用户不存在");
}
}
public void shuru() {
PhoneInfo p = new PhoneInfo("", "", "", "", "", "");
System.out.print("姓名:");
String name = sc.next();
p.setName(name);
map.put("姓名:", name);
System.out.println();
System.out.print("性别:");
String sex = sc.next();
p.setSex(sex);
map.put("性别:", sex);
System.out.println();
System.out.print("年龄:");
String age = sc.next();
p.setAge(age);
map.put("年龄:", age);
System.out.println();
System.out.print("电话:");
String tel = sc.next();
p.setTel(tel);
map.put("电话:", tel);
System.out.println();
System.out.print("QQ:");
String qq = sc.next();
p.setQq(qq);
map.put("QQ:", qq);
System.out.println();
System.out.print("地址:");
String address = sc.next();
p.setAddress(address);
map.put("地址:", address);
System.out.println();
}
}
部分代码截图:
运行截图:
写在最后:从Map中获取元素一定要注意是Key值还是Value值,修改时要先remove.