0

Hi. I'm looking for help with spliting up my code into separate classes so it runs better.

It's a simple addressbook program that allows the user several actions:

  1. add contact

  2. search

  3. delete

  4. display all

  5. exit

I'm new to java, so I need all the help I can get!

My code:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class AddressBookOperations
{


    public static void main(String[] args) throws IOException
    {
        String s = null;
        String s2 = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        // Console in = System.console();

        while (s2 != "5"){
        System.out.println(" Welcome, Please enter an option: \n"
                             + "---------------------------------\n"
                        + " 1- Add contact\n 2- search contact\n 3- delete contact\n 4- display all contacts\n 5- exit\n");
        s2 = in.readLine();

        if (s2.equals("5"))
        {
            System.exit(0);
        }

        else
        {
            s = s2;
        }

        if (s != null)
        {
            String dataLine;
            String data;
            if (s.equals("1")) {
                System.out.println("Enter Name: ");
                dataLine = in.readLine();
                data = dataLine;
                in = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Enter LastName: ");
                dataLine = in.readLine();
                data = data + "--" + dataLine;
                System.out.println("Enter Address: ");
                dataLine = in.readLine();
                data = data + "--" + dataLine;
                System.out.println("Enter PhoneNumber: ");
                dataLine = in.readLine();
                data = data + "--" + dataLine;
                writeToFile("AddressBookData.dat", data, true, true);
            }
            else if (s.equals("2")) // Search contact
            {
                System.out.println("Enter Name 0r PhoneNumber: ");
                dataLine = in.readLine();
                String result = readFromFile("C:/AddressBookData.dat", dataLine);
                System.out.println("Search Results\n" + result);
            }
            else if (s.equals("3")) // Delete contact
            {
                System.out.println("Enter Name: ");
                dataLine = in.readLine();
                data = dataLine;
                System.out.println("PhoneNumber: ");
                dataLine = in.readLine();
                data = data + "--" + dataLine;
                deleteFromFile("AddressBookData.dat", data);
            }
            else if (s.equals("4")) // Display all contacts
            {
                String result = readFromFile("AddressBookData.dat", null);
                System.out.println("All contacts\n" + result);



                }
            }
        }
    }

    private static void deleteFromFile(String string, String dataLine) 
    {
        String data = readFromFile(string, null);
        data = data.replaceAll(dataLine, "");
        writeToFile(string, data, false, false);
    }

    public static boolean writeToFile(String fileName, String dataLine,
            boolean isAppendMode, boolean isNewLine) {
        if (isNewLine) {
            dataLine = "\n" + dataLine;
        }

        try {
            File outFile = new File(fileName);
            DataOutputStream dos;
            if (isAppendMode) {
                dos = new DataOutputStream(new FileOutputStream(fileName, true));
            } else {
                dos = new DataOutputStream(new FileOutputStream(outFile));
            }

            dos.writeBytes(dataLine);
            dos.close();
        } catch (FileNotFoundException ex) {
            return (false);
        } catch (IOException ex) {
            return (false);
        }
        return (true);

    }


     // Reads data from a given file

    public static String readFromFile(String fileName, String dataLine2)
    {
        String DataLine = "";
        String fileData = "";
        try
        {
            File inFile = new File(fileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile)));

            if (dataLine2 != null)
            {
                while ((DataLine = br.readLine()) != null)
                {
                    if (DataLine.contains(dataLine2))
                    {
                        fileData = DataLine;
                    }
                }
            }

            else
            {
                while ((DataLine = br.readLine()) != null)
                {
                    fileData = fileData + "\n" + DataLine;
                    //System.out.println(DataLine);
                }
            }
            br.close();
        }

        catch (FileNotFoundException ex)
        {
            return (null);
        } catch (IOException ex)
        {
            return (null);
        }
        return (fileData);

    }

    public static boolean isFileExists(String fileName)
    {
        File file = new File(fileName);
        return file.exists();
    }
}
Zoot
  • 2,217
  • 4
  • 29
  • 47
David Needham
  • 119
  • 1
  • 3
  • 9

2 Answers2

1

Make a Contact class to store the information for a contact. You should be able to override the Contact class' ToString() method and you can use that for printing out to a file. That should be a good start.

Another thing you could do is pull the file into a List, hold it in memory for the duration of the program, and only write it back out to the file when finished with it. Disk IO is going to be the slowest part of this program most likely so you want to avoid that as much as possible.

Joe Phillips
  • 49,743
  • 32
  • 103
  • 159
1

You asked practically the same question before and I ended up giving you all of the code for the entire thing.

That question is here: Address book that reads and write from a data file

From there, all that you have to do from there is put Contact into a file named Contact.java and make the class defintion read public class Contact {. Then do the same with ContactDriver.

On another note, you have now asked three questions that all relate to the same project, basically getting us to write the entire project for you. I don't really have a problem with writing the code, since it is good practice for me, but I do have a problem with you showing no effort and repeatedly asking questions to get us to write the entire Application for you.

Community
  • 1
  • 1
Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
  • The code above is my own effort but I am struggling with java and I apoligize for all the questions.. – David Needham Dec 12 '11 at 02:29
  • 2
    I understand that you have your code, but each question has an answer that requires you to do nothing but copy paste. All of this is seriously starting to look like a homework assignment that you don't feel like doing yourself. – Jon Egeland Dec 12 '11 at 02:33
  • If he's on his 3rd iteration of the code, that is not a bad thing. The question is different even though it is related – Joe Phillips Dec 12 '11 at 02:37
  • @JoePhilllips Take a look at the other three questions. They end up being answered with complete code. (Except for one). – Jon Egeland Dec 12 '11 at 02:53
  • And here he has completely ignored those answers, and is asking basically for the answer to all of them put together. – Jon Egeland Dec 12 '11 at 02:54