| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 813 人关注过本帖
标题:求助加两个方法,关于通讯录
只看楼主 加入收藏
ldxydj
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-5-31
收藏
 问题点数:0 回复次数:7 
求助加两个方法,关于通讯录

编程完成通讯录程序。完成的功能主要有录入、删除、修改及查询。录入数据保存到文件中(注,程序启动初始以先读入以往录入数据) 这个只有 名字与号码,这里只有录入和查询,求助帮我加个删除和修改,时间紧迫我自己也没时间想,马上就要上交了。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.Serializable;

public class Person implements Comparable<Person>, Serializable {
// Constructor
public Person(String firstName, String surname) {
this.firstName = firstName;
this.surname = surname;
}

public String toString() {
return firstName + " " + surname;
}

// Compare Person objects
public int compareTo(Person person) {
int result = surname.compareTo(person.surname);
return result == 0 ? firstName.compareTo(((Person)person).firstName):result;
}

public boolean equals(Object person) {
return compareTo((Person)person) == 0;
}

public int hashCode() {
return 7*firstName.hashCode()+13*surname.hashCode();
}

// Read a person from the keyboard
public static Person readPerson() {
String firstName = null;
String surname = null;
try {
System.out.print("Enter first name: ");
firstName = keyboard.readLine().trim();
System.out.print("Enter surname: ");
surname = keyboard.readLine().trim();
} catch(IOException e) {
System.err.println("Error reading a name.");
e.printStackTrace();
System.exit(1);
}
return new Person(firstName,surname);
}


private static BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in));

private String firstName; // First name of person
private String surname; // Second name of person
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.Serializable;

class PhoneNumber implements Serializable {
public PhoneNumber(String areacode, String number) {
this.areacode = areacode;
this.number = number;
}

public String toString() {
return areacode + " " + number;
}

// Read a phone number from the keyboard
public static PhoneNumber readNumber() {
String area = null; // Stores the area code
String localcode = null; // Stores the local code
try {

System.out.print("Enter area code: ");
area = keyboard.readLine().trim();
System.out.print("Enter local code: ");
localcode = keyboard.readLine().trim();
System.out.print("Enter the number: ");
localcode += " "+keyboard.readLine().trim();
} catch(IOException e) {
System.err.println("Error reading a phone number.");
e.printStackTrace();
System.exit(1);
}
return new PhoneNumber(area,localcode);
}

private static BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in));
private String areacode;
private String number;
}
import java.io.Serializable;

class BookEntry implements Serializable {
public BookEntry(Person person, PhoneNumber number) {
this.person = person;
this.number = number;
}

public Person getPerson() {
return person;
}

public PhoneNumber getNumber() {
return number;
}

public String toString() {
return person.toString() + '\n' + number.toString();
}

// Read an entry from the keyboard
public static BookEntry readEntry() {
return new BookEntry(Person.readPerson(), PhoneNumber.readNumber());
}

private Person person;
private PhoneNumber number;
}
import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class FormattedInput
{

// Method to read an int value
public int readInt() throws InvalidUserInputException {
if (readToken() != tokenizer.TT_NUMBER) {
throw new InvalidUserInputException(" readInt() failed. "
+ "Input data not numeric");
}

if (tokenizer.nval > (double) Integer.MAX_VALUE
|| tokenizer.nval < (double) Integer.MIN_VALUE) {
throw new InvalidUserInputException(" readInt() failed. "
+ "Input outside range of type int ");
}

if (tokenizer.nval != (double) (int) tokenizer.nval) {
throw new InvalidUserInputException(" readInt() failed. "
+ "Input not an integer");
}
return (int) tokenizer.nval;
}

// Method to read a long value
public long readLong() throws InvalidUserInputException {
if (readToken() != tokenizer.TT_NUMBER) {
throw new InvalidUserInputException(" readLong() failed. "
+ "Input data not numeric");
}

if (tokenizer.nval > (double) Long.MAX_VALUE
|| tokenizer.nval < (double) Long.MIN_VALUE) {
throw new InvalidUserInputException(" readLong() failed. "
+ "Input outside range of type long ");
}

if (tokenizer.nval != (double) (long) tokenizer.nval) {
throw new InvalidUserInputException(" readLong() failed. "
+ "Input not an integer");
}
return (long) tokenizer.nval;
}

// Method to read a double value
public double readDouble() throws InvalidUserInputException {
if (readToken() != tokenizer.TT_NUMBER) {
throw new InvalidUserInputException(" readDouble() failed. "
+ "Input data not numeric");
}
return tokenizer.nval;
}

// Method to read a double value
public float readFloat() throws InvalidUserInputException {
if (readToken() != tokenizer.TT_NUMBER) {
throw new InvalidUserInputException(" readFloat() failed. "
+ "Input data not numeric");
}
return (float)tokenizer.nval;
}

// Method to read a string
public String readString() throws InvalidUserInputException {
if (readToken() == tokenizer.TT_WORD || ttype == '\"'
|| ttype == '\'') {
return tokenizer.sval;
} else {
throw new InvalidUserInputException(" readString() failed. "
+ "Input data is not a string");
}
}

// Helper method to read the next token
private int readToken() {
try {
ttype = tokenizer.nextToken();
return ttype;

} catch (IOException e) { // Error reading in nextToken()
e.printStackTrace(System.err);
System.exit(1); // End the program
}
return 0;
}

// Object to tokenize input from the standard input stream
private StreamTokenizer tokenizer = new StreamTokenizer(
new BufferedReader(
new InputStreamReader(System.in)));
private int ttype; // Stores the token type code
}
public class InvalidUserInputException extends Exception {
public InvalidUserInputException() {}

public InvalidUserInputException(String message) {
super(message);
}
}
public class TryPhoneBook {
public static void main(String[] args) {
PhoneBook book = new PhoneBook(); // The phone book
FormattedInput in = new FormattedInput(); // Keyboard input
Person someone;
for(;;) {
System.out.println("Enter 1 to enter a new phone book entry\n"+
"Enter 2 to find the number for a name\n"+
"Enter 9 to quit.");
int what = 0; // Stores input selection
try {
what = in.readInt();

} catch(InvalidUserInputException e) {
System.out.println(e.getMessage()+"\nTry again.");
continue;
}

switch(what) {
case 1:
book.addEntry(BookEntry.readEntry());
break;
case 2:
someone = Person.readPerson();
BookEntry entry = book.getEntry(someone);
if(entry == null) {
System.out.println("The number for " + someone +
" was not found. ");
} else {
System.out.println("The number for " + someone +
" is " + entry.getNumber());
}
break;
case 9:
System.out.println("Ending program.");
return;
default:
System.out.println("Invalid selection, try again.");
break;
}
}
}
}

[此贴子已经被作者于2007-7-15 9:11:54编辑过]

搜索更多相关主题的帖子: 通讯录 import java Person public 
2007-07-14 17:02
大嘴先生2
Rank: 1
等 级:新手上路
威 望:2
帖 子:815
专家分:0
注 册:2006-4-17
收藏
得分:0 
确实长!

骑白马的未必是王子,也可能是唐僧;有翅膀的未必是天使,也可能是鸟人。
2007-07-14 17:07
七月初七
Rank: 1
等 级:新手上路
帖 子:53
专家分:0
注 册:2007-7-1
收藏
得分:0 
不算长。。我们现在做的“固定资产”的项目。。我都写了40个类了。。才完成1/3.。

你是天上乌鸦在飞,我是地上野狗在追~^o^
2007-07-14 17:07
ldxydj
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-5-31
收藏
得分:0 
回复:(七月初七)[em11]不算长。。我们现在做的“固...

你是工作还是学习?

2007-07-14 17:13
大嘴先生2
Rank: 1
等 级:新手上路
威 望:2
帖 子:815
专家分:0
注 册:2006-4-17
收藏
得分:0 
编程完成通讯录程序。完成的功能主要有录入、删除、修改及查询。录入数据保存到文件中(注,程序启动初始以先读入以往录入数据) 这个只有 名字与号码,这里只有录入和查询,求助帮我加个删除和修改,时间紧迫我自己也没时间想,马上就要上交了

这样大家不就看到了你要加什么了吗?!

骑白马的未必是王子,也可能是唐僧;有翅膀的未必是天使,也可能是鸟人。
2007-07-14 17:28
七月初七
Rank: 1
等 级:新手上路
帖 子:53
专家分:0
注 册:2007-7-1
收藏
得分:0 
学习啊。。汗

你是天上乌鸦在飞,我是地上野狗在追~^o^
2007-07-14 17:57
ldxydj
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-5-31
收藏
得分:0 

能不能有哪个大侠管我一下啊。

2007-07-15 15:59
ldxydj
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-5-31
收藏
得分:0 

纯属灌水

2007-07-16 16:46
快速回复:求助加两个方法,关于通讯录
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034673 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved