0

For a project, i have to create an address book and I am trying to get it to store the person data into an array list and read from a file text but have not been able to figure out how to do it. Here's the instructions:
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'm mostly stuck on 6 and 7. It pretty much stumped me.
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;
     }
     }
2
Contributors
1
Reply
20
Views
2 Years
Discussion Span
Last Post by mikias.kidane.9
0

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

6) First you ask the user using Scanner for the file name they want to save in. you can get the name using Scanner easily. but dont forget to set it as .txt or which ever you prefer. After that use filewriter ..

example )

public static void main(String[] args) throws IOException {

      String myFile = "text.txt";
       FileWriter file = new FileWriter (new File (myFile), true);
        file.write("hello world \n");

        file.close();
    }

7)Here is example

private static void scannerRead(File scanFile) throws IOException
    {
        try {

            Scanner scan = new Scanner (scanFile);
            scan.nextLine();

            while (scan.hasNextLine()){

                System.out.println(scan.nextLine());
            }
            scan.close();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.