Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE CODE IN JAVA, no paper thank you Q: My Person class : public class Person implements Comparable { private String firstName; private String lastName;

PLEASE CODE IN JAVA, no paper thank you

Q:

My Person class :

public class Person implements Comparable {

private String firstName;

private String lastName;

private String street;

private String postalCode;

private String phone;

private String email;

// default constructor

public Person() {

}

// Parameterized constructor

public Person(String firstName, String lastName, String street, String postalCode, String phone, String email) {

this.firstName = firstName;

this.lastName = lastName;

this.street = street;

this.postalCode = postalCode;

this.phone = phone;

this.email = email;

}

// Method which enables comparison between 2 person objects

// Useful in sorting

public int compareTo(Person p) {

return (this.getLastName().compareTo(p.getLastName()));

}

// Accessors and mutators from here on

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public String getStreet() {

return street;

}

public void setStreet(String street) {

this.street = street;

}

public String getPostalCode() {

return postalCode;

}

public void setPostalCode(String postalCode) {

this.postalCode = postalCode;

}

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

}

My PersonRepository class :

import java.util.Arrays;

import java.util.Scanner;

class PersonRepository

{

private static Scanner sc = new Scanner(System.in);

private static int MAX_PERSONS = 10;

private static Person[] persons = new Person[MAX_PERSONS];

private static int currentIndex = 0; // to mark the current position (index) of the array.

// helper method to print the menu.

private static int promptMenu()

{

while (true)

{

System.out.print("1. Add a person 2. Remove a person 3. Sort by lastname 4. Quit Your choice: ");

int userChoice = sc.nextInt();

sc.nextLine();

// in case user enters invalid choice.

if (userChoice < 1 || userChoice > 4)

{

System.out.println("That's not a valid option. Please select correct option");

}

return userChoice;

}

}

public static void main(String[] args)

{

while (true)

{

switch (promptMenu())

{

case 1:

addPerson();

break;

case 2:

removePerson();

break;

case 3:

sortByLastName();

break;

case 4:

return;

}

}

}

private static void sortByLastName() {

// we make use of built-in sort method in Array class to sort.

System.out.println("Before sorting: ");

displayPersons();

// we copy the existing array elements into a temp array.

// so that the sorted order will not be reflecting in the original array.

Person[] personsToBeSorted = new Person[currentIndex];

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

personsToBeSorted[i] = persons[i];

}

Arrays.sort(personsToBeSorted);

System.out.println("After sorting: ");

for (int i = 0; i < personsToBeSorted.length; i++) {

System.out.println(personsToBeSorted[i].getFirstName() + " " + personsToBeSorted[i].getLastName());

}

}

private static void addPerson() {

if (currentIndex == MAX_PERSONS)

{

System.out.println("Persons database is full. Can't add anymore");

return;

}

System.out.print("Enter firstname: ");

String firstName = sc.nextLine();

System.out.print("Enter lastname: ");

String lastName = sc.nextLine();

System.out.print("Enter street name: ");

String street = sc.nextLine();

System.out.print("Enter postal code: ");

String postalCode = sc.nextLine();

System.out.print("Enter phone number: ");

String phone = sc.nextLine();

System.out.print("Enter email address: ");

String email = sc.nextLine();

Person person = new Person(firstName, lastName, street, postalCode, phone, email);

persons[currentIndex++] = person;

displayPersons();

}

private static void removePerson() {

if (currentIndex == 0)

{

System.out.println("No persons to delete.");

return;

}

System.out.print("Enter firstname of the person: ");

String firstName = sc.nextLine();

System.out.print("Enter lastname of the person: ");

String lastName = sc.nextLine();

for (int i = 0; i < currentIndex; i++)

{

if (persons[i].getFirstName().equalsIgnoreCase(firstName)

&& persons[i].getLastName().equalsIgnoreCase(lastName))

persons[--currentIndex] = null;

}

displayPersons();

}

private static void displayPersons() {

System.out.println("Persons in the database: ");

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

System.out.println(persons[i].getFirstName() + " " + persons[i].getLastName());

}

}

}

Question:

1. Create a class called Student and another called Teacher. Both of these classes inherit from the Person class you created in the previous assignment. Specifications are seen below: Student - Student number - grade number - period 1 course name - period 2 course name - period 3 course name - period 4 course name

Teacher -Department name (assume 1 only) - course 1 name - course 2 name - course 3 name - student list for course 1 (max 20) - student list for course 2 (max 20) - student list for course 3 (max 20)

2. The teacher class should contain a method to add a student to a course. Eg: addStudentToClass(String coursename, Student s)

3. Create a class called Principal which inherits from Person. The Principal class contains a school name, and a list of teachers (max 10).

4. The principal class should contain a method to add a Teacher to the school.

Eg: addTeacherToSchool(Teacher t)

NEXT STEP :

1. Create set and get methods for the Principal, Teacher, and Student classes using the attached Person Class as a template. 2. Design a program to add students and teacher. Create a menu system similar to the one seen below:

1. add a teacher - this method will add a teacher to the principal 2. add a Student - this method will create a student and add it to all the teacher with the same course 3. quit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

List the advantages and disadvantages of the pay programs. page 505

Answered: 1 week ago