求一个java电话本小程序!用控制台运行就成!
3个回答
展开全部
本来昨天就给你写好了,结果断网了没法提交,今天提交上来
主要写了两个类,Person类封装数据,PhoneBook类封装操作方法
1.封装数据Person.java
public class Person {
private String name;
private String number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
2.封装操作方法 PhoneBook.java
按照姓名或号码查找、删除、修改,显示所有,添加Person对象
import java.util.Vector;
public class PhoneBook {
Vector<Person> phooeBookVector=new Vector<Person>();
//按照姓名查找电话本
public Person queryByName(String name) {
for (Person person : phooeBookVector) {
if (person.getName().equals(name)) {
return person;
}
}
return null;
}
//按照号码查找电话本
public Person queryByNumber(String number) {
for (Person person : phooeBookVector) {
if (person.getNumber().equals(number)) {
return person;
}
}
return null;
}
//显示所有电话本记录
public void displayAll() {
for (Person person : phooeBookVector) {
System.out.print("姓名:"+person.getName());
System.out.println("号码:"+person.getNumber());
}
}
//按照姓名删除电话本
public boolean delByName(String name) {
for (Person person : phooeBookVector) {
if (person.getName().equals(name)) {
phooeBookVector.remove(person);
return true;
}
}
return false;
}
//按照号码删除电话本
public boolean delByNumber(String number) {
for (Person person : phooeBookVector) {
if (person.getNumber().equals(number)) {
phooeBookVector.remove(person);
return true;
}
}
return false;
}
//以姓名修改电话本
public boolean updateByName(String name,Person person) {
for (Person tmpPerson : phooeBookVector) {
if (tmpPerson.getName().equals(name)) {
phooeBookVector.remove(tmpPerson);
phooeBookVector.add(person);
return true;
}
}
return false;
}
//以号码修改电话本
public boolean updateByNumber(String number,Person person) {
for (Person tmpPerson : phooeBookVector) {
if (tmpPerson.getNumber().equals(number)) {
phooeBookVector.remove(tmpPerson);
phooeBookVector.add(person);
return true;
}
}
return false;
}
//添加Person本到Vector
public boolean addPhoneBook(Person phoneBook) {
return this.phooeBookVector.add(phoneBook);
}
//获取Person对象
public Vector<Person> getPhoneBook() {
return this.phooeBookVector;
}
}
3.一个简单的测试类,由于方法太多没有完全测试
import java.util.Scanner;
public class PhoneBookTest {
/**
* @param args
*/
public static void main(String[] args) {
PhoneBook phoneBook = new PhoneBook();
Scanner sc = new Scanner(System.in);
// 自动循环添加10条记录
for (int i = 0; i < 10; i++) {
Person person = new Person();
person.setName("woaibb2005_" + i);
person.setNumber("12345678" + i);
phoneBook.addPhoneBook(person);
}
//手动循环添加5条记录
for (int i = 0; i < 5; i++) {
Person person = new Person();
System.out.println("输入姓名"+i+":");
person.setName(sc.next());
System.out.println("输入号码"+i+":");
person.setNumber(sc.next());
phoneBook.addPhoneBook(person);
}
// 显示出所有记录
phoneBook.displayAll();
// 按照姓名查找
Person testBook = new Person();
testBook = phoneBook.queryByName("woaibb2005_5");
if (null != testBook)
System.out.println("testBook:" + testBook.getName() + "|"
+ testBook.getNumber());
else
System.out.println("没有该条记录");
// 按照号码删除
boolean delResult = phoneBook.delByNumber("123456783");
if (delResult)
System.out.println("删除成功");
else
System.out.println("删除失败");
// 删除后查询123456783记录
testBook = phoneBook.queryByNumber("123456783");
if (null != testBook)
System.out.println("testBook:" + testBook.getName() + "|"
+ testBook.getNumber());
else
System.out.println("没有该条记录");
}
}
主要写了两个类,Person类封装数据,PhoneBook类封装操作方法
1.封装数据Person.java
public class Person {
private String name;
private String number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
2.封装操作方法 PhoneBook.java
按照姓名或号码查找、删除、修改,显示所有,添加Person对象
import java.util.Vector;
public class PhoneBook {
Vector<Person> phooeBookVector=new Vector<Person>();
//按照姓名查找电话本
public Person queryByName(String name) {
for (Person person : phooeBookVector) {
if (person.getName().equals(name)) {
return person;
}
}
return null;
}
//按照号码查找电话本
public Person queryByNumber(String number) {
for (Person person : phooeBookVector) {
if (person.getNumber().equals(number)) {
return person;
}
}
return null;
}
//显示所有电话本记录
public void displayAll() {
for (Person person : phooeBookVector) {
System.out.print("姓名:"+person.getName());
System.out.println("号码:"+person.getNumber());
}
}
//按照姓名删除电话本
public boolean delByName(String name) {
for (Person person : phooeBookVector) {
if (person.getName().equals(name)) {
phooeBookVector.remove(person);
return true;
}
}
return false;
}
//按照号码删除电话本
public boolean delByNumber(String number) {
for (Person person : phooeBookVector) {
if (person.getNumber().equals(number)) {
phooeBookVector.remove(person);
return true;
}
}
return false;
}
//以姓名修改电话本
public boolean updateByName(String name,Person person) {
for (Person tmpPerson : phooeBookVector) {
if (tmpPerson.getName().equals(name)) {
phooeBookVector.remove(tmpPerson);
phooeBookVector.add(person);
return true;
}
}
return false;
}
//以号码修改电话本
public boolean updateByNumber(String number,Person person) {
for (Person tmpPerson : phooeBookVector) {
if (tmpPerson.getNumber().equals(number)) {
phooeBookVector.remove(tmpPerson);
phooeBookVector.add(person);
return true;
}
}
return false;
}
//添加Person本到Vector
public boolean addPhoneBook(Person phoneBook) {
return this.phooeBookVector.add(phoneBook);
}
//获取Person对象
public Vector<Person> getPhoneBook() {
return this.phooeBookVector;
}
}
3.一个简单的测试类,由于方法太多没有完全测试
import java.util.Scanner;
public class PhoneBookTest {
/**
* @param args
*/
public static void main(String[] args) {
PhoneBook phoneBook = new PhoneBook();
Scanner sc = new Scanner(System.in);
// 自动循环添加10条记录
for (int i = 0; i < 10; i++) {
Person person = new Person();
person.setName("woaibb2005_" + i);
person.setNumber("12345678" + i);
phoneBook.addPhoneBook(person);
}
//手动循环添加5条记录
for (int i = 0; i < 5; i++) {
Person person = new Person();
System.out.println("输入姓名"+i+":");
person.setName(sc.next());
System.out.println("输入号码"+i+":");
person.setNumber(sc.next());
phoneBook.addPhoneBook(person);
}
// 显示出所有记录
phoneBook.displayAll();
// 按照姓名查找
Person testBook = new Person();
testBook = phoneBook.queryByName("woaibb2005_5");
if (null != testBook)
System.out.println("testBook:" + testBook.getName() + "|"
+ testBook.getNumber());
else
System.out.println("没有该条记录");
// 按照号码删除
boolean delResult = phoneBook.delByNumber("123456783");
if (delResult)
System.out.println("删除成功");
else
System.out.println("删除失败");
// 删除后查询123456783记录
testBook = phoneBook.queryByNumber("123456783");
if (null != testBook)
System.out.println("testBook:" + testBook.getName() + "|"
+ testBook.getNumber());
else
System.out.println("没有该条记录");
}
}
展开全部
我有 里面还填加了回收站的程序 想要QQ447921868 我明天晚上9点放学以后传给你 其实你要是了解的话很好写的不是很难 可以先写出个框架来 在填写内部内容 一般只用5个类就可以分别是:1:Menu class 2:Operate.class 3:Person.class 4:Regex.class 5:App.class 有了这几个框架就好写多了 这个电话本是个越级段 自己写出来会很有感悟的~~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//就是这个啦~~~ ,优化完毕
import java.util.Scanner;
import java.util.Vector;
public class PhoneBook {
Vector<Person> pVec = new Vector<Person>();
static Scanner san = new Scanner(System.in);
public void add() {
System.out.print("输入姓名: ");
String name = san.next();
System.out.print("输入号码: ");
String phone = san.next();
Person p = new Person(name, phone);
pVec.add(p);
begin();
}
public void select() {
System.out.print("输入查找的用户名: ");
String name = san.next();
for(int i = 0; i < pVec.size(); i ++) {
Person p = pVec.get(i);
if(name.equals(p.getName())) {
System.out.println(p);
}
}
begin();
}
public void delete() {
System.out.print("输入删除的用户名: ");
String name = san.next();
for(int i = 0; i < pVec.size(); i ++) {
Person p = pVec.get(i);
if(name.equals(p.getName())) {
pVec.remove(i);
//System.out.println(p);
}
}
begin();
}
public void begin() {
System.out.print("1.添加用户 2.查找用户 3.显示全部 4.退出 5.删除用户:");
int op = san.nextInt();
switch(op) {
case 1:
add();
break;
case 2:
select();
break;
case 3:
printAll();
break;
case 4:
exit();
break;
case 5:
delete();
break;
default :
exit();
}
}
public void printAll() {
for(Person p: pVec){
System.out.println(p);
}
begin();
}
public void exit() {
System.exit(0);
}
public static void main(String[] args) {
PhoneBook pb = new PhoneBook();
System.out.println("欢迎使用简易电话本程序");
System.out.println("---------------------------------");
pb.begin();
}
}
class Person{
private String name;
private String phone;
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Person(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String toString() {
return this.getName() + "的联系方式是: " +this.getPhone();
}
}
import java.util.Scanner;
import java.util.Vector;
public class PhoneBook {
Vector<Person> pVec = new Vector<Person>();
static Scanner san = new Scanner(System.in);
public void add() {
System.out.print("输入姓名: ");
String name = san.next();
System.out.print("输入号码: ");
String phone = san.next();
Person p = new Person(name, phone);
pVec.add(p);
begin();
}
public void select() {
System.out.print("输入查找的用户名: ");
String name = san.next();
for(int i = 0; i < pVec.size(); i ++) {
Person p = pVec.get(i);
if(name.equals(p.getName())) {
System.out.println(p);
}
}
begin();
}
public void delete() {
System.out.print("输入删除的用户名: ");
String name = san.next();
for(int i = 0; i < pVec.size(); i ++) {
Person p = pVec.get(i);
if(name.equals(p.getName())) {
pVec.remove(i);
//System.out.println(p);
}
}
begin();
}
public void begin() {
System.out.print("1.添加用户 2.查找用户 3.显示全部 4.退出 5.删除用户:");
int op = san.nextInt();
switch(op) {
case 1:
add();
break;
case 2:
select();
break;
case 3:
printAll();
break;
case 4:
exit();
break;
case 5:
delete();
break;
default :
exit();
}
}
public void printAll() {
for(Person p: pVec){
System.out.println(p);
}
begin();
}
public void exit() {
System.exit(0);
}
public static void main(String[] args) {
PhoneBook pb = new PhoneBook();
System.out.println("欢迎使用简易电话本程序");
System.out.println("---------------------------------");
pb.begin();
}
}
class Person{
private String name;
private String phone;
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Person(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String toString() {
return this.getName() + "的联系方式是: " +this.getPhone();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询