Created
July 29, 2013 11:46
-
-
Save poi519/6103796 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.Comparator; | |
class MalformedContactStringException extends Exception { | |
public MalformedContactStringException(String str) { | |
super(str); | |
} | |
} | |
class Contact { | |
public String firstName; | |
public String lastName; | |
public String phoneNumber; | |
public Contact(String f, String l, String n) { | |
firstName = f; | |
lastName = l; | |
phoneNumber = n; | |
} | |
public static Comparator<Contact> firstNameComparator = new Comparator<Contact>() { | |
@Override | |
public int compare(Contact contact, Contact contact2) { | |
return contact.firstName.compareTo(contact2.firstName); | |
} | |
}; | |
public static Comparator<Contact> lastNameComparator = new Comparator<Contact>() { | |
@Override | |
public int compare(Contact contact, Contact contact2) { | |
return contact.lastName.compareTo(contact2.lastName); | |
} | |
}; | |
public static Comparator<Contact> phoneNumberComparator = new Comparator<Contact>() { | |
@Override | |
public int compare(Contact contact, Contact contact2) { | |
return contact.phoneNumber.compareTo(contact2.phoneNumber); | |
} | |
}; | |
} | |
class PhoneBook { | |
public ArrayList<Contact> contacts; | |
public PhoneBook() { | |
contacts = new ArrayList<>(); | |
} | |
public PhoneBook(ArrayList<Contact> l) { | |
contacts = l; | |
} | |
static PhoneBook loadFile(String path) { | |
ArrayList<Contact> contactsList = new ArrayList<>(); | |
try(BufferedReader br = new BufferedReader(new FileReader(path))) { | |
String f, l, n; | |
while((f = br.readLine()) != null && | |
(l = br.readLine()) != null && | |
(n = br.readLine()) != null) { | |
contactsList.add(new Contact(f, l, n)); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return new PhoneBook(contactsList); | |
} | |
void writeToFile(String path) { | |
try(BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( | |
new FileOutputStream(path), "utf-8"))) { | |
for(Contact c : contacts) { | |
writer.write(c.firstName); | |
writer.write(c.lastName); | |
writer.write(c.phoneNumber); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import javax.swing.table.AbstractTableModel; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
public class PhoneBookForm { | |
private JPanel panel1; | |
private JTable table1; | |
private JButton buttonAdd; | |
private JButton buttonDelete; | |
public PhoneBookForm() { | |
buttonAdd.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
PhoneBookTableModel tm = ((PhoneBookTableModel) table1.getModel()); | |
tm.phoneBook.contacts.add(new Contact("", "", "")); | |
tm.fireTableDataChanged(); | |
} | |
}); | |
buttonDelete.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
PhoneBookTableModel tm = ((PhoneBookTableModel) table1.getModel()); | |
tm.phoneBook.contacts.remove(table1.getSelectedRow()); | |
tm.fireTableDataChanged(); | |
} | |
}); | |
} | |
public static void main(String[] args) { | |
JFrame frame = new JFrame("PhoneBookForm"); | |
frame.setContentPane(new PhoneBookForm().panel1); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
private void createUIComponents() { | |
table1 = new JTable(new PhoneBookTableModel()); | |
} | |
} | |
class PhoneBookTableModel extends AbstractTableModel { | |
static private String[] headers = {"First", "Last", "Phone"}; | |
public PhoneBook phoneBook; | |
public PhoneBookTableModel() { | |
phoneBook = new PhoneBook(); | |
} | |
public Class getColumnClass(int column) { | |
return String.class; | |
} | |
public void setValueAt(Object value, int row, int column) { | |
Contact contact = phoneBook.contacts.get(row); | |
String s = (String) value; | |
switch(column) { | |
case 0: contact.firstName = s; break; | |
case 1: contact.lastName = s; break; | |
case 2: contact.phoneNumber = s; break; | |
} | |
} | |
public String getValueAt(int row, int column) { | |
Contact contact = phoneBook.contacts.get(row); | |
switch(column) { | |
case 0: return contact.firstName; | |
case 1: return contact.lastName; | |
case 2: return contact.phoneNumber; | |
default: return "Empty"; | |
} | |
} | |
public int getRowCount() { | |
return phoneBook.contacts.size(); | |
} | |
public int getColumnCount() {return 3;} | |
public String getColumnName(int column) { | |
return headers[column]; | |
} | |
public boolean isCellEditable(int i, int j) {return true;} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment