Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
AddressBookEntry class

C#
public class AddressBookEntry {
        // TODO Address Book Entry
        private String name;
        private String address;
        private String emailAdd;
        private String telNo;

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getEmailAdd() {
            return emailAdd;
        }

        public void setEmailAdd(String emailAdd) {
            this.emailAdd = emailAdd;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getTelNo() {
            return telNo;
        }

        public void setTelNo(String telNo) {
            this.telNo = telNo;
        }
    }



Address Book Class


public class AddressBook {
		// TODO Address Book 
	
			private int size;
			private AddressBookEntry list[];

			public AddressBook(int size) {
				// variable size addressbook
				this.size = size;
				list = new AddressBookEntry[size];
			}

			public AddressBook() {
				// if size not specified, default no. of entries is 100
				size = 100;
				list = new AddressBookEntry[size];
			}

			public boolean addAddressBookEntry(AddressBookEntry entry) {
				for (int i = 0; i < list.length; i++) {
					// find next available empty slot
					if (list[i] == null) {
						list[i] = entry;
						return true;
					}
				}
				// return false if no slot available, addressbook is full
				return false;
			}

			public boolean deleteAddressBookEntry(int recordNo) {
				int validRecord = 0;
				// record number is greater than the index number by 1
				if (recordNo > 0 && (recordNo - 1) < size) {
					for (int i = 0; i < list.length; i++) {
						if (list[i] != null) {
							validRecord += 1;
						}
						if (validRecord == recordNo) {
							list[i] = null;
							return true; // return true if deletion is successfull
						}
					}
				}
				return false;
			}

			// find a specific record number using its record number
			public AddressBookEntry findAddressBookEntryByRecordNo(int recordNo) {
				if (recordNo > 0 && (recordNo - 1) < size) {
					if (list[recordNo - 1] != null) {
						return list[recordNo - 1];
					}
				}
				return null;
			}

			public boolean updateAddressBookEntry(int recordNo, AddressBookEntry entry) {
				if (recordNo > 0 && (recordNo - 1) < size) {
					list[recordNo - 1] = entry;
				}
				return false;
			}

			public AddressBookEntry[] getAllEntries() {
				return list;
			}
	}



AddressBookApp Class



C#
import java.lang.reflect.Method;
import java.util.Scanner;
public class AddressBookApp {
    private static Scanner dataReader;
    private static AddressBook book;

    // TODO Address Book App

    public static void main(String[] args) {
        book = new AddressBook(10);
        dataReader = new Scanner(System.in);

        boolean lContinue = true;
        while (lContinue) {
            switch (Character.toUpperCase(menu())) {
            case '1': addBookEntry(); break;
            case '2': deleteEntry(); break;
            case '3': viewAllEntries(); break;
            case '4': editEntry(); break;
            case '5': searchEntryByName(); break;
            case '6': searchEntryByRecord(); break;
            case 'X':
                lContinue = false;
                break;
            default:
                System.out.println("\nInvalid Menu option");
            }
        }
        System.out.println("\nEnd of program...");
    }

    public static char menu() {
        char choice;
        System.out.println("\nAddressBook Menu");
        System.out.println("1. Add Entry");
        System.out.println("2. Delete Entry");
        System.out.println("3. View all Entries");
        System.out.println("4. Update an Entry");
        System.out.println("5. Search Entry By Name");
        System.out.println("6. Search Entry By Record Number");
        System.out.println("X. Exit Program");
        System.out.print("\nSelect Menu Option: ");
        choice = dataReader.nextLine().charAt(0);
        return choice;
    }

    public static AddressBookEntry getEntryDetails(AddressBookEntry entry) {
        if( entry == null ) {
            entry = new AddressBookEntry();
        }
        System.out.print("\nName     : "); entry.setName(dataReader.nextLine());
        System.out.print("Address  : "); entry.setAddress(dataReader.nextLine());
        System.out.print("Phone No.: "); entry.setTelNo(dataReader.nextLine());
        System.out.print("Email    : "); entry.setEmailAdd(dataReader.nextLine());
        return entry;
    }

    public static void addBookEntry() {
        AddressBookEntry entry = getEntryDetails(null);
        if( entry != null ) {
            book.addAddressBookEntry(entry);
        }
    }

    public static void editEntry() {

        // TODO: edit a single record entry
        System.out.println("\nUnder construction....");
    }

    public static void searchEntryByRecord() {
        // TODO: search an entry using its record no.
        // display "record not found" if such record does not exist.
        // Display all its entry.
        // Hint: use the method "findAddressBookEntryByRecordNo()"
        //       from the AddressBook class
        System.out.println("\nUnder construction....");
    }


    public static void deleteEntry() {

        // TODO: delete an entry using its record no.
        // display "record not found" if such record does not exist.
        // Hint: use the method "deleteAddressBookEntry()"
        //       from the AddressBook class
    }

    // display a single record
    public static void displayEntry(AddressBookEntry entry, int recNo) {
        System.out.println("\nRecord No. " + recNo);
        System.out.println("Name     : " + entry.getName());
        System.out.println("Address  : " + entry.getAddress());
        System.out.println("Phone No.: " + entry.getTelNo());
        System.out.println("Email    : " + entry.getEmailAdd());
    }

    // Search all entries containing name search criteria
    public static void searchEntryByName() {
        System.out.print("\nSearch[Name]: ");
        // ensure no extraneous space and search criteria all in lowercase
        String name = dataReader.nextLine().trim().toLowerCase();

        // get a reference to the Addressbook list
        AddressBookEntry[] list = book.getAllEntries();
        for( int i = 0; i < list.length; i++ ) {
            // compare search criteria with every entry
            if(list[i]!=null && list[i].getName().toLowerCase().contains(name)) {
                displayEntry(list[i],i+1);
            }
        }
        System.out.println("No more records follow...");
    }

    public static void viewAllEntries() {
        int validRecords = 0;

        // get a reference to the Addressbook list
        AddressBookEntry[] list = book.getAllEntries();
        if( list.length == 0) {
            System.out.println("\nList empty...");
        }

        for( int i = 0; i < list.length; i++ ) {
            if( list[i] != null ) {
                displayEntry(list[i],++validRecords);
            }
        }
        System.out.println("No more entries to follow...");
    }
}


Please Help me
Posted
Comments
Richard MacCutchan 24-Jul-15 8:47am    
Unless the file is inordinately large, you should read all the objects into a collection class in memory. you can then insert, delete and modify as much as you want, before writing the records to a file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900