Important alert: (current site time 7/15/2013 3:03:33 PM EDT)
 

VB icon

Address Book v 1.0

Email
Submitted on: 3/27/2005 5:33:17 AM
By: lklklklklk lk 
Level: Intermediate
User Rating: By 2 Users
Compatibility: Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 15906
(About the author)
 
     Objective: 1. Java's Builtin Linked List Used 2. GUI Interface 3. File Input Output.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Address Book v 1.0
// Description:Objective: 
1. Java's Builtin Linked List Used
2. GUI Interface
3. File Input Output.
// By: lklklklklk lk
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4722&lngWId=2//for details.//**************************************

/*
Address Book (Name, Phone, Email)
Version: 1.00
Objective: BuiltIn LinkedList Used
Programmer : Md. Mahmud Ahsan 
(My first Project in Java)
Matric no: c041101
*/
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
// user defined type where we put name, email, mobile
class Address{
	private String name;
	private String mobile;
	private String email;
	
	public Address(String _name, String _mobile, String _email){
		name = _name;
		mobile = _mobile;
		email = _email;
	}
	
	public String toString(){
		return name + "\n" + mobile + "\n"+ email+"\n";
	}
	
}
public class AddressBook extends JFrame{
	private LinkedList lList; // built in linked list of java.util
	
	private JLabel lName, lMobile, lEmail, lIndex;
	private JTextField fName, fMobile, fEmail, fIndex;
	private JTextField fRemove;
	private JTextArea outputArea;
	
	private JButton bAdd, bRemove, bExit, bAbout;
	
	private FileOutputStream fout; // used for file output
	private FileInputStream fin; // used for file input
	
	public AddressBook() throws IOException{
		// title
		super("Address Book (Name, Mobile, Email)"); 
		
		lList = new LinkedList();
		
		try{
			// if A file already exist and have some data read it
			fin = new FileInputStream("Data.dat");
			BufferedReader br = new BufferedReader(new InputStreamReader(fin));
			
			String sName = br.readLine();
			String sMobile = br.readLine();
			String sEmail = br.readLine();
			
			while (sName != null){
				// Read from file and store them in linked List's object
				lList.add(new Address(sName, sMobile, sEmail));
				
				sName = br.readLine();
				sMobile = br.readLine();
				sEmail = br.readLine();
			}
			fin.close();
		}
		catch(Exception err){
			// if no file create a new file
			fout = new FileOutputStream("Data.dat");
		}	
		
		// set graphical user interface
		Container container = getContentPane();
		container.setLayout(new FlowLayout());
		
		lName = new JLabel("Name");
		container.add(lName);
		fName = new JTextField(20);
		container.add(fName);
		
		lMobile = new JLabel("Mobile");
		container.add(lMobile);
		fMobile = new JTextField(10);
		container.add(fMobile);
		
		lEmail = new JLabel("Email");
		container.add(lEmail);
		fEmail = new JTextField(15);
		container.add(fEmail);
		
		bAdd = new JButton("Add");
		container.add(bAdd);
		bAdd.addActionListener( // add event
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					StringBuffer str = new StringBuffer();
					str.append(fName.getText());
					
					// if name field is empty
					if (str.length() == 0){						
						JOptionPane.showMessageDialog(null, "Name Field must be initialized", "Info", JOptionPane.INFORMATION_MESSAGE);
						return; // if name field empty
					}
					
					// add new information from user to linkedlist
					lList.add(new Address(fName.getText(), fMobile.getText(), fEmail.getText()));
					fName.setText("");
					fMobile.setText("");
					fEmail.setText("");
					update(); // show final result to outputArea
					diskFileUpdate(); // store information to disk
						
				}
			}
		);	
		
		lIndex = new JLabel("Enter Index to remove Item from Linked List");
		container.add(lIndex);
		fIndex = new JTextField(5);
		container.add(fIndex);
		
		bRemove = new JButton("Remove Item");
		container.add(bRemove);
		bRemove.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					try{
						// remove item from linked list
						if(lList.size() != 0){
							lList.remove(Integer.parseInt(fIndex.getText()));
						}
						update(); // show final result in outputArea
						diskFileUpdate(); // store file in disk
					}
					catch(Exception err){
						JOptionPane.showMessageDialog(null, "Error" + err, "Error", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		);
				
		// show the data to outputArea
		outputArea = new JTextArea(20, 40);
		outputArea.setEditable(false);
		container.add(new JScrollPane(outputArea));
		update();
		
		bAbout = new JButton("About Me");
		container.add(bAbout);
		bAbout.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					String about = "Address Book\nVersion 1.00\nDeveloper: Md. Mahmud Ahsan\n Student of IIUC DC\n Matric no: C041101\n";
					JOptionPane.showMessageDialog(null, about, "About Me", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		);
		setSize(700, 500);
		setVisible(true);
	}
	private void update(){
		// I used Iterator to access linked list's object
		Iterator it = lList.iterator();
		String out=""; // it used for collecting all members
		int count = 0;
		while (it.hasNext()){
			Object element = it.next();
			out += "\nPERSON " + count + "\n";
			out += "=============\n";
			out += element.toString();
			++count;
		}		
		outputArea.setText(out); 
	}
	
	private void diskFileUpdate(){
		try{
			// store to disk file
			fout = new FileOutputStream("Data.dat");
			// StringBuffer used to access every character
			StringBuffer out = new StringBuffer("");
			char c;
			Iterator it = lList.iterator();
			while (it.hasNext()){
				Object element = it.next();
				out.append(element.toString());
			}
			// write file every character by character
			for (int i = 0; i < out.length(); ++i){
				c = out.charAt(i);
				fout.write(c);
			}
			fout.close();
		}
		catch(Exception err){
			JOptionPane.showMessageDialog(null, "Error Caught: " + err, "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
		
	public static void main(String args[]){
		try{
			AddressBook window = new AddressBook();
			window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null, "Error" + e, "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
}		
				


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.