JAVA GUI help build this simple gui only. nothing else. Will raise the point val
ID: 3760959 • Letter: J
Question
JAVA GUI help build this simple gui only. nothing else. Will raise the point value to 1000.
http://jackmyers.info/oopda/registrarAssignment.pdf
----------------------------------------------------------------------------------------------------------------
package university;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
public class Person {
private int id;
private int age;
private static int maxAge;
private String firstName;
private String middleName;
private String lastName;
private String email;
private String ssn;
//Constructor
public Person(int id, String firstName, String middleName, String lastName,
String email, String ssn, int age)
{
this.id = id;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.email = email;
this.ssn = ssn;
this.age = age;
if (age > Person.maxAge)
{
Person.maxAge = age;
}
}
public Person(int id, String lastName)
{
this.id = id;
this.lastName = lastName;
}
/**
* getFullName returns a Person's full name
* @return string with format FirstName MiddleName
*/
public String toString()
{
return firstName + " " + middleName + " " + lastName;
}
/**
* getEmailProvider is used to get the
* @return
*/
public String getEmailProvider()
{
int atSign = email.indexOf("@");
return email.substring(atSign);
}
/**
* gestLast4SSN returns the last four digits of an SSN
*
* @return last for digits of SSN
*/
public String getLast4SSN()
{
return ssn.substring(ssn.length()-4);
}
/**
* isEmailValid checks for four errors that could be present in the email string:
* It could possibly contain too many or too little of a either the @ sign or periods
* and checks to see if there are more than one of each, or if there are none used.
* It also checks to see if the period comes before the @ sign which is not valid.
*
* @param address The email address that will be validated
* @return true on successful validation, otherwise false
*/
public static boolean isValidEmail(String address)
{
String errorMsg;
if(!address.contains("@") || !address.contains("."))
{
errorMsg = "Email must use both an "@" sign and have a period!";
JOptionPane.showMessageDialog(null, errorMsg);
return false;
}
else if (address.lastIndexOf("@") != address.indexOf("@"))
{
errorMsg = "There may not be more than one "@" sign!";
JOptionPane.showMessageDialog(null, errorMsg);
return false;
}
else if (address.lastIndexOf(".") != address.indexOf("."))
{
errorMsg = "There may not be more than one period!";
JOptionPane.showMessageDialog(null, errorMsg);
return false;
}
else if (address.lastIndexOf(".") < address.indexOf("@"))
{
errorMsg = "A period must follow the "@" sign!";
JOptionPane.showMessageDialog(null, errorMsg);
return false;
}
else
return true;
}
/**
* Static (class) method isValidSSN performs several validation checks.<br />
* - It ensures a Social Security Number (ssn) contains only digits and
* '-' characters;<br />
* - It ensures that exactly two '-' characters are present in the ssn
* in the correct positions;<br />
* - It also will verify the length of 11 (including hyphens).
*
* @param ssn The Social Security Number to be validated
* @return true on successful validation, otherwise false
*/
public static boolean isValidSSN(String ssn)
{
String errorMsg;
char[] ssnChars = ssn.toCharArray();
int hyphenCount = 0;
for(int i = 0; i < ssnChars.length; i++)
{
if(!Character.isDigit(ssnChars[i]) && ssnChars[i] != '-')
{
errorMsg = "Each character must be a digit or a hyphen!";
JOptionPane.showMessageDialog(null, errorMsg);
return false;
}
else
// Will count the hyphens
if (ssnChars[i] == '-') hyphenCount++;
}
//Checks to see if there are two hyphens
if(hyphenCount > 2 && hyphenCount <= 3)
{
errorMsg = "SSN's must have only two hyphens, you have either too many or not enough!";
JOptionPane.showMessageDialog(null, errorMsg);
return false;
}
if (ssn.length() != 11)
{
errorMsg = "Length of SSN must be 11 character! (Hyphens included)";
JOptionPane.showMessageDialog(null, errorMsg);
return false;
}
if(ssn.indexOf("-") != 3 || ssn.lastIndexOf("-") != 6)
{
errorMsg = "The hyphens must be placed at the 4th and 7th position";
JOptionPane.showMessageDialog(null, errorMsg);
return false;
}
else
return true;
}
/**
* isValidSSN2 Checks to see if the SSN follows a specific pattern
* @param ssn
* @return
*/
public static boolean isValidSSN2(String ssn)
{
String errorMsg = "Invalid SSN!";
String expression = "^\d{3}-\d{2}-\d{4}$";
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(ssn);
if(matcher.matches()) return true;
else
{
JOptionPane.showMessageDialog(null, errorMsg);
return false;
}
}
/**
* isValidAge checks to see if the person is old enough to enroll
* @param myAge Is the age being validated
* @return true on successful validation, otherwise false
*/
public static boolean isValidAge(String myAge)
{
if (Integer.parseInt(myAge) > 15)
{
return true;
}
else {
return false;
}
}
/**
* isOldest checks to see if an age is the max value for the class and returns false if its not
* @return true if a person age is the oldest (including ties), otherwise false
*/
public boolean isOldest()
{
if (this.age >= Person.maxAge) return true;
else
return false;
}
/**
* Returns information about a perosn suitable for display
* @return formatted person info
*/
public String getInfo()
{
String info = this.toString() + " (" + this.getClass().getSimpleName() + ")";
info += " " + this.getEmailProvider();
info += " " + this.getLast4SSN();
info += " " + (this.isOldest() ? "oldest" : "not oldest");
return info;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static int getMaxAge() {
return maxAge;
}
public static void setMaxAge(int maxAge) {
Person.maxAge = maxAge;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
/**
* Returns information about a person suitable
*/
}
package university;
import java.util.HashMap;
import javax.swing.JOptionPane;
public class Student extends Person
{
private String major;
//private HashMap<Integer, section> currentSections = new HashMap<Integer, Section>();
public Student(int id, String firstName, String middleName, String lastName, String email,
String ssn, int age, String major)
{
super(id, firstName, middleName, lastName, email, ssn, age);
this.major = major;
}
public String getMajor()
{
return major;
}
public void setMajor(String major)
{
this.major = major;
}
@Override
public String getInfo()
{
String info = super.getInfo();
info += " " + this.major;
return info;
}
}
package university;
public class Instructor extends Person
{
private String department;
public Instructor(int id, String firstName, String middleName, String lastName,
String email, String ssn, int age, String department) {
super(id, firstName, middleName, lastName, email, ssn, age);
this.department = department;
}
public Instructor(int id, String lastName)
{
super(id, lastName);
// TODO Auto-generated constructor stub
}
@Override
public String getInfo()
{
String info = super.getInfo();
info += " " + this.department;
return info;
}
public String getDepartment()
{
return department;
}
public void setDepartment(String department)
{
this.department = department;
}
}
package university;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
//TODO Turn combobox into list
public class GUI extends JFrame
{
private ArrayList<Section> section = new ArrayList<Section>();
private JPanel listPanel = new JPanel();
private JPanel sectionPanel = new JPanel();
private JPanel departmentPanel = new JPanel();
private JPanel descriptionPanel = new JPanel();
private Student student;
private JLabel selectDeparment = new JLabel("Select Department");
private JLabel studentName = new JLabel(student.getFirstName() + " " + student.getLastName());
private JLabel sectionLabel = new JLabel("Sections");
private JComboBox<String> comboDepartment = new JComboBox<String>();
comboDepartment = createDep
private static String title = "Registrar";
public GUI (Student student, ArrayList<Section> section)
{
super(title);
this.student = student;
this.section = section;
setLayout(new BorderLayout());
this.getContentPane().add(sectionPanel, BorderLayout.CENTER);
this.getContentPane().add(departmentPanel, BorderLayout.NORTH);
departmentPanel.add(comboDepartment);
departmentPanel.add(selectDeparment);
this.getContentPane().add(descriptionPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class SectionTally implements ActionListener
{
public void actionPerformed (Event e)
{
}
}
}
Explanation / Answer
UniversityDriverDemo.java
package university;
import java.util.HashMap;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class UniversityDriverDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Get information for Person
String fname = getFieldData(sc, "Enter person's first name");
String mname = getFieldData(sc, "Enter person's middle name");
String lname = getFieldData(sc, "Enter person's last name");
String email, ssn, age;
HashMap<Integer, Person> people = new HashMap<Integer, Person>();
boolean emailValid = false;
do {
email = getFieldData(sc, "Enter person's email address");
emailValid = Person.isValidEmail(email);
} while (!emailValid);
boolean ssnValid = false;
do {
ssn = getFieldData(sc, "Enter person's SSN in ###-##-#### format");
ssnValid = Person.isValidSSN(ssn);
} while (!ssnValid);
boolean ageValid = false;
do {
age = getFieldData(sc, "Enter person's age");
ageValid = Person.isValidAge(age);
} while (!ageValid);
int ageAsInt = Integer.parseInt(age);
// Make instances of Person and its subclasses
Person myPerson = new Person(1, fname, mname, lname, email, ssn, ageAsInt);
Student myStudent = new Student(2, "Jane", "Marie", "Doe",
"doeS@students.rowan.edu", "222-33-4444", 19, "Computer Science");
Instructor myInstructor = new Instructor(3, "Stephen", "J.", "Hartley",
"hartleyS@rowan.edu", "333-44-5555", 35, "Math/Science");
// Add to HashMap
people.put(myPerson.getId(), myPerson);
people.put(myStudent.getId(), myStudent);
people.put(myInstructor.getId(), myInstructor);
// Iterate through polymorphic HashMap
for (Person person : people.values()) {
System.out.println(" " + person + " (" +
person.getClass().getSimpleName() + ")");
System.out.println(person.getEmailDomain());
System.out.println(person.getLast4SSN());
System.out.println(person.isOldest() ? "oldest" : "not oldest");
if (person instanceof Student) {
Student student = (Student) person;
System.out.println(student.getMajor());
}
if (person instanceof Instructor) {
Instructor instructor = (Instructor) person;
System.out.println(instructor.getDepartment());
}
}
}
private static String getFieldData(Scanner sc, String prompt) {
System.out.println(prompt);
return sc.nextLine();
}
}class Person {
public int id;
public String firstName;
public String middleName;
public String lastName;
public String email;
public String ssn;
public int age;
static int maxAge;
/**
* getFullName returns a Person's full name
*
* @return string with format FirstName MiddleName LastName
*/
public String toString() {
return firstName + " " + middleName + " " + lastName;
}
/**
* getEmailDomain returns the part of the email address after
* the "@" which corresponds to the domain.
*
* @return the domain portion of the email address
*/
public String getEmailDomain() {
int ampLocation = email.indexOf("@");
return email.substring(ampLocation+1);
}
/**
* getLast4SSN returns the last four digits of a Social
* Security Number
*
* @return last four digits of SSN
*/
public String getLast4SSN() {
return ssn.substring(ssn.length()-4);
}
/**
* Static (class) method isValidEmail performs several validation checks.<br />
* - It ensures email address contains an '@' and a '.' character;<br />
* - It also checks that only one '@' is present in the email;<br />
* - It also will verify that at least one '.' follows the '@' character.
*
* @param address The email address to be validated
* @return true on successful validation, otherwise false
*/
public static boolean isValidEmail(String address) {
String errorMessage;
if (!address.contains("@") || !address.contains(".")) {
errorMessage = "Email address must contain both an ampersand and period.";
JOptionPane.showMessageDialog(null, errorMessage);
return false;
}
else if (address.lastIndexOf("@") != address.indexOf("@")) {
errorMessage = "Email address cannot contain two ampersands.";
JOptionPane.showMessageDialog(null, errorMessage);
return false;
}
else if (address.lastIndexOf(".") < address.indexOf("@")) {
errorMessage = "A period must follow the ampersand in an email address.";
JOptionPane.showMessageDialog(null, errorMessage);
return false;
}
else
return true;
}
/**
* Static (class) method isValidSSN performs several validation checks.<br />
* - It ensures a Social Security Number (ssn) contains only digits and
* '-' characters;<br />
* - It ensures that exactly two '-' characters are present in the ssn
* in the correct positions;<br />
* - It also will verify the length of 11 (including hyphens).
*
* @param ssn The Social Security Number to be validated
* @return true on successful validation, otherwise false
*/
public static boolean isValidSSN(String ssn) {
String errorMessage;
char[] ssnChars = ssn.toCharArray();
int numberOfHyphens = 0;
for (int i=0; i < ssnChars.length; i++) {
if (!Character.isDigit(ssnChars[i]) && ssnChars[i] != '-') {
errorMessage = "Every character of an SSN must be a digit or a hyphen.";
JOptionPane.showMessageDialog(null, errorMessage);
return false;
}
else
// Character is either a digit or a hyphen, let's count the hyphens!
if (ssnChars[i] == '-') numberOfHyphens++;
}
// Make sure number of slashes is two
if (numberOfHyphens != 2) {
errorMessage = "SSNs must contain two hyphens.";
JOptionPane.showMessageDialog(null, errorMessage);
return false;
}
if (ssn.length() != 11) {
errorMessage = "Length of SSN must be 11 characters including hyphens.";
JOptionPane.showMessageDialog(null, errorMessage);
return false;
}
else if (ssn.indexOf("-") != 3 || ssn.lastIndexOf("-") != 6) {
errorMessage = "Hyphens must be included in the 4th and 7th positions.";
JOptionPane.showMessageDialog(null, errorMessage);
return false;
}
else
return true;
}
/**
* Static (class) method isValidAge checks to see if the String can
* be turned into a valid integer and, if so, if that value is greater
* than 15.
*
* @param myAge The age to be validated
* @return true on successful validation, otherwise false
*/
public static boolean isValidAge(String myAge) {
if (Integer.parseInt(myAge) > 15) {
return true;
}
else {
return false;
}
}
/**
* Method isOldest checks to see if a particular age is the max value for class
* or false if it is not
*
* @return true if a person's age is the oldest (including ties), otherwise false
*/
public boolean isOldest() {
if (this.age >= Person.maxAge) return true;
else return false;
}
// Getters and Setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// Constructor
public Person(int id, String firstName, String middleName, String lastName,
String email, String ssn, int age) {
this.id = id;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.email = email;
this.ssn = ssn;
this.age = age;
if (age > Person.maxAge) {
Person.maxAge = age;
}
}
}
class Instructor extends Person {//instructor subclass odf person
private String department;
public Instructor(int id, String firstName, String middleName,
String lastName, String email, String ssn, int age, String department) {//constructor istructor with arguments
super(id, firstName, middleName, lastName, email, ssn, age);
this.department = department;
}
public String getDepartment() {//setter and getter values of depeartment
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
class Student extends Person {//student is asub class od person
private String major;
public Student(int id, String firstName, String middleName,
String lastName, String email, String ssn, int age, String major) {//contructor studen with fn,ln,email,ssn,age,major as parameters
super(id, firstName, middleName, lastName, email, ssn, age);
this.major = major;
}
public String getMajor() {//setter and geter values of major
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
output
run:
Enter person's first name
ussan
Enter person's middle name
bolt
Enter person's last name
bolt
Enter person's email address
bolt@gmail.com
Enter person's SSN in ###-##-#### format
123-33-4444
Enter person's age
56
ussan bolt bolt (Person)
gmail.com
4444
oldest
Jane Marie Doe (Student)
students.rowan.edu
4444
not oldest
Computer Science
Stephen J. Hartley (Instructor)
rowan.edu
5555
not oldest
Math/Science
BUILD SUCCESSFUL (total time: 48 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.