22-10-2008, 05:20
|
|
|
חבר מתאריך: 22.10.08
הודעות: 15
|
|
[JAVA] עזרה בכמה דברים בספר טלפונים
קודם כל, אני חדש כאן.
לעניין: עשיתי מן ספר טלפונים ואני רוצה לדעת איך אפשר לעשות שכשה המשתמש מכניס את אותו השם פעמיים, התוכנה תראה שגיאה "כבר יש שם כזה"
הינה הקוד:
קוד:
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;
System.out.print ("Enter new name: ");
name = Stdin.readLine ();
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)
}
}
|