07-11-2008, 03:45
|
|
|
חבר מתאריך: 22.10.08
הודעות: 15
|
|
ציטוט:
במקור נכתב על ידי דור
כבר ענו לך...
|
הינה התקדמות קטנה:
קוד:
//p.361 #1
import hsa.PhoneBook;
import hsa.Stdin;
public class BetterPhoneBook
{
public static void main (String[] args)
{
PhoneBook book;
int command;
book = new PhoneBook ();
System.out.println ("Enter a command");
System.out.println (" 0 - Quit");
System.out.println (" 1 - Add an entry");
System.out.println (" 2 - Look up and entry");
System.out.println (" 3 - Remove an entry");
System.out.println (" 4 - Edit an entry");
System.out.print ("Command: ");
command = Stdin.readInt ();
// Loop
while (command != 0)
{
if (command == 1)
{
// Add entry.
String name, phone, personName;
int a, i;
System.out.print ("Enter new name: ");
name = Stdin.readLine ();
personName = book.lookUp (name);
a = personName.length ();
for (i = 1; i <= a; i++)
{
if (a == 0)
{
System.out.print ("error");
break;
}
}
System.out.print ("Enter phone number: ");
phone = Stdin.readLine ();
book.add (name, phone);
System.out.println ("Added " + name + " to the phone book");
} // if (command == 1)
else if (command == 2)
{
// Look up entry.
String name, phone;
System.out.print ("Enter name: ");
name = Stdin.readLine ();
phone = book.lookUp (name);
if (phone.length () == 0)
{
System.out.println (name + " was not found " + "in phone book");
}
else
{
System.out.println (name + "'s phone " + "number is " + phone);
}
} // if (command == 2)
else if (command == 3)
{
// Delete entry.
String name, phone;
System.out.print ("Enter name to be deleted: ");
name = Stdin.readLine ();
phone = book.lookUp (name);
if (phone.length () == 0)
{
System.out.println (name + " was not found " + "in phone book");
}
else
{
book.remove (name);
System.out.println (name + " is deleted");
}
} // if (command == 3)
else if (command == 4)
{
// Edit entry.
String name, phone;
System.out.print ("Enter name to be edited: ");
name = Stdin.readLine ();
phone = book.lookUp (name);
if (phone.length () == 0)
{
System.out.println (name + " was not found " + "in phone book");
}
else
{
System.out.print ("Enter new phone number: ");
phone = Stdin.readLine ();
book.remove (name);
book.add (name, phone);
System.out.println (name + "'s phone " + "number is now " + phone);
}
} // if (command == 4)
else
{
System.out.println ("Illegal command. Command");
System.out.println ("must be from 0 to 4");
} // else
// Prompt user and read the command.
System.out.print ("Command: ");
command = Stdin.readInt ();
} // while (command != 0)
}
}
זה לא אומר "שגיאה" שמכניסים את אותו השם פעם שנייה.
למה?
|