用JAVA编手机通讯录
编一个手机通讯录,刚开始显示你有6个联系人,然后显示是否关掉手机,不关掉就先显示第一个联系
人的信息,然后你可以你可以通过前进,后退选择其他联系人,并且你可以呼叫,发短信,编辑
你需要在这个程序里填上所需的代码import java.util.Scanner;
public class Ass
{
private MobileFacilities myPhone;
private boolean tracing;
private Scanner sc;
public CallOrText()
{
}
public void intro()
{
}
public void use()
{
}
public void trace(String message)
{
if(tracing)
{
System.out.println("CallOrText: " + message);
}
}
}
以下是你所需要获取的信息,你可以调用里面的信息
import java.util.Random;
import java.util.Scanner;
public class MobileFacilities
{
//contacts (names, gender, home number, mobile number, birthdates)
private String[] names={"Minnie Mouse", "Homer Simpson", "Eric Cartman",
"Bob the Builder", "Lightning McQueen", "Dorothy the Dinosaur"};
private char[] genders={'f', 'm', 'm',
'm', 'm', 'f'};
private String[] homeNumbers={"0363654321", "0364192834", "0362262001",
"", "", "0298341333"};
private String[] mobileNumbers={"0418123456", "", "",
"0898356455", "0292773344", ""};
private String[] birthDates={"19380629", "19700110", "20010802",
"19830321", "", ""};
private boolean tracing;
private Random generator;
private int currentContact;
private String msg;
private int msgLength=0;
public MobileFacilities()
{
tracing = true;
generator=new Random();
currentContact=1;
msg="";
msgLength=0;
}
public void setUp(int seed)
{
trace("setUp() called");
if (seed ==0)
{
generator.setSeed(1234);
trace("setUp: rng seeded");
}
trace("setUp() left");
}
public int getNumContacts()
{
trace("getNumContacts() called and exited with return value of " + names.length);
return names.length;
}
public int getCurrentNumber()
{
trace("getCurrentNumber() called and exited with return value of " + currentContact);
return currentContact;
}
public String getCurrentName()
{
trace("getCurrentName() called and exited with return value of " + names[currentContact-1]);
return names[currentContact-1];
}
public char getCurrentGender()
{
trace("getCurrentGender() called and exited with return value of " + genders[currentContact-1]);
return genders[currentContact-1];
}
public String getCurrentHomeNumber()
{
trace("getCurrentHomeNumber() called and exited with return value of " + homeNumbers[currentContact-1]);
return homeNumbers[currentContact-1];
}
public String getCurrentMobileNumber()
{
trace("getCurrentMobileNumber() called and exited with return value of " + mobileNumbers[currentContact-1]);
return mobileNumbers[currentContact-1];
}
public String getCurrentBirthdate()
{
trace("getCurrentBirthdate() called and exited with return value of " + birthDates[currentContact-1]);
return birthDates[currentContact-1];
}
public void getPrevious()
{
trace("getPrevious() called");
if (currentContact == 1) {
trace("getPrevious: rolled back to end");
currentContact=names.length;
}
else {
trace("getPrevious: contact retreated");
currentContact--;
}
trace("getPrevious() left");
}
public void getNext()
{
trace("getNext() called");
if (currentContact == names.length)
{
trace("getNext: rolled back to start");
currentContact=1;
}
else
{
trace("getNext: contact advanced");
currentContact++;
}
trace("getNext() left");
}
public boolean call()
{
trace("call() called");
System.out.print("Calling ");
if (! homeNumbers[currentContact-1].equals(""))
{
trace("call: displaying home number");
System.out.println("(H) " + homeNumbers[currentContact-1]+"...");
}
else
{
trace("call: displaying mobile number");
System.out.println("(M) " + mobileNumbers[currentContact-1]+"...");
}
if (generator.nextInt(100)<65) //person unavailable 65% of the time
{
trace("call() left with return value: false");
return false;
}
else
{
trace("call() left with return value: false");
return true;
}
}
public void converse(Scanner sc)
{
String line;
trace("converse() called");
System.out.println("Hello?");
line=sc.nextLine();
System.out.println("Who's this?");
line=sc.nextLine();
System.out.println("How'd you get this number?");
line=sc.nextLine();
System.out.println("Don't call me at work! Bye!");
trace("converse() left");
}
public void voiceMail(Scanner sc)
{
String line;
trace("voiceMail() called");
System.out.println("You have reached the voicemail for " + names[currentContact-1] + ".");
System.out.println("Please enter your message...");
line=sc.nextLine();
System.out.println("Message stored.");
trace("voiceMail() left");
}
public boolean newMsg()
{
trace("newMsg() called");
msg="";
msgLength=0;
if (mobileNumbers[currentContact-1].equals(""))
{
trace("newMsg() left with return value: false");
return false;
}
else
{
trace("newMsg() left with return value: false");
return true;
}
}
public void addMsgLine(String line)
{
int origLength;
trace("addMsgLine() called");
origLength=msg.length();
msg=msg + "\n" + line;
msgLength++;
trace("addMsgLine() left");
}
public void sendMsg()
{
int origLength;
trace("sendMsg() called");
origLength=msg.length();
System.out.println(msgLength + " line message sent.");
trace("sendMsg() left");
}
public boolean changeName(String name)
{
trace("changeName() called");
if (name.equals(""))
{
trace("changeName() left with return value: false");
return false;
}
else
{
names[currentContact-1]=name;
trace("changeName() left with return value: true");
return true;
}
}
public boolean changeGender(char gender)
{
trace("changeGender() called");
gender=Character.toLowerCase(gender);
if ((gender != 'm') && (gender != 'f'))
{
trace("changeGender() left with return value: false");
return false;
}
else
{
genders[currentContact-1]=gender;
trace("changeGender() left with return value: true");
return true;
}
}
public boolean checkNumber(String number)
{
trace("checkNumber() called");
if (number.length() != 10)
{
trace("checkNumber() left with return value: false");
return false;
}
else
{
for (int i=0; i<number.length(); i++)
{
if (! Character.isDigit(number.charAt(i)))
{
trace("checkNumber() left with return value: false");
return false;
}
}
trace("checkNumber() left with return value: true");
return true;
}
}
public boolean changeHomeNumber(String number)
{
trace("changeHomeNumber() called");
if (! checkNumber(number))
{
trace("changeHomeNumber() left with return value: false");
return false;
}
else
{
homeNumbers[currentContact-1]=number;
trace("changeHomeNumber() left with return value: true");
return true;
}
}
public boolean changeMobileNumber(String number)
{
trace("changeMobileNumber() called");
if (! checkNumber(number))
{
trace("changeMobileNumber() left with return value: false");
return false;
}
else
{
mobileNumbers[currentContact-1]=number;
trace("changeMobileNumber() left with return value: true");
return true;
}
}
public boolean changeBirthDate(String date)
{
trace("changeBirthDate() called");
if (! checkDate(date))
{
trace("changeBirthDate() left with return value: false");
return false;
}
else
{
birthDates[currentContact-1]=date;
trace("changeBirthDate() left with return value: true");
return true;
}
}
public boolean checkDate(String date)
{
final int[] days={31,28,31,30,31,30,31,31,30,31,30,31};
int year;
int month;
int day;
int limit;
trace("checkDate() called");
if (date.length() != 8)
{
trace("checkDate() left with return value: false");
return false;
}
else
{
trace("checkDate: length ok");
for (int i=0; i<date.length(); i++)
{
if (! Character.isDigit(date.charAt(i)))
{
trace("checkDate() left with return value: false");
return false;
}
}
trace("checkDate: numbers only");
month=Integer.valueOf(date.substring(4,6));
if ((month == 0) || (month > 12))
{
trace("checkDate() left with return value: false");
return false;
}
else
{
limit=days[month-1];
day=Integer.valueOf(date.substring(6,8));
if (month == 2)
{
trace("checkDate: February");
year=Integer.valueOf(date.substring(0,4));
if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)))
{
trace("checkDate: leap year");
limit++;
}
}
if ((day == 0) || (day > limit))
{
trace("checkDate() left with return value: false");
return false;
}
}
trace("checkDate() left with return value: true");
return true;
}
}
public void setTracing(boolean on)
{
tracing = on;
}
public void trace(String message)
{
if (tracing)
{
System.out.println("MobileFacilities: " + message);
}
}
}