每日一言
泪眼问花花不语,乱红飞过秋千去。 —欧阳修-
简单的通讯录实现,跟写Java实现图书管理系统差不多,用到的知识也差不多,就当个小练习,练习一下写Java程序的手感。
Java实现图书管理系统
关于通讯录的代码都写在下面了
1. 代码
1.1 contact 包
1.1.1 Contact
package contact;public class Contact {private int size;//当前占用容量private Person[] people = new Person[3];public int getSize() {return size;}public void setSize(int size) {this.size = size;}public Person getPerson(int pos) {return people[pos];}public void setPerson(Person person,int pos) {this.people[pos] = person;}public Boolean isFull(Contact contact) {return contact.getSize() == people.length;}
}
1.1.2 Person
package contact;public class Person {private String name;private String sex;private int age;private String telephone;public String getName() {return name;}public int getAge() {return age;}@Overridepublic String toString() {return "Person{" +"名字='" + name + '\'' +", 性别='" + sex + '\'' +", 年龄=" + age +", 电话='" + telephone + '\'' +'}';}public Person(String name, String sex, int age, String telephone) {this.name = name;this.sex = sex;this.age = age;this.telephone = telephone;}
}
1.2 option 包
1.2.1 AddContact
package option;import contact.Contact;
import contact.Person;import java.util.Scanner;public class AddContact implements IOperation{@Overridepublic void work(Contact contact) {if(contact.isFull(contact)) {System.out.println("通讯录已满,无法添加!");return;}else {Scanner scanner = new Scanner(System.in);System.out.println("请输入联系人姓名:>");String name = scanner.nextLine();System.out.println("请输入联系人性别:>");String sex = scanner.nextLine();System.out.println("请输入联系人年龄:>");int age = scanner.nextInt();System.out.println("请输入联系人电话:>");String telephone = scanner.next();Person person = new Person(name,sex,age,telephone);int size = contact.getSize();contact.setPerson(person,size);contact.setSize(size+1);System.out.println("添加成功!");}}}
1.2.2 DelContact
package option;import contact.Contact;import java.util.Scanner;public class DelContact implements IOperation{@Overridepublic void work(Contact contact) {int size = contact.getSize();if(0 == size) {System.out.println("通讯录为空,无法删除!");return;}else {System.out.println("请输入要删除的联系人姓名");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();for (int i = 0; i < size; i++) {if(name.equals(contact.getPerson(i).getName())) {//删除for (int j = i; j < size-1; j++) {contact.setPerson(contact.getPerson(j+1),j);}contact.setSize(size-1);System.out.println("删除成功!");return;}}System.out.println("没有你要删除的联系人!");}}
}
1.2.3 ExitContact
package option;import contact.Contact;public class ExitContact implements IOperation{@Overridepublic void work(Contact contact) {//释放占用内存for (int i = 0; i < contact.getSize(); i++) {contact.setPerson(null,i);}System.out.println("退出成功!");System.exit(0);}
}
1.2.4 FindContact
package option;import contact.Contact;import java.util.Scanner;public class FindContact implements IOperation{@Overridepublic void work(Contact contact) {int size = contact.getSize();System.out.println("请输入要查找的联系人姓名");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();for (int i = 0; i < size; i++) {if(name.equals(contact.getPerson(i).getName())) {System.out.println(contact.getPerson(i).toString());return;}}System.out.println("没有你要查找的联系人!");}
}
1.2.5 IOperation 接口
package option;import contact.Contact;public interface IOperation {void work(Contact contact);
}
1.2.6 ModifyContact
package option;import contact.Contact;
import contact.Person;import java.util.Scanner;public class ModifyContact implements IOperation{@Overridepublic void work(Contact contact) {int size = contact.getSize();System.out.println("请输入要修改的联系人姓名");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();for (int i = 0; i < size; i++) {if(name.equals(contact.getPerson(i).getName())) {//修改System.out.println("请输入联系人姓名:>");String name1 = scanner.nextLine();System.out.println("请输入联系人性别:>");String sex = scanner.nextLine();System.out.println("请输入联系人年龄:>");int age = scanner.nextInt();System.out.println("请输入联系人电话:>");String telephone = scanner.next();Person person = new Person(name1,sex,age,telephone);contact.setPerson(person,i);System.out.println("修改成功!");return;}}System.out.println("没有你要修改的联系人!");}
}
1.2.7 ShowContact
package option;import contact.Contact;public class ShowContact implements IOperation{@Overridepublic void work(Contact contact) {if(0 == contact.getSize()) {System.out.println("通讯录为空");return;}else {for (int i = 0; i < contact.getSize(); i++) {System.out.println(contact.getPerson(i).toString());}}}
}
1.3 Main
import contact.Contact;
import option.AddContact;import java.util.Scanner;public class Main {public static int 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("0.退出通讯录");System.out.println("*********************");System.out.println("请输入选项:>");Scanner scanner = new Scanner(System.in);return scanner.nextInt();}public static void main(String[] args) {int op;User user = new User();Contact contact = new Contact();do {op = menu();if(op > user.iOperation.length || op < 0) {System.out.println("输入错误!");continue;}user.doOperation(contact,op);}while (0 != op);}
}
1.4 User
import contact.Contact;
import option.*;public class User {IOperation[] iOperation = new IOperation[]{new ExitContact(),new AddContact(),new DelContact(),new ShowContact(),new FindContact(),new ModifyContact(),};public void doOperation(Contact contact,int op) {this.iOperation[op].work(contact);}
}
2. 效果
2.1 添加联系人
2.2 展示联系人
2.3 删除联系人
2.4 查找联系人
2.5 修改联系人
2.6 退出通讯录
结语
请给自己些耐心,一口吃不成胖子。
山外青山楼外楼,莫把百尺当尽头。
保持空杯心态加油努力吧!
都看到这里啦!真棒(*^▽^*)
可以给作者一个免费的赞赞吗,这将会鼓励我继续创作,谢谢大家
编程小白写作,如有纰漏或错误,欢迎指正