前言:通讯录非常适合前期需要练习语法的小伙伴,效果非常好。
水再浑浊,只要长久沉淀,依然会分外清澈。
人再愚钝,只要足够努力,一样能改写命运。
此通讯录的功能包括:
1.删除联系人
2.增加联系人
3.展示已有联系人
4.查找联系人
5.排序联系人
6.退出通讯录
public class Main {
public static IMainList func(){
AddressList.menu();//打印菜单
Scanner scanner=new Scanner(System.in);
System.out.println("请选择->");
int k= scanner.nextInt();
if(k==1){
return new DeleatePerson();//删除联系人
}else if(k==2) {
return new AddPerson();//增加联系人
}else if(k==3){
return new ShowPerson();//展示联系人
}else if(k==4){
return new LookupPerson();//查询联系人
}else if(k==5){
return new SortPerson();//联系人排序
}
return null;
}
public static void main(String[] args) {
//AddPerson addPerson=new AddPerson();//创建通信录
AddressList addressList=new AddressList();
while(true) {
IMainList iMainList = func();//向上转型,执行通讯录的相关操作
if(iMainList==null){
System.out.println("选择错误,退出通讯录系统!");
break;
}
iMainList.func(addressList);
}
}
}
联系人包含姓名、电话号、出生年月信息。
public class Person1 implements Comparable<Person1>{
public String name;
private String Telephone;//电话号
private int year;//出生年月
private int month;
private int day;
public Person1(String name, String telephone, int year, int month, int day) {
this.name = name;
Telephone = telephone;
this.year = year;
this.month = month;
this.day = day;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone() {
return Telephone;
}
public void setTelephone(String telephone) {
Telephone = telephone;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "Person1{" +
"name='" + name + '\'' +
", Telephone='" + Telephone + '\'' +
", year=" + year +
", month=" + month +
", day=" + day +
'}';
}
@Override
public int compareTo(Person1 o) {
return this.name.compareTo(o.name);
}
}
IMainList是一个接口主要实现通过向上转型调用某个功能。
public interface IMainList {
public abstract void func(AddressList addressList);
}
定义了联系人数组用于存放联系人信息,和菜单功能。
public class AddressList {//通讯录
Person1[] person=new Person1[100];
public static int count=0;
public AddressList() {
}
public static void menu(){
System.out.println("=================");
System.out.println("1.删除联系人");
System.out.println("2.增加联系人");
System.out.println("3.展示已有联系人");
System.out.println("4.查找联系人");
System.out.println("5.排序联系人");
System.out.println("6.退出通讯录");
System.out.println("=================");
}
}
AddPerson类实现了IMainList接口,主要重写方法,增加联系人操作。
public class AddPerson implements IMainList {
@Override
public void func(AddressList addressList) {
Scanner scanner =new Scanner(System.in);
System.out.println("请输入姓名");
String name= scanner.nextLine();
System.out.println("请输入电话号");
String Telephone= scanner.nextLine();
System.out.println("请输入年份");
int year= scanner.nextInt();
System.out.println("请输入月份");
int month= scanner.nextInt();
System.out.println("请输入日期");
int day= scanner.nextInt();
Person1 per=new Person1(name,Telephone,year,month,day);
addressList.person[addressList.count]=per;
System.out.println("添加联系人成功");
addressList.count++;
}
}
删除联系人功能
public class DeleatePerson implements IMainList {
@Override
public void func(AddressList addressList) {
//删除联系人
Scanner scanner =new Scanner(System.in);
System.out.println("请输入要删除的联系人姓名:");
String n= scanner.nextLine();
for(int i=0;i<AddressList.count;i++){
if(n.equals(addressList.person[i].getName())){
for(int j=i;j<AddressList.count-1;j++){
addressList.person[j]=addressList.person[j+1];
}
break;
}
}
addressList.person[AddressList.count-1]=null;
AddressList.count-=1;
System.out.println("删除"+n+"联系人成功");
}
}
查找联系人功能
public class LookupPerson implements IMainList {
@Override
public void func(AddressList addressList) {
Scanner scanner =new Scanner(System.in);
System.out.println("请输入要查找的联系人->");
String n= scanner.nextLine();
int flag=0;
for(int i=0;i<=AddressList.count;i++){
if(n.equals(addressList.person[i].getName())){
System.out.println("找到了-->"+n+ " 联系人");
flag=1;
break;
}
}
if(flag==0){
System.out.println("找不到此联系人");
}
}
}
显示已有联系人的信息
public class ShowPerson implements IMainList {
@Override
public void func(AddressList addressList) {
for(int i=0;i<addressList.count;i++){
System.out.println(addressList.person[i].toString());
}
}
}
对联系人进行排序(通过年份排序)文章来源:https://www.toymoban.com/news/detail-512751.html
public class SortPerson implements IMainList {
@Override
public void func(AddressList addressList) {
Person1 person1=new Person1("1","222",1,1,1);
for(int i=0;i<AddressList.count-1;i++){
for(int j=0;j<AddressList.count-1-i;j++){
if(addressList.person[j].getYear()>addressList.person[j+1].getYear()){
person1=addressList.person[j];
addressList.person[j]=addressList.person[j+1];
addressList.person[j+1]=person1;
}
}
}
System.out.println("排序完成!");
}
}
文章来源地址https://www.toymoban.com/news/detail-512751.html
到了这里,关于Java实现通讯录的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!