25
Person Class package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor Code * @author Alex */ public class Person implements Cloneable { // instance member varialbes private String firstName; private String lastName; private int age; private String mStatus; private String gender; // static class member variables protected static int totPeople = 0; protected static final int MAXPEOPLE = 5; protected static Person[] people = new Person[MAXPEOPLE]; public Person() { firstName = "FIRST"; lastName = "LAST"; age = 0; mStatus = "unknown"; gender = "unknown"; } public Person(String a, String b, String c, int d, String e) { setFirstName(a);

alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

Embed Size (px)

Citation preview

Page 1: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

Person Class

package assignment7;

/** Alex Rockelli 108942222 CIS 331 Section 4

* This work is done in compliance with the JMU Honor Code

* @author Alex

*/

public class Person implements Cloneable {

// instance member varialbes

private String firstName;

private String lastName;

private int age;

private String mStatus;

private String gender;

// static class member variables

protected static int totPeople = 0;

protected static final int MAXPEOPLE = 5;

protected static Person[] people = new Person[MAXPEOPLE];

public Person() {

firstName = "FIRST";

lastName = "LAST";

age = 0;

mStatus = "unknown";

gender = "unknown";

}

public Person(String a, String b, String c, int d, String e) {

setFirstName(a);

setLastName(b);

setGender(c);

Page 2: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

setAge(d);

setMStatus(e);

}

// instance methods

public String personInfo(boolean fullData) {

String data = toString() + " ";

if (!fullData)

data += "Name: " + firstName + "\nLast name: " + lastName;

else

data += "Name: " + firstName + " " + lastName +

"\nAge: " + age + "\nGender: " + gender +

"\nMarital Status: " + mStatus;

return data;

}

public boolean equals(String test) {

String compare = firstName + " " + lastName;

return compare.equalsIgnoreCase(test);

}

public String getFirstName() {

return firstName;

}

public void setFirstName(String x) {

// change first name to...

String capLetter = x.substring(0, 1);

String rest = x.substring(1, x.length());

firstName = capLetter.toUpperCase() + rest.toLowerCase();

}

public String getLastName() {

return lastName;

Page 3: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

}

public void setLastName(String x) {

// change last name to...

String capLetter = x.substring(0, 1);

String rest = x.substring(1, x.length());

lastName = capLetter.toUpperCase() + rest.toLowerCase();

}

public String getGender() {

return gender;

}

public void setGender(String x) {

// change gender to...

if (x.equalsIgnoreCase("male"))

gender = x;

else if (x.equalsIgnoreCase("female"))

gender = x;

else

gender = "Unknown";

}

public int getAge() {

return age;

}

public void setAge(int x) {

// change age to...

if (x < 0)

age = 0;

else

age = x;

}

Page 4: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

public String getMStatus() {

return mStatus;

}

public void setMStatus(String x) {

// change marital status to...

if (x.equalsIgnoreCase("married"))

mStatus = x;

else if (x.equalsIgnoreCase("divorced"))

mStatus = x;

else if (x.equalsIgnoreCase("widowed"))

mStatus = x;

else if (x.equalsIgnoreCase("single"))

mStatus = x;

else

mStatus = "unknown";

}

@Override

public Object clone() {

try {

return super.clone();

}

catch (CloneNotSupportedException ex) {

System.out.print("Sorry, " + ex.toString());

return null;

}

}

@Override

public String toString() {

return "Person";

Page 5: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

}

// static class methods

public static String listPersons(Person[] x) {

String namesList = "";

for (int i = 0; i < totPeople; i++) {

if (people[i].firstName != null && people[i].lastName != null)

namesList += people[i].toString() + " " +

people[i].firstName + " " + people[i].lastName + "\n";

}

if (namesList.equals(""))

namesList = "No people have been added yet!\n";

return namesList;

}

public static int findPerson(String name) {

int found = -1;

for (int i = 0; i < totPeople; i++) {

if (people[i].equals(name))

found = i;

}

return found;

}

public static boolean addPerson(String a, String b, String c, int d, String e) {

for (int i = 0; i < MAXPEOPLE; i++) {

if (people[i] == null){

people[i] = new Person(a, b, c, d, e);

totPeople++;

return true;

}

Page 6: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

}

return false;

}

public static double averageAge(Person[] x) {

double averageAge = 0;

for (int i = 0; i < totPeople; i++) {

averageAge += x[i].age;

}

averageAge /= totPeople;

return averageAge;

}

public static Person getPerson(int x) {

return people[x];

}

public static Person[] getPeople() {

return Person.people;

}

}

Student Class

package assignment7;

/**Alex Rockelli 108942222 CIS 331 Section 4

* This work is done in compliance with the JMU Honor Code

*

* @author Alex

*/

public class Student extends Person{

// instance member variables

private String major;

Page 7: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

private String classStanding;

private double gpa;

private Faculty advisor;

// default constructor

public Student() {

super();

setClassStanding("freshman");

setMajor("undeclared");

setGPA(0.0);

}

// overloaded constructor

public Student(String a, String b, String c, int d, String e, String f, String g, double h) {

super(a, b, c, d, e);

setMajor(f);

setClassStanding(g);

setGPA(h);

}

public String getMajor() {

return major;

}

public void setMajor(String x) {

if (x.equalsIgnoreCase("cis"))

major = "CIS";

else if (x.equalsIgnoreCase("marketing"))

major = x;

else if (x.equalsIgnoreCase("management"))

major = x;

else if (x.equalsIgnoreCase("finance"))

major = x;

Page 8: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

else if (x.equalsIgnoreCase("accounting"))

major = x;

else

major = "undeclared";

}

public String getClassStanding() {

return classStanding;

}

public void setClassStanding(String x) {

if (x.equalsIgnoreCase("senior"))

classStanding = x.substring(0,1).toUpperCase() + x.substring(1).toLowerCase();

else if (x.equalsIgnoreCase("junior"))

classStanding = x.substring(0,1).toUpperCase() + x.substring(1).toLowerCase();

else if (x.equalsIgnoreCase("sophomore"))

classStanding = x.substring(0,1).toUpperCase() + x.substring(1).toLowerCase();

else

classStanding = "Freshman";

}

public double getGPA() {

return gpa;

}

public void setGPA(double test) {

if ((test > 0.0) && (test <= 4.0))

gpa = test;

else

gpa = 0.0;

}

public Faculty getAdvisor() {

return advisor;

Page 9: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

}

public void setAdvisor(Faculty x) {

advisor = x;

}

@Override

public String personInfo(boolean fullData) {

String data = "";

data += super.personInfo(fullData);

if (fullData)

if (advisor != null)

data += "\nMajor: " + major + "\nClass Standing: " + classStanding +

"\nGPA: " + gpa + "\nAdvisor: " + advisor.toString() + " "

+ advisor.getFirstName() + " " + advisor.getLastName();

else

data += "\nMajor: " + major + "\nClass Standing: " + classStanding +

"\nGPA: " + gpa + "\nAdvisor: no advisor assigned";

return data;

}

@Override

public String toString() {

return "Student";

}

// static class methods

public static String showOverallGPA(Person[] x) {

// pay close attention to this method

// requires type casting to Student object

int numStudents = 0;

double totGPA = 0;

String overall = "";

Page 10: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

for (int i = 0; i < totPeople; i++) {

if (Person.people[i] instanceof Student) {

totGPA += (((Student)people[i]).getGPA());

numStudents++;

}

}

totGPA /= numStudents;

if (numStudents !=1)

overall = "There are " + numStudents + " students and the overall average GPA is "

+ totGPA + "\n";

else

overall = "There is " + numStudents + " student and the overall average GPA is "

+ totGPA + "\n";

return overall;

}

public static boolean addStudent(String a, String b, String c, int d, String e, String f, String g, double h) {

for (int i = 0; i < Person.MAXPEOPLE; i++) {

if (Person.people[i] == null){

Person.people[i] = new Student(a, b, c, d, e, f, g, h);

Person.totPeople++;

return true;

}

}

return false;

}

}

Faculty Class

Page 11: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

package assignment7;

/**Alex Rockelli 108942222 CIS 331 Section 4

* This work is done in compliance with the JMU Honor Code

*

* @author Alex

*/

public class Faculty extends Person{

//instance member variables

private String professorType;

public Faculty() {

super();

setProfessorType("lecturer");

}

public Faculty(String a, String b, int c, String d, String e, String f) {

super(a, b, d, c, e);

setProfessorType(f);

}

public String getProfessorType() {

return professorType;

}

public void setProfessorType(String x) {

if (x.equalsIgnoreCase("assistant"))

professorType = x;

else if (x.equalsIgnoreCase("associate"))

professorType = x;

else if (x.equalsIgnoreCase("full"))

professorType = x;

else

professorType = "lecturer";

Page 12: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

}

@Override

public String toString() {

return "Faculty";

}

@Override

public String personInfo(boolean fullData) {

String data = "";

data += super.personInfo(fullData);

if (fullData)

data += "\nProfessor Type: " + professorType;

return data;

}

// static class method

public static boolean addFaculty(String a, String b, String c, int d, String e, String f) {

for (int i = 0; i < Person.MAXPEOPLE; i++) {

if (Person.people[i] == null){

Person.people[i] = new Faculty(a, b, d, c, e, f);

Person.totPeople++;

return true;

}

}

return false;

}

}

Application Class

package assignment7;

Page 13: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

import java.util.*;

/**Alex Rockelli 108942222 CIS 331 Section 4

* This work is done in compliance with the JMU Honor Code

*

* @author Alex

*/

public class PeopleApplication {

public static void main(String[] args) {

// welcome message

System.out.println("Welcome to People Application\n");

// try catch for error handling

try {

// int for switch case

int option;

// start with a loop for continuation

do {

Scanner value = new Scanner(System.in);

// show the menu

option = menuChoice();

// switch case for option value

switch (option) {

case 1: // addPerson

option1();

break;

case 2: // addStudent

option2();

break;

case 3: // addFaculty

option3();

Page 14: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

break;

case 4: // listPersons

option4();

break;

case 5: // findPerson

System.out.println("Enter the first and last name of the person to search for");

String search = value.nextLine();

int num = Person.findPerson(search);

if (num != -1) {

System.out.println("Person Information");

System.out.println(Person.getPerson(num).personInfo(true));

System.out.println();

} else {

System.out.println("Person not found\n");

}

break;

case 6: //averageAge

System.out.println("Average age is: " + Person.averageAge(Person.getPeople()));

System.out.println();

break;

case 7: // showOverallGPA

System.out.println(Student.showOverallGPA(Person.people));

break;

case 8: // assign Advisor

assignAdvisor();

break;

case 9: // quit

System.out.println("Goodbye!");

System.exit(0);

Page 15: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

break;

}

} while (option != 9);

} catch (InputMismatchException | NullPointerException | ArrayIndexOutOfBoundsException ex) {

// error message if exception caught interacting with program

System.out.println("\nSorry, " + ex.toString());

System.out.println("Run me again please!");

}

}

// method for displaying menu

public static int menuChoice() {

// boolean for menu loop continuation

boolean keepGoing = true;

// int for user choice

int choice = 0;

while (keepGoing) {

// menu output

System.out.println("Choices are:");

System.out.println("(1) Add a new person");

System.out.println("(2) Add a new student");

System.out.println("(3) Add a new faculty");

System.out.println("(4) List the people");

System.out.println("(5) Display information about a person");

System.out.println("(6) Display average age of people");

System.out.println("(7) Display average gpa of students");

System.out.println("(8) Assign an advisor to a student");

System.out.println("(9) Quit");

System.out.println("\nWhat is your choice?");

// scanner for user choice

Page 16: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

Scanner input = new Scanner(System.in);

// try catch block for menu choice and input error handling

try {

choice = input.nextInt();

if (choice > 9 || choice < 1) {

System.out.println("***Invalid choice, please try again\n");

} else {

keepGoing = false;

}

return choice;

} catch (InputMismatchException ex) {

// error message for menu choice input

System.out.println("***Invalid choice, please try again\n");

input.nextLine();

}

}

return choice;

}

// option 1 method addPerson

public static void option1() {

Scanner value = new Scanner(System.in);

System.out.println("Enter the first and last name of the person");

String first = value.next();

String last = value.next();

// while loop and try catch for correct age numeric input

boolean keepGoing = true;

int age = 0;

while (keepGoing) {

try {

Page 17: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

System.out.println("Enter the person's age");

age = value.nextInt();

keepGoing = false;

} catch (InputMismatchException ex) {

System.out.println("***Invalid input, enter a number\n");

value.nextLine();

}

}

System.out.println("Enter the person's marital status");

String status = value.next();

System.out.println("Enter the person's gender");

String gender = value.next();

if (Person.addPerson(first, last, gender, age, status)) {

System.out.println("Person Added\n");

} else {

System.out.println("Insufficient space to add person\n");

}

}

// option 2 method addStudent

public static void option2() {

Scanner value = new Scanner(System.in);

System.out.println("Enter the first and last name of the person");

String first = value.next();

String last = value.next();

// while loop and try catch for correct age numeric input

boolean keepGoing = true;

int age = 0;

while (keepGoing) {

try {

Page 18: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

System.out.println("Enter the person's age");

age = value.nextInt();

keepGoing = false;

} catch (InputMismatchException ex) {

System.out.println("***Invalid input, enter a number\n");

value.nextLine();

}

}

System.out.println("Enter the person's marital status");

String status = value.next();

System.out.println("Enter the person's gender");

String gender = value.next();

System.out.println("Enter the student's class standing");

String standing = value.next();

System.out.println("Enter the student's major");

String major = value.next();

// while loop and try catch for correct gpa numeric input

boolean realGPA = true;

double gpa = 0.0;

while (realGPA) {

try {

System.out.println("Enter the student's gpa");

gpa = value.nextDouble();

if ((gpa >= 0.0) && (gpa <= 4.0))

realGPA = false;

else

System.out.println("***Invalid input, enter a number between 0.0 and 4.0\n");

} catch (InputMismatchException ex) {

System.out.println("***Invalid input, enter a number between 0.0 and 4.0\n");

Page 19: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

value.nextLine();

}

}

if (Student.addStudent(first, last, gender, age, status, major, standing, gpa)) {

System.out.println("Student Added\n");

} else {

System.out.println("Insufficient space to add student\n");

}

}

// option 3 method addFaculty

public static void option3() {

Scanner value = new Scanner(System.in);

System.out.println("Enter the first and last name of the person");

String first = value.next();

String last = value.next();

// while loop and try catch for correct age numeric input

boolean keepGoing = true;

int age = 0;

while (keepGoing) {

try {

System.out.println("Enter the person's age");

age = value.nextInt();

keepGoing = false;

} catch (InputMismatchException ex) {

System.out.println("***Invalid input, enter a number\n");

value.nextLine();

}

}

System.out.println("Enter the person's marital status");

Page 20: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

String status = value.next();

System.out.println("Enter the person's gender");

String gender = value.next();

System.out.println("Enter the faculty member's professor type (lecturer, assistant, associate, full)");

String professorType = value.next();

if (Faculty.addFaculty(first, last, gender, age, status, professorType)) {

System.out.println("Faculty Added\n");

} else {

System.out.println("Insufficient space to add faculty\n");

}

}

// option 4 method listPeople

public static void option4() {

System.out.println("People List\n" + Person.listPersons(Person.getPeople()));

}

public static void assignAdvisor() {

// counters for name matches

int studentMatchCount = 0;

int advisorMatchCount = 0;

Scanner value = new Scanner(System.in);

System.out.println("Enter the first and last name of the student to search for");

String studentNameTest = value.nextLine();

// step through people[] to find a name match (for Student)

for (int i = 0; i < Person.totPeople; i++) {

if (Person.people[i].equals(studentNameTest)) {

studentMatchCount++;

// check if the instance with the name match is a Student instance

if (Person.people[i] instanceof Student) {

// if true, ask for advisor name

Page 21: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

System.out.println("Enter the first and last name of the advisor to search for");

String advisorNameTest = value.nextLine();

// step through people[] to find a name match (for advisor)

for (int j = 0; j < Person.totPeople; j++) {

if (Person.people[j].equals(advisorNameTest)) {

advisorMatchCount++;

// check if the instance with the name match is a Faculty instance

if (Person.people[j] instanceof Faculty) {

// if yes, print out successful message

System.out.println("Advisor assigned\n");

((Student)Person.people[i]).setAdvisor(((Faculty)Person.people[j]));

}

else

// name match for person was not a Faculty instance

System.out.println("Sorry, that name belongs to a person, but not a faculty\n");

}

}

if (advisorMatchCount == 0)

System.out.println("Sorry, no faculty found\n");

}

else

System.out.println("Sorry, that name belongs to a person, but not a student\n");

}

}

if (studentMatchCount == 0)

System.out.println("Sorry, no student found\n");

}

}

Page 22: alexrockelli.files.wordpress.com€¦ · Web viewPerson Class. package assignment7; /** Alex Rockelli 108942222 CIS 331 Section 4 * This work is done in compliance with the JMU Honor

UML Diagram