Hello, I have a java code that was created for a Contact Form and was wondering
ID: 3830236 • Letter: H
Question
Hello, I have a java code that was created for a Contact Form and was wondering how would I implement it to a Controller for a GUI? The java code has been created for a class so I need help in creating a Controller for the Contact Form Class (java). Thanks in advance!
package contactlist;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ContactList {
List<Contact> contactlist = new ArrayList<>();
private int top = 0;
public static void main(String[] args) throws IOException {
contact = new Contact();
List.Contact c;
c = contact;
ContactList list = new ContactList();
BufferedReader keyIn;
keyIn = new BufferedReader(new InputStreamReader(System.in));
String choose = "";
while (true) {
System.out.println(" [1] Add contact");
System.out.println("[2] View all contacts");
System.out.println("[3] Quit");
System.out.print("Choose : ");
try {
choose = keyIn.readLine();
} catch (IOException e) {
System.out.println("Error");
}
switch (choose) {
case "1":
list.addContact();
break;
case "2":
list.viewContacts();
break;
case "3":
System.exit(0);
break;
default:
System.out.println("Error");
break;
}
}
}
public ContactList() {
this.contactlist = new ArrayList<>();
}
public void addContact() throws IOException {
BufferedReader keyIn;
keyIn = new BufferedReader(new InputStreamReader(System.in));
String firstName;
String lastName;
String address;
String email;
String phone;
String jobTitle;
String organization;
String dateOfBirth;
Scanner input = new Scanner(System.in);
System.out.println("Please enter Specify the contact type (1) Personal
or (2) Business: ");
int contactType = input.nextInt();
if (contactType == 1) {
System.out.print("First Name: ");
firstName = keyIn.readLine();
System.out.print("Last Name: ");
lastName = keyIn.readLine();
System.out.print("Address: ");
address = keyIn.readLine();
System.out.print("E-mail address: ");
email = keyIn.readLine();
System.out.print("Phone number: ");
phone = keyIn.readLine();
System.out.print("Date of Birth (MM/DD/YYYY): ");
dateOfBirth = keyIn.readLine();
PersonalContact entry;
entry = new PersonalContact(firstName, lastName, address, email,
phone, dateOfBirth);
contactlist.add(entry);
top++;
try {
entry.write();
} catch (Exception e) {
}
} else if (contactType == 2) {
System.out.print("First Name: ");
firstName = keyIn.readLine();
System.out.print("Last Name: ");
lastName = keyIn.readLine();
System.out.print("Address: ");
address = keyIn.readLine();
System.out.print("E-mail address: ");
email = keyIn.readLine();
System.out.print("Phone number: ");
phone = keyIn.readLine();
System.out.print("Job Title: ");
jobTitle = keyIn.readLine();
System.out.print("Organization: ");
organization = keyIn.readLine();
BusinessContact entry;
entry = new BusinessContact(firstName, lastName, address, email,
phone, jobTitle, organization);
contactlist.add(entry);
top++;
try {
entry.write();
} catch (Exception e) {
}
}
}
public void viewContacts() {
for (int index = 0; index < top; index++) {
System.out.println((index + 1) + " First Name " +
contactlist.get(index).getFirstName());
System.out.println("Last Name " + contactlist.get(index).getLastName());
System.out.println("Address: " + contactlist.get(index).getAddress());
System.out.println("E-mail: " + contactlist.get(index).getEmail());
System.out.println("Phone: " + contactlist.get(index).getPhone());
System.out.println("Job Title " + contactlist.get(index).getJobTitle());
System.out.println("Organization " + contactlist.get(index).
getOrganization());
System.out.println("Date of Birth " + contactlist.get(index).
getDateOfBirth());
}
}
}
Contact Class:
package contactlist;
public class Contact {
private String firstName;
private String lastName;
private String address;
private String email;
private String phone;
public Contact(String firstName, String lastName, String address, String email, String phone){
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.email = email;
this.phone = phone;
}
public String getfirstName() {
return this.firstName;
}
public String setfirstName(){
return (this.firstName = firstName);
}
public String getlastName() {
return this.lastName;
}
public String setlastName(){
return (this.lastName = lastName);
}
public String getAddress() {
return this.address;
}
public String setAddress(){
return (this.address = address);
}
public String getEmail() {
return this.email;
}
public String setEmail(){
return (this.email = email);
}
public String getPhone() {
return this.phone;
}
public String setPhone(){
return (this.phone = phone);
}
String getFirstName() {
return firstName;
}
String getLastName() {
return lastName;
}
String getJobTitle() {
throw new UnsupportedOperationException("Not supported yet.");
}
String getOrganization() {
throw new UnsupportedOperationException("Not supported yet.");
}
String getDateOfBirth() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Explanation / Answer
PROGRAM CODE:
package GUI;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class ContactForm {
public static void main(String[] args) {
List<Contact> contactList = new ArrayList<>();
JFrame frame = new JFrame("Contact Form");
GridBagConstraints constraints = new GridBagConstraints();
JPanel formPanel = new JPanel(new GridBagLayout());
JTextField firstName = new JTextField(10);
JTextField lastName = new JTextField(10);
JTextField address = new JTextField(10);
JTextField email = new JTextField(10);
JTextField phone = new JTextField(10);
JButton add = new JButton("Add");
JButton viewAll = new JButton("View All");
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.ipadx = 30;
constraints.ipady = 5;
constraints.gridx = 0;
constraints.gridy = 0;
formPanel.add(new JLabel("First Name: "), constraints);
constraints.gridx = 1;
constraints.gridy = 0;
formPanel.add(firstName, constraints);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 1;
formPanel.add(new JLabel("Last Name: "), constraints);
constraints.gridx = 1;
constraints.gridy = 1;
formPanel.add(lastName, constraints);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 2;
formPanel.add(new JLabel("Address: "), constraints);
constraints.gridx = 1;
constraints.gridy = 2;
formPanel.add(address, constraints);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 3;
formPanel.add(new JLabel("Email: "), constraints);
constraints.gridx = 1;
constraints.gridy = 3;
formPanel.add(email, constraints);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 4;
formPanel.add(new JLabel("Phone: "), constraints);
constraints.gridx = 1;
constraints.gridy = 4;
formPanel.add(phone, constraints);
constraints.gridheight = 10;
constraints.gridx = 0;
constraints.gridy = 5;
formPanel.add(add, constraints);
constraints.gridx = 1;
constraints.gridy = 5;
formPanel.add(viewAll, constraints);
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
contactList.add(new Contact(firstName.getText(), lastName.getText(), address.getText(), email.getText(), phone.getText()));
firstName.setText("");
lastName.setText("");
email.setText("");
phone.setText("");
address.setText("");
}
});
viewAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFrame f = new JFrame("Contact List");
String column[]={"FIRST NAME","LAST NAME","ADDRESS", "EMAIL", "PHONE"};
String row[][] = new String[contactList.size()][5];
for(int i=0; i<contactList.size(); i++)
{
row[i][0] = contactList.get(i).getfirstName();
row[i][1] = contactList.get(i).getfirstName();
row[i][2] = contactList.get(i).getfirstName();
row[i][3] = contactList.get(i).getfirstName();
row[i][4] = contactList.get(i).getfirstName();
}
JTable jt=new JTable(row, column);
jt.setBounds(30,40,200,300);
JScrollPane sp=new JScrollPane(jt);
f.add(sp);
f.setSize(300,400);
f.setVisible(true);
}
});
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(formPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(500, 300);
}
}
The above code will create a GUI which will add contacts and view all the contacts.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.