Sie sind auf Seite 1von 3

/**Creates an AddressBook that contains 100 AddressBookEntries*/

import java.io.*;
public class AddressBook
{
//index of last entry
private int top = 0;
private static final int MAXENTRIES = 100;
private AddressBookEntry[] list;

/**The main method*/


public static void main(String [] args){
BufferedReader keyIn = new BufferedReader (new InputStreamReader(System.in));
AddressBook addBook = new AddressBook();
String act = "";
while(true){
//displays the options
System.out.println("\n[A] Add entry");
System.out.println("\n[D] Delete entry");
System.out.println("\n[V] View all entries");
System.out.println("\n[U] Update entry");
System.out.println("\n[Q] Quit");
System.out.println("\n[A] Enter Desired Action: ");
try
{act = keyIn.readLine();}
catch(Exception e)
{System.out.println("Error");}
//checks for the appropriate action for his choice
if(act.equalsIgnoreCase("A"))
addBook.addEntry();
else if(act.equalsIgnoreCase("D"))
addBook.delEntry();
else if(act.equalsIgnoreCase("V"))
addBook.viewEntries();
else if(act.equalsIgnoreCase("U"))
addBook.updateEntry();
else if(act.equalsIgnoreCase("Q"))
System.exit(0);
else
System.out.println("Unknown command");
}
}
/**end of main method**/

/**Creates the AddressBook*/


public AddressBook(){
list = new AddressBookEntry[MAXENTRIES];
}

/**method for adding an AddressBookEntry to the AddressBook */


public void addEntry(){
BufferedReader keyIn = new BufferedReader (new
InputStreamReader(System.in));
String name = "";
String address = "";
int telNumber = 0;
String emailAdd= "";
if(top == MAXENTRIES)
{
System.out.println("Address Book is full");
return;
}
//asks the user for the data of the AddressBook
try{
System.out.print("Name: ");
name = keyIn.readLine();
System.out.print("Address: ");
address = keyIn.readLine();
System.out.print("Telephone Number: ");
telNumber = Integer.parseInt(keyIn.readLine());
System.out.print("Email Address: ");
emailAdd = keyIn.readLine();
}
catch(Exception e){
System.out.println(e);
System.exit(0);
}

AddressBookEntry entry = new AddressBookEntry(name, address, telNumber,


emailAdd);
list[top] = entry;
top++;
}
/**method that deletes an AddressBookEntry from the AddressBook with the index*/
public void delEntry(){
BufferedReader keyIn = new BufferedReader (new
InputStreamReader(System.in));
int index = 0;
//checksif the AddressBook is empty
if (top==0){
System.out.println("Address Book is empty");
return;
}
//asks for the entry which is to be deleted
try{
//shows the current entries on the record book
viewEntries();

System.out.println("\nEnter entry number: ");


index = Integer.parseInt(keyIn.readLine())-1;
}catch(Exception e){}

//checks if the index is within bounds

if(index < 0 || index >=top){


System.out.println("Index out of Bounds");
return;
}
else{
for(int i=index; i<top;i++){
list[i] = list [i+1];
}
list[top] = null;
top--;
}
}
/**method that prints all the entries in the AddressBook*/
public void viewEntries(){
for(int index = 0;index<top;index++){
System.out.println((index+1) + "\t Name: " +
list[index].getName());
System.out.println("\t Address: " + list[index].getAddress());
System.out.println("\t Tel Num: " + list[index].getTelNumber());
System.out.println("\t Email: " + list[index].getEmailAdd());
}
}
/***method that updates an entry*/
public void updateEntry(){
BufferedReader keyIn = new BufferedReader (new
InputStreamReader(System.in));

int index = 0;
String name = "";
String address = "";
int telNumber = 0;
String emailAdd = "";

//asks for the entries data


try{

System.out.print("Entry Number: ");


index = Integer.parseInt(keyIn.readLine())-1;
System.out.print("Name: ");
name = keyIn.readLine();
System.out.print("Address: ");
address = keyIn.readLine();
System.out.print("Tel Num: ");
telNumber = Integer.parseInt(keyIn.readLine());
System.out.print("Email Address: ");
emailAdd = keyIn.readLine();
}
catch(Exception e){
System.out.println(e);
System.exit(0);
}

//updates the entry


AddressBookEntry entry = new AddressBookEntry(name, address, telNumber,
emailAdd);
list[index] = entry;
}
}

Das könnte Ihnen auch gefallen