Jump to content

For a project, i have to create an address book and i need some help with is. I got most of the code down, but the last part of it confuses me. I have to turn this program into a file that someone can actually use and i don't know what to do. Here's the instructions:

    // Your program must provide the following features either all in the main
    // method or, preferably as a collection of static methods in this class.
    // Each of the methods can recieve the ProgramState object as a way of 
    // passing all the variables around
    
    // Present the user a menu of different options for manipulating their 
    // address book:
    
    // 1) Add a Person to the address book (remember to reset the view)
    //    You will need to prompt the user for each property and the set that
    //    value on the person, then add to the address book
    // 2) Display how many people are in the address book and current view
    //    (e.g. "Showing 5 of 37 entries")
    // 3) Print the current address book view
    //    This should nicely print all entries in the current view to System.out
    // 4) Allow the user to filter the current address book view
    //    using any of the filter methods defined above (you'll need to ask them
    //    which they want to do and get input/handle accordingly)
    // 5) Remove any filters on the address book
    //    Just return the currentView to the full address book
    // 6) Save the address book to a file (i.e. Save As)
    //    Optionally: "Save current view" and/or "Save all addresses"
    //    You'll need to prompt the user for a file name the write the contents
    //    to the file in a way that you can read back in with operation (7)
    //    below.
    // 7) Load the address book from a file (i.e. Open)
    //    Consider how you wrote the contents of the address bok out in (6).
    //    Open this file and "scan" through it creating new Person objects, each
    //    of which you add the to the address book
 
I just need some clarification for each part and a little example.
 
Here's my code so far:
 class Person{     private int age;     private String firstname;     private String lastname;     private String middlename;         public Person(){     age = 0;     firstname = "";     lastname ="";     middlename ="";          }     public Person(String first, String middle, String last){     this.age=age;     this.firstname= first;     this.middlename=middle;     this.lastname=last;         }          public String getfirstname(){     return firstname;         }     public String getmiddlename(){     return middlename;         }     public String getlastname(){     return lastname;         }          public void setfirstname(String F){     firstname = F;         }     public void setmiddlename(String I){     middlename = I;         }     public void setlastname(String L){     lastname = L;       }     public void setage(int age){     this.age = age;     }           public String getfullname(){     return firstname+" "+middlename+" "+lastname;         }        public int getage(){     return this.age;         }              }     class Addressbook{     private Person[] entries;     private int numPeople;     public Addressbook(){     numPeople = 0;     entries = new Person [150];     }     public Addressbook(Person[]people){     numPeople = people.length;     entries = people;     }     public int getsize(){     return numPeople;     }     public Person getPerson(int position){     return entries[position];         }     public Person getentry(int index){     return entries [index];         }     public int addentry(Person added){     entries[numPeople] = added;     numPeople++;     return numPeople;     }     public Addressbook filterbyage(int lower, int upper){     Addressbook found = new Addressbook();         for(int i=0; i<numPeople;i++){     Person entry;     entry = this.getentry(i);     if(entry.getage()>= lower && entry.getage()<= upper){     found.addentry(entry);       }     return found;     }     }     public Addressbook filterbyfirstname(String firstname){     Addressbook found = new Addressbook();      for(int i=0; i<numPeople;i++){     Person entry;     entry = this.getentry(i);     if(entry.getfirstname().equals(firstname)){     found.addentry(entry);     }     return found;     }     }     public Addressbook filterbymiddlename(String middlename){     Addressbook found = new Addressbook();      for(int i=0; i<numPeople;i++){     Person entry;     entry = this.getentry(i);     if(entry.getfirstname().equals(middlename)){     found.addentry(entry);     }     return found;     }     }     public Addressbook filterbylastname(String lastname){     Addressbook found = new Addressbook();      for(int i=0; i<numPeople;i++){     Person entry;     entry = this.getentry(i);     if(entry.getlastname().equals(lastname)){     found.addentry(entry);     }     return found;     }     }
     
Thanks. 
Link to comment
https://linustechtips.com/topic/278162-need-help-with-address-book/
Share on other sites

Link to post
Share on other sites

6: http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html

for entry in address book{

wirte a line for each entry to the file

 

7: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

while scanner.hasNextLine(){

create a new address book entry for each line in the file

Personal Build Project "Rained-On"

helped building up the CPU Overclocking Database and GPU Overclocking Database, check them out ;)

#KilledMyWife #MakeBombs #LinusIsNotFunny || Please, dont use non-default grey font colors. Think about the night-theme users! ;)

Link to post
Share on other sites

Use proper indentation too please.

// this is much easier to read    public Addressbook filterbyfirstname(String firstname){        Addressbook found = new Addressbook();         for(int i=0; i<numPeople;i++){            Person entry;            entry = this.getentry(i);            if(entry.getfirstname().equals(firstname)){                found.addentry(entry);            }            return found;        }    }// than this     public Addressbook filterbyfirstname(String firstname){     Addressbook found = new Addressbook();      for(int i=0; i<numPeople;i++){     Person entry;     entry = this.getentry(i);     if(entry.getfirstname().equals(firstname)){     found.addentry(entry);     }     return found;     }     }

I'm looking through things for you now. Just so you're aware, all your filter methods in AddressBook are missing return statements. Currently your code doesn't compile. My guess is you want return found; outside of the for loop.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×