Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that functions as an address book. It should have entries c

ID: 3691204 • Letter: W

Question

Write a Java program that functions as an address book. It should have entries containing at least the following information: first and last name, phone number, date of birth, address, and email address. You should be able to add entries and remove entries, as well as printing out any given entry. It should save entries to a file, and be able to read in entries from a file as well. The address book must be able to sort by at least one field (preferably last name). You may use any sort for this that you like. The address book will almost certainly contain a fixed limit on the total number of entries. However, it should be possible to increase this limit by simply changing a single line in your program and then recompiling with no other modifications. An excellent program will be able to sort the entries by any field (first name, last name, DOB, phone number, email address, etc). An excellent program will be able to display only entries matching a certain criteria (only last names beginning with the letter 'M', for example). An excellent program easily and intelligently handles the use of multiple address books (multiple files). A spectacular program features the ability to move an entry from one address book to another. 1) Load from file 2) Save to file 3) Add an entry 4) Remove an entry 5) Edit an existing entry 6) Sort the address book 7) Search for a specific entry 8) Quit Please choose what you'd like to do with the database: Just a recommendation: use a variable to keep track of the number of elements currently in the address book. This value will increase when you add new entries and decrease when you remove entries. This is the variable you will use in all your for loops for iterating through the array of records. Once this variable reaches the capacity of the array (arr.length), the address book is "full" and no new entries can be added. Bonus: when loading a file containing an address book, the program automatically checks if there is a friend's birthday within 3 days and prints on the screen all the close birthdays (birthdays that are today, then the ones that are tomorrow and lastly the ones that are the day after tomorrow). Bonus 2: Use encryption and passwords for the address book files in the following way: XOR has a property very useful in encryption: A XOR B=C; C XOR B=A, thus A is your unencrypted file, B is your password and C is the encrypted file. You will pick a password, say "computing" and you should perform XOR between each ASCII code of the password with each byte of the file: first character of the password with first byte of the file, second character in pwd with second character in file, etc. At the end of the PWD you go to the begining character of the password.

Explanation / Answer

Below are the code with comments to explain the working of the code.

import java.util.Scanner;

class ADDRESSBOOK{

    Scanner s = new Scanner(System.in);

    //Nested class for each entry

    class Entry{

        private String first;

        private String last;

        private String phone;

        private String dob;

        private String address;

        private String email;

        Entry(String first, String last, String phone, String dob, String address, String email){

            this.first = first;

            this.last = last;

this.address =phone;

this.address = dob;

            this.address = address;

            this.email = email;

        }

        Entry(){

            first = "";

            last = "";

            phone= "";

            dob = "";

            address = "";

            email = "";

        }

        public void readEntry(){

            System.out.println("First Name:"+first );

            System.out.println("Last Name:"+last );

    System.out.println("Phone:"+phone );

            System.out.println("DOB:"+dob );

            System.out.println("Address:"+address );

            System.out.println("Email:"+email );

        }

    }

    // how many entries are in the address book//

    private int entries = 0;

    Entry[] contents;

    public void initEntries(int e){

        contents = new Entry[e];

        for (int i = 0;i<contents.length;i++){      //Initializes an array of entries, then loops through to initialize each individual entry

            contents[i] = new Entry();

        }

    }

    public int getEntries(){

        return contents.length;

    }

    //Adds //

    public void add(String first, String last, String phone, String dob, String address, String email){

        if (entries<contents.length){

        contents[entries] = new Entry(first, last, phone, dob, address, email);

        entries++;

        }

        else System.out.println("Error: book is full");

    }

    //Removes//   

public void remove(int entry){

        if (entries>0){

            contents[entry] = new Entry();

            for (int i = 0;i<entries-entry;i++){

                if (entry+1==entries) contents[entry] = new Entry();

                else{

                    Entry temp = contents[entry+i];

                    contents[entry+i] = contents[entry+i+1]; //Removes an entry end moves each entry after it one backwards.

                    contents[entry+i+1] = temp;

                }

            }

            entries--;

            }

            else System.out.println("Error: book is empty.");

    }

    //edit//

    public void edit(String first, String last, String phone, String dob, String address, String email, int selection){

        contents[selection].first = first;

        contents[selection].last = last;

contents[selection].phone = phone;

        contents[selection].dob = dob;

        contents[selection].address = address;

        contents[selection].email = email;

    }

    //Sorts    //

    public void sort(int n){

        for(int i = 0;i<contents.length;i++){

            for (int j = 0;j<contents.length;j++){

                switch(n){

                    case 1:

                        if (contents[i].first.compareTo(contents[j].first)<0){

                            Entry temp = contents[i];

                            contents[i] = contents[j];

                            contents[j] = temp;

                        }

                        break;

                    case 2:

                        if (contents[i].last.compareTo(contents[j].last)<0){

                            Entry temp = contents[i];

                            contents[i] = contents[j];

                            contents[j] = temp;

                        }

                        break;

                    case 3:

                        if (contents[i].phone.compareTo(contents[j].phone)<0){

                            Entry temp = contents[i];

                            contents[i] = contents[j];

                            contents[j] = temp;

                        }

                        break;

                   case 4:

                        if (contents[i].dob.compareTo(contents[j].dob)<0){

                            Entry temp = contents[i];

                            contents[i] = contents[j];

                            contents[j] = temp;

                        }

                        break;

                    case 5:

                        if (contents[i].address.compareTo(contents[j].address)<0){

                            Entry temp = contents[i];

                            contents[i] = contents[j];

                            contents[j] = temp;

                        }

                        break;

                    case 6:

                        if (contents[i].email.compareTo(contents[j].email)<0){

                            Entry temp = contents[i];

                            contents[i] = contents[j];

                            contents[j] = temp;

                        }

                        break;

                    default:

                        System.out.println("Error: invalid field");

                        break;

                }

            }

        }

    }

//search//

    public void search(int m){

        for(int i = 0;i<contents.length;i++){

            for (int j = 0;j<contents.length;j++){

                switch(m){

                    case 1:

                        if (contents[i].first.compareTo(contents[j].first)<0){

System.out.print("first name found");

                        }

                        break;

                    case 2:

                        if (contents[i].last.compareTo(contents[j].last)<0){

System.out.print("last name found");

                        }

                        break;

                    case 3:

if (contents[i].phone.compareTo(contents[j].phone)<0){

                        System.out.print("phone number found");

                        }

                        break;

                   case 4:

                        if (contents[i].dob.compareTo(contents[j].dob)<0){

System.out.print("date of birth found");

                        }

                        break;

                    case 5:

                        if (contents[i].address.compareTo(contents[j].address)<0){

                            System.out.print("address found");

                        }

                        break;

                    case 6:

                        if (contents[i].email.compareTo(contents[j].email)<0){

                            System.out.print("email found");

                        }

                        break;

                    default:

                        System.out.println("Error: invalid field");

                        break;

                }

            }

        }

    }

//save//

    public void addFromCopy(Entry e){

        if (entries<contents.length){

            contents[entries] = e;

            entries++;

            }

            else System.out.println("Error: book is full");

    }

}

public class address {

    public static void main(String[] args){

        Scanner s = new Scanner(System.in);

        System.out.print("how many address books want to create? ");

        int howManyBooks;

        int howManyEntries;

        Book[] library = new Book[0];

        while(true){

            howManyBooks = s.nextInt();

            if (howManyBooks>0){

                library = new Book[howManyBooks];                                   break;

            }

            else System.out.print("You must create at least 1 book.");

            }

        for (int i=0;i<library.length;i++){

            library[i] = new Book();

            while(true){

                System.out.print("How many entries in this address book "+i+"? ");

                howManyEntries = s.nextInt();

                if (howManyEntries>0) {

                    library[i].initEntries(howManyEntries);                                     break;

                }

                else System.out.println("You must create at least 1 Entry.");

                }

        }

        boolean done = false;

        int selectedBook = 0;

        int selection;

        while (done==false){

            System.out.println("Book "+selectedBook+" is currently selected.");

            for (int i = 0;i<library[selectedBook].getEntries();i++){

                System.out.println("===========Entry "+i+" ===========");

                library[selectedBook].contents[i].readEntry();                System.out.println("================================");

            }

            System.out.println("Select an chose!");

            System.out.println("1. Insert an entry");

            System.out.println("2. Delete an entry");

            System.out.println("3. Update an entry");

            System.out.println("4. Sort ");

            System.out.println("5. Search Entry");

            System.out.println("6. save entry ");

            System.out.println("7. Exit the menu");

            System.out.print("> ");

            selection = s.nextInt();

            String first, last, phone, dob, address, email;

            switch(selection){

            case 1:

                System.out.print("First name? ");

                first = s.next();

                System.out.print("Last name? ");

                last = s.next();

                System.out.print("phone? ");

                phone = s.next();

                System.out.print("dob? ");

                dob = s.next();

                System.out.print("Address? ");

                address = s.next();

                System.out.print("Email? ");

                email = s.next();

                library[selectedBook].add(first, last,phone,dob, address, email);

                break;

            case 2:

                System.out.print("Remove which entry? ");

                int entry = s.nextInt();

                library[selectedBook].remove(entry);

                break;

            case 3:

                System.out.print("Edit which entry?");

                int whichEntry = s.nextInt();

                System.out.print("First name? ");

                first = s.next();

                System.out.print("Last name? ");

                last = s.next();

                    System.out.print("phone? ");

                phone = s.next();

                    System.out.print("dob? ");

                dob = s.next();

                System.out.print("Address? ");

                address = s.next();

                System.out.print("Email? ");

                email = s.next();

                library[selectedBook].edit(first, last, phone, dob, address, email, whichEntry);

                break;

            case 4:

                System.out.println("Sort alphabetically by which field?");

                System.out.println("1. Sort by first name");

                System.out.println("2. Sort by last name");

               System.out.println("3. Sort by phone");

                    System.out.println("4. Sort by dob");

                System.out.println("5. Sort by address");

                System.out.println("6. Sort by email");

                library[selectedBook].sort(s.nextInt());

                break;

            case 5:

                System.out.println("search which field?");

                System.out.println("1. Search by first name");

                System.out.println("2. Search t by last name");

               System.out.println("3. Search by phone");

                    System.out.println("4. Search by dob");

                System.out.println("5. Search by address");

                System.out.println("6. Search by email");

                library[selectedBook].search (s.nextInt());

                break;

            case 6:

                System.out.print("Move which entry? ");

                int entryNo = s.nextInt();

                Book.Entry entryToMove = library[selectedBook].contents[entryNo];

                library[selectedBook].remove(entryNo);

                System.out.print("To which book? ");

                int whichBook = s.nextInt();

                library[whichBook].addFromCopy(entryToMove);

                break;

            case 7:

                done = true;

                break;

            default:

                System.out.print("Please choose a valid menu number");

            }

        }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote