isms Publish time 13-10-2010 02:20 PM

JAVA PROGRAMMING : PhoneBook

Salam semua,

mini projek nih tok sy punya kelas assignment. Lecture soh buat phonebook guna java dan fail input dia simpan dalam text file.
function dia pun setakat, add nama n no telefon, delete, edit dan view balik data yg dah simpan dalam textfile tuh td.

key-in data tuh pula melalui console (import java.util.Scanner).

sy ada try2 cari kat internet cth kebanyakkannya tak guna scanner + textfile.

anyone, tolong saya.

alusmetai Publish time 15-10-2010 03:28 AM

Post Last Edit by alusmetai at 15-10-2010 07:37

1. Tulis class Phonebook untuk represent the record.

2. Tulis class Scanner. refer kat website ini-> Java Scanner

3. Tulis class TextReader. Code aku amik dari internet je.

public class TextReader {
   private static void readFile(String fileName) {
       try {
         File file = new File(fileName);
         FileReader reader = new FileReader(file);
         BufferedReader in = new BufferedReader(reader);
         String string;
         while ((string = in.readLine()) != null) {
         System.out.println(string);
         }
         in.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
   }

   public static void main(String[] args) {
       if (args.length != 1) {
         System.err.println("usage: java TextReader "
         + "file location");
         System.exit(0);
       }
       readFile(args);
   }
   }

4. Tambah method appendToFile() untuk tambah/edit record. Oleh sebab code readFile() di atas tu amik data dari 1 file sebaris demi sebaris, maka ko kenalah simpan setiap record/tuple dlm phonebook tu sebaris demi sebaris. Selepas tu ko kena parse kan lagi sekali tiap2 baris record tu utk dipecahkan kepada columns.

5. Untuk delete a particular record dlm file phonebook, sila rujuk code di bawah:
public void removeLineFromFile(String file, String lineToRemove) {

    try {

      File inFile = new File(file);
      
      if (!inFile.isFile()) {
      System.out.println("Parameter is not an existing file");
      return;
      }
      
      //Construct the new file that will later be renamed to the original filename.
      File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
      
      BufferedReader br = new BufferedReader(new FileReader(file));
      PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
      
      String line = null;

      //Read from the original file and write to the new
      //unless content matches data to be removed.
      while ((line = br.readLine()) != null) {
      
      if (!line.trim().equals(lineToRemove)) {

          pw.println(line);
          pw.flush();
      }
      }
      pw.close();
      br.close();
      
      //Delete the original file
      if (!inFile.delete()) {
      System.out.println("Could not delete file");
      return;
      }
      
      //Rename the new file to the filename the original file had.
      if (!tempFile.renameTo(inFile))
      System.out.println("Could not rename file");
      
    }
    catch (FileNotFoundException ex) {
      ex.printStackTrace();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
}

Modify sikit method tu utk delete record berdasarkan input user contohnya user input Name dan bukan berdasarkan satu record. Of course di sini kita tak sentuh pasal database design issues spt user with same names apa nak buat etc etc.

notes: Kalau ko suka, ko boleh buat untuk tiap2 satu record ada satu file. Ni lagi senang. Jadi terpulanglahkepada requirement atau pantang larang assignment ko atau selera ko sendiri utk pilih yg mana satu.
Pages: [1]
View full version: JAVA PROGRAMMING : PhoneBook


ADVERTISEMENT