Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: Design and implement an address book
|
Posted: Jul 2, 2002 12:23 PM
|
|
Kymberly, the following is a functional address book application which has the features you asked for.
You can download the source at:
http://www.quantumhyperspace.com/SourceBank/AddressBook.java
Best of wishes with your programming.
Charles
This application read text files which look like: ID=0001 Name=Bell, Charles HomeAddress=123 MyStreet, Vickburg, MS 39180 HomePhoneNumber=111-222-1234 BusinessAddress=123 MyStreet, Vickburg, MS 39180 BusinessPhoneNumber=601-122-1234 FaxMachineNumber=601-436-1234 CellularPh oneNumber=601-343-3456 PagerNumber=601-223-3344
/* AddressBook.java * @author Charles Bell * @version July 1, 2002 */
import java.awt.*; import java.io.*; import java.util.*; import javax.swing.*;
public class AddressBook{ final String addressBookFileExtension = ".abf"; private Vector entries = new Vector(); private boolean debug = false; public AddressBook(){ init(); } public static void main(String[] args){ new AddressBook(); } public void init(){ entries = getAddressBookEntries(); ActionSelector selector = new ActionSelector(); boolean done = false; String selection = ""; while (!done){ selection = selector.getActionSelection(selection); if (selection.compareTo("Add Address Book Entry") == 0){ entries.add(addAddressBookEntry()); }else if (selection.compareTo("Edit Address Book Entry") == 0){ editAddressBookEntry(); }else if (selection.compareTo("View Address Book Entries") == 0){ viewAddressBookEntries(); } done = (selection.compareTo("Exit") == 0); } System.exit(0); } public Vector getAddressBookEntries(){ Vector v = new Vector(); File current = new File("."); File[] addressBookFiles = current.listFiles(new AddressBookFileFilter()); AddressBookReader reader = new AddressBookReader(); for (int i = 0; i < addressBookFiles.length; i++){ AddressBookEntry nextEntry = reader.read(addressBookFiles[i]); v.add(nextEntry); } if (debug) System.out.println("found " + v.size() + " Address Book entries."); return v; } public AddressBookEntry addAddressBookEntry(){ if (debug) System.out.println("addAddressBookEntry()"); AddressBookEntry newEntry = new AddressBookEntry(); String id = getNextID(); while (id.length() < 4){ id = "0" + id; } newEntry.setID(id); AddressBookEditor editor = new AddressBookEditor(); newEntry = editor.edit(newEntry); AddressBookWriter writer = new AddressBookWriter(); writer.write(newEntry); return newEntry; } public void editAddressBookEntry(){ if (debug) System.out.println("editAddressBookEntry()"); AddressBookEntrySelector selector = new AddressBookEntrySelector(); AddressBookEntry entry = selector.getSelection(); if (debug) System.out.println("Selected: " + entry.getName()); if (entry != null) { AddressBookEditor editor = new AddressBookEditor(); entry = editor.edit(entry); AddressBookWriter writer = new AddressBookWriter(); writer.write(entry); } } public void viewAddressBookEntries(){ if (debug) System.out.println("viewAddressBookEntries()"); AddressBookEntrySelector selector = new AddressBookEntrySelector(); AddressBookEntry entry = new AddressBookEntry(); while (entry != null){ entry = selector.getSelection(); if (entry != null){ if (debug) System.out.println("Selected: " + entry.getName()); AddressBookViewer viewer = new AddressBookViewer(); viewer.view(entry); } } } public String getNextID(){ File current = new File("."); String[] addressBookFileNames = current.list(new AddressBookFileFilter()); int last = 0; for (int i = 0; i < addressBookFileNames.length; i++){ String nextName = addressBookFileNames[i]; String numberPart = nextName.substring(0,nextName.indexOf(addressBookFileExtension)); int number = toInt(numberPart); if (number > 0) last = Math.max(last,number); } last++; if (debug) System.out.println(String.valueOf(last)); return String.valueOf(last); } private int toInt(String s){ int n = -1; try{ n = Integer.parseInt(s); }catch(NumberFormatException nfe){ System.err.println("NumberFormatException: " + nfe.getMessage()); n = -1; } return n; }
class AddressBookReader{ public AddressBookEntry read(File f){ AddressBookEntry entry = new AddressBookEntry(); try{ Properties properties = new Properties(); properties.load(new FileInputStream(f)); entry.setID(properties.getProperty("ID")); entry.setName(properties.getProperty("Name")); entry.setHomeAddress(properties.getProperty("HomeAddress")); entry.setHomePhoneNumber(properties.getProperty("HomePhoneNumber")); entry.setBusinessAddress(properties.getProperty("BusinessAddress")); entry.setBusinessPhoneNumber(properties.getProperty("BusinessPhoneNumber")); entry.setFaxMachineNumber(properties.getProperty("FaxMachineNumber")); entry.setCellularPhoneNumber(properties.getProperty("CellularPhoneNumber")); entry.setPagerNumber(properties.getProperty("PagerNumber")); if (debug) System.out.println(entry.toString()); }catch(FileNotFoundException fnfe){ System.err.println("FileNotFoundException: " + fnfe.getMessage()); }catch(IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); } return entry; } }
class AddressBookWriter{ public void write(AddressBookEntry entry){ try{ FileWriter fw = new FileWriter(entry.getID() + addressBookFileExtension); fw.write("ID=" + entry.getID() + "\n"); fw.write("Name=" + entry.getName() + "\n"); fw.write("HomeAddress=" + entry.getHomeAddress() + "\n"); fw.write("HomePhoneNumber=" + entry.getHomePhoneNumber() + "\n"); fw.write("BusinessAddress=" + entry.getBusinessAddress() + "\n"); fw.write("BusinessPhoneNumber=" + entry.getBusinessPhoneNumber() + "\n"); fw.write("FaxMachineNumber=" + entry.getFaxMachineNumber() + "\n"); fw.write("CellularPhoneNumber=" + entry.getCellularPhoneNumber() + "\n"); fw.write("PagerNumber=" + entry.getPagerNumber()); fw.close(); }catch(FileNotFoundException fnfe){ System.err.println("FileNotFoundException: " + fnfe.getMessage()); }catch(IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); } } } class AddressBookEntry{ private String id; private String name; private String homeAddress; private String homePhoneNumber; private String businessAddress; private String businessPhoneNumber; private String faxMachineNumber; private String cellularPhoneNumber; private String pagerNumber; public AddressBookEntry(){ this.id = ""; this.name = ""; this.homeAddress = ""; this.homePhoneNumber = ""; this.businessAddress = ""; this.businessPhoneNumber = ""; this.faxMachineNumber = ""; this.cellularPhoneNumber = ""; this.pagerNumber = ""; } public AddressBookEntry(String id, String name, String homeAddress, String homePhoneNumber, String businessAddress, String businessPhoneNumber, String faxMachineNumber, String cellularPhoneNumber, String pagerNumber){ this.id = id; this.name = name; this.homeAddress = homeAddress; this.homePhoneNumber = homePhoneNumber; this.businessAddress = businessAddress; this.businessPhoneNumber = businessPhoneNumber; this.faxMachineNumber = faxMachineNumber; this.cellularPhoneNumber = cellularPhoneNumber; this.pagerNumber = pagerNumber; } public String getID(){ return id; } public String getName(){ return name; } public String getHomeAddress(){ return homeAddress; } public String getHomePhoneNumber(){ return homePhoneNumber; } public String getBusinessAddress(){ return businessAddress; } public String getBusinessPhoneNumber(){ return businessPhoneNumber; } public String getFaxMachineNumber(){ return faxMachineNumber; } public String getCellularPhoneNumber(){ return cellularPhoneNumber; } public String getPagerNumber(){ return pagerNumber; } public void setID(String s){ id = s; } public void setName(String s){ name = s; } public void setHomeAddress(String s){ homeAddress = s; } public void setHomePhoneNumber(String s){ homePhoneNumber = s; } public void setBusinessAddress(String s){ businessAddress = s; } public void setBusinessPhoneNumber(String s){ businessPhoneNumber = s; } public void setFaxMachineNumber(String s){ faxMachineNumber = s; } public void setCellularPhoneNumber(String s){ cellularPhoneNumber = s; } public void setPagerNumber(String s){ pagerNumber = s; } public String toString(){ return "ID: " + getID() + "\n" + "Name: " + getName() + "\n" + "Home Address: " + getHomeAddress() + "\n" + "Home Phone Number: " + getHomePhoneNumber() + "\n" + "Business Address: " + getBusinessAddress() + "\n" + "Business Phone Number: " + getBusinessPhoneNumber() + "\n" + "Fax Machine Number: " + getFaxMachineNumber() + "\n" + "Cellular Phone Number:" + getCellularPhoneNumber() + "\n" + "Pager Number: " + getPagerNumber(); } } class ActionSelector{ private String[] actions = { "Add Address Book Entry", "Edit Address Book Entry", "View Address Book Entries", "Exit"};
/** Pops up a user interface of JRadio buttons to select an action * from a pallette of allowable Action Names. */ public String getActionSelection(String current){ String choice = current; String message = "Select Action:"; String[] options = getOptions(); JFrame f = new JFrame(); JPanel buttonPanel = new JPanel(); if (options.length/2 == 0){ buttonPanel.setLayout(new GridLayout(options.length/2,2)); }else{ buttonPanel.setLayout(new GridLayout(options.length/2+1,2)); } ButtonGroup buttongroup = new ButtonGroup(); JRadioButton[] buttons = new JRadioButton[options.length]; for (int i = 0;i<options.length;i++){ buttons[i] = new JRadioButton(options[i],(current.compareTo(options[i]) == 0)); buttonPanel.add(buttons[i]); buttongroup.add(buttons[i]); } JScrollPane scrollpane = new JScrollPane(buttonPanel); Object[] array = {message,scrollpane}; scrollpane.setFont(new Font("SansSerif",Font.PLAIN,8)); JOptionPane editpane = new JOptionPane(scrollpane,JOptionPane.PLAIN_MESSAGE,JOptionPane.OK_CANCEL_OPTION); editpane.setFont(new Font("SansSerif",Font.PLAIN,8)); JDialog dialog = editpane.createDialog(f,message); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); f.pack(); f.setLocationRelativeTo(null); dialog.show(); for (int i = 0;i<options.length;i++){ if (buttons[i].isSelected()) { choice = buttons[i].getText(); } } dialog.dispose(); f.dispose(); return choice; } private String[] getOptions(){ return actions; } } class AddressBookFileFilter implements FilenameFilter{ public boolean accept(File dir, String name){ return (name.indexOf(addressBookFileExtension) > 0); } } class AddressBookEditor {
/** Pops up a user interface to edit an AddressBookEntry. */ public AddressBookEntry edit(AddressBookEntry entry){ String message = "Edit Address Book Entry"; JFrame f = new JFrame(); JPanel editorPanel = new JPanel(); editorPanel.add(new JLabel("Name:")); JTextField nameField = new JTextField(entry.getName()); editorPanel.add(nameField); editorPanel.add(new JLabel("Home Address:")); JTextField homeAddressField = new JTextField(entry.getHomeAddress()); editorPanel.add(homeAddressField);
editorPanel.add(new JLabel("Home Phone Number:")); JTextField homePhoneNumberField = new JTextField(entry.getHomePhoneNumber()); editorPanel.add(homePhoneNumberField); editorPanel.add(new JLabel("Business Address:")); JTextField businessAddressField = new JTextField(entry.getBusinessAddress()); editorPanel.add(businessAddressField); editorPanel.add(new JLabel("Business Phone Number:")); JTextField businessPhoneNumberField = new JTextField(entry.getBusinessPhoneNumber()); editorPanel.add(businessPhoneNumberField); editorPanel.add(new JLabel("Fax Machine Number:")); JTextField faxMachineNumberField = new JTextField(entry.getFaxMachineNumber()); editorPanel.add(faxMachineNumberField); editorPanel.add(new JLabel("Cellular Phone Number:")); JTextField cellularPhoneNumberField = new JTextField(entry.getCellularPhoneNumber()); editorPanel.add(cellularPhoneNumberField); editorPanel.add(new JLabel("Pager Number:")); JTextField pagerNumberField = new JTextField(entry.getPagerNumber()); editorPanel.add(pagerNumberField);
editorPanel.setLayout(new GridLayout(8,2)); Object[] array = {message,editorPanel}; JOptionPane editpane = new JOptionPane(editorPanel,JOptionPane.PLAIN_MESSAGE,JOptionPane.OK_CANCEL_OPTION) ; editpane.setFont(new Font("SansSerif",Font.PLAIN,8)); JDialog dialog = editpane.createDialog(f,message); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); f.pack(); f.setLocationRelativeTo(null); dialog.show(); entry.setName(nameField.getText()); entry.setHomeAddress(homeAddressField.getText()); entry.setHomePhoneNumber(homePhoneNumberField.getText()); entry.setBusinessAddress(businessAddressField.getText()); entry.setBusinessPhoneNumber(faxMachineNumberField.getText()); entry.setFaxMachineNumber(faxMachineNumberField.getText()); entry.setCellularPhoneNumber(cellularPhoneNumberField.getText()); entry.setPagerNumber(pagerNumberField.getText()); dialog.dispose(); f.dispose(); return entry; } }
class AddressBookViewer {
/** Pops up a user interface to view an AddressBookEntry. */ public void view(AddressBookEntry entry){ String message = "Edit Address Book Entry"; JFrame f = new JFrame(); JPanel editorPanel = new JPanel(); editorPanel.add(new JLabel("Name:")); JTextField nameField = new JTextField(entry.getName()); editorPanel.add(nameField); editorPanel.add(new JLabel("Home Address:")); JTextField homeAddressField = new JTextField(entry.getHomeAddress()); editorPanel.add(homeAddressField);
editorPanel.add(new JLabel("Home Phone Number:")); JTextField homePhoneNumberField = new JTextField(entry.getHomePhoneNumber()); editorPanel.add(homePhoneNumberField); editorPanel.add(new JLabel("Business Address:")); JTextField businessAddressField = new JTextField(entry.getBusinessAddress()); editorPanel.add(businessAddressField); editorPanel.add(new JLabel("Business Phone Number:")); JTextField businessPhoneNumberField = new JTextField(entry.getBusinessPhoneNumber()); editorPanel.add(businessPhoneNumberField); editorPanel.add(new JLabel("Fax Machine Number:")); JTextField faxMachineNumberField = new JTextField(entry.getFaxMachineNumber()); editorPanel.add(faxMachineNumberField); editorPanel.add(new JLabel("Cellular Phone Number:")); JTextField cellularPhoneNumberField = new JTextField(entry.getCellularPhoneNumber()); editorPanel.add(cellularPhoneNumberField); editorPanel.add(new JLabel("Pager Number:")); JTextField pagerNumberField = new JTextField(entry.getPagerNumber()); editorPanel.add(pagerNumberField);
editorPanel.setLayout(new GridLayout(8,2)); Object[] array = {message,editorPanel}; JOptionPane editpane = new JOptionPane(editorPanel,JOptionPane.PLAIN_MESSAGE,JOptionPane.OK_CANCEL_OPTION) ; editpane.setFont(new Font("SansSerif",Font.PLAIN,8)); JDialog dialog = editpane.createDialog(f,message); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); f.pack(); f.setLocationRelativeTo(null); dialog.show(); dialog.dispose(); f.dispose(); } }
class AddressBookEntrySelector{ /** Pops up a user interface of JRadio buttons to select an AddressBookEntry. * from a pallette of all AddressBookEntries. */ public AddressBookEntry getSelection(){ String message = "Select an Address Book Entry"; JFrame f = new JFrame(); JPanel buttonPanel = new JPanel(); if (entries.size() == 0){ buttonPanel.setLayout(new GridLayout(entries.size()/2,2)); }else{ buttonPanel.setLayout(new GridLayout(entries.size()/2+1,2)); } ButtonGroup buttongroup = new ButtonGroup(); JRadioButton[] buttons = new JRadioButton[entries.size()]; for (int i = 0;i<entries.size();i++){ AddressBookEntry nextEntry = (AddressBookEntry) entries.elementAt(i); if (debug) System.out.println(nextEntry.toString()); buttons[i] = new JRadioButton(nextEntry.getName()); buttonPanel.add(buttons[i]); buttongroup.add(buttons[i]); } JScrollPane scrollpane = new JScrollPane(buttonPanel); Object[] array = {message,scrollpane}; scrollpane.setFont(new Font("SansSerif",Font.PLAIN,8)); JOptionPane editpane = new JOptionPane(scrollpane,JOptionPane.PLAIN_MESSAGE,JOptionPane.OK_CANCEL_OPTION); editpane.setFont(new Font("SansSerif",Font.PLAIN,8)); JDialog dialog = editpane.createDialog(f,message); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); f.pack(); f.setLocationRelativeTo(null); dialog.show(); AddressBookEntry choice = null; for (int i = 0;i<entries.size();i++){ if (buttons[i].isSelected()) { choice = (AddressBookEntry) entries.elementAt(i); } } dialog.dispose(); f.dispose(); return choice; } } }
|
|