In: Computer Science
Alice is going to create a notebook including the profile of all her friends. For each of her friends, she would like to keep first-name, last-name, cell number, and birthdate (month and day). At the beginning, she doesn’t know how many spaces she should reserve for recording her friends, so she gradually inputs her friends’ information into it. Please help her by first creating a Java class, called Person, with the required information from description above, along with any required setter/getter methods. Then create, a Java class, called FriendsList, that keeps the list of some persons as someone’s friends, using the Person class above, and performs the required operations, such as add new friends, delete/ modify an existing one, return the list of her friends sorted by last-names, report the number of her friends, report the list of her friends who born in a given month sorted by their day of birth, and report the list of her friends who born in a given day of a month, sorted by their last-names. She also needs to get the cell number of a friend by giving her/his first-name and last-name. Create a tester class, called MyFriends, to test the FriendList class by creating a new object of this class, adding some friends, modifying and deleting some existing friends, retrieving and printing all friends, retrieving and printing the list of friends who born in a given month, retrieving and printing the list of friends who born in a given day of a month, and finding the cell number of an existing friend by giving her/his first-name and last-name. Also, create a separate copy of the FriendList object you have already created, and remove all friends with first-name “Shane” from the new object.
Create a subclass of Person, called Employee, such that it can keep extra information of a person including year of hiring, annual salary, vacation days, and unused vacation days. Provide required setter/getter methods for this class. Create a subclass of Employee, called Manager, which also has monthly bonus, and the list of all her/his employees. Provide required setter/getter methods for this class. Create a class, called Company, with the following information: name of the company, starting year, and the list of all its employees/managers. Provide required setter/getter methods for this class. Create a tester class, called MyCompany, in which you should test all the above classes by creating a company, adding some employees/managers into it. Bonus: Provide two listings of the company’s employees/managers, one sorted by last-names, and one sorted by each manager and then her/his employees.
Hello dear,
PLEASE UPVOTE IF THIS ANSWER SEEMS HELPFUL AS IT GIVES THE CONFIDENCE TO HELP MORE STUDENTS
THANKYOU
Here is your code with comments,please go through it.
(Person.java) public class Person { private String firstName; private String lastName; private String cellNumber; private int birthMonth; private int birthDay; public Person() { firstName = ""; lastName = ""; cellNumber = ""; birthMonth = 0; birthDay = 0; } public Person(String firstName, String lastName, String cellNumber, int birthMonth, int birthDay) { this.firstName = firstName; this.lastName = lastName; this.cellNumber = cellNumber; this.birthMonth = birthMonth; this.birthDay = birthDay; } 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 getCellNumber() { return cellNumber; } public void setCellNumber(String cellNumber) { this.cellNumber = cellNumber; } public int getBirthMonth() { return birthMonth; } public void setBirthMonth(int birthMonth) { this.birthMonth = birthMonth; } public int getBirthDay() { return birthDay; } public void setBirthDay(int birthDay) { this.birthDay = birthDay; } @Override public String toString() { return "Name: " + firstName + " " + lastName + ", Brithday: " + getBirthMonth() + "/" + getBirthDay() + ", Cell number: " + getCellNumber(); } }
(FriendsList.java)
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class FriendsList { private ArrayList<Person> friends; public FriendsList() { friends = new ArrayList<>(); } public void addFriend(Person person) { friends.add(person); } //delete using first name, last name, birth month and day public void deleteFriend(String firstName, String lastName, int month, int day) { Person person = null; for (Person p : friends) { if (p.getFirstName().equalsIgnoreCase(firstName) && p.getLastName().equalsIgnoreCase(lastName) && p.getBirthMonth() == month && p.getBirthDay() == day) { person = p; break; } } if (person != null) friends.remove(person); } // delete using cell number as it is unique for each person public void deleteFriend(String cellNumber) { Person person = null; for (Person p : friends) { if (p.getCellNumber().equals(cellNumber)) { person = p; break; } } if (person != null) friends.remove(person); } // sort by last name public ArrayList<Person> sortByLastName() { Collections.sort(friends, new Comparator<Person>() { @Override public int compare(Person person, Person t1) { return person.getLastName().compareTo(t1.getLastName()); } }); return friends; } // get total friend count public int totalFriends() { return friends.size(); } // find friend who born in a given month public ArrayList<Person> friendsBornInMonth(int month) { ArrayList<Person> bornSameMonth = new ArrayList<>(); for (Person person : friends) { if (person.getBirthMonth() == month) { bornSameMonth.add(person); } } Collections.sort(bornSameMonth, new Comparator<Person>() { @Override public int compare(Person person, Person t1) { return person.getBirthDay() - t1.getBirthDay(); } }); return bornSameMonth; } // born on a particular day public ArrayList<Person> friendsBornOnDay(int day) { ArrayList<Person> bornOnDay = new ArrayList<>(); for (Person person : friends) { if (person.getBirthDay() == day) { bornOnDay.add(person); } } Collections.sort(bornOnDay, new Comparator<Person>() { @Override public int compare(Person person, Person t1) { return person.getLastName().compareTo(t1.getLastName()); } }); return bornOnDay; } // get cell number public String getCellNumber(String fName, String lName) { for (Person person : friends) { if (person.getFirstName().equalsIgnoreCase(fName) && person.getLastName().equalsIgnoreCase(lName)) { return person.getCellNumber(); } } return "None found"; } // delete by first name public void deleteByFirstName(String firstName) { for (Person person : friends) { if (person.getFirstName().equalsIgnoreCase(firstName)) { friends.remove(person); } } } }
(MyFriends.java)
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class MyFriends { public static void main(String[] args) { FriendsList fbFriends = new FriendsList(); Person jack = new Person("Jack","Sparrow","888-99-1000",12,31); Person ron = new Person("Ronald","Hughes","888-77-1000",12,15); Person peter = new Person("Peter","Romanko","123-99-1000",6,15); fbFriends.addFriend(jack); fbFriends.addFriend(ron); fbFriends.addFriend(peter); System.out.println("All friends"); ArrayList<Person> friends = fbFriends.sortByLastName(); for(Person person:friends){ System.out.println(person); } System.out.println("Born in December"); ArrayList<Person> decemberBorn = fbFriends.friendsBornInMonth(12); for(Person person:decemberBorn){ System.out.println(person); } System.out.println("Born 15"); ArrayList<Person> born15 = fbFriends.friendsBornOnDay(15); for(Person person:born15){ System.out.println(person); } System.out.println(fbFriends.totalFriends()); System.out.println("Person Ron's phone #: "+fbFriends.getCellNumber("Ronald","Hughes")); fbFriends.deleteFriend("123-99-1000"); System.out.println(fbFriends.totalFriends()); } }
Employee.java
package com.core.java;
//This class extends person
public class Employee extends Person {
//Instance variables
private int yearOfHiring;
private double annualSalary;
private int vacationDays;
private int unusedVacationDays;
//Constructor
public Employee(String firstName, String lastname,
String cellnumber, String birthdate,int yearOfHiring, double
annualSalary, int vacationDays, int unusedVacationDays) {
super(firstName, lastname,
cellnumber, birthdate);
this.yearOfHiring =
yearOfHiring;
this.annualSalary =
annualSalary;
this.vacationDays =
vacationDays;
this.unusedVacationDays =
unusedVacationDays;
}
@Override
public String toString() {
return "Employee
"+super.toString()+",yearOfHiring=" + yearOfHiring + ",
annualSalary=" + annualSalary + ", vacationDays="
+ vacationDays + ", unusedVacationDays=" +
unusedVacationDays+"\n" ;
}
//Getters & setters
public int getYearOfHiring() {
return yearOfHiring;
}
public void setYearOfHiring(int yearOfHiring) {
this.yearOfHiring =
yearOfHiring;
}
public double getAnnualSalary() {
return annualSalary;
}
public void setAnnualSalary(double annualSalary)
{
this.annualSalary =
annualSalary;
}
public int getVacationDays() {
return vacationDays;
}
public void setVacationDays(int vacationDays) {
this.vacationDays =
vacationDays;
}
public int getUnusedVacationDays() {
return unusedVacationDays;
}
public void setUnusedVacationDays(int
unusedVacationDays) {
this.unusedVacationDays =
unusedVacationDays;
}
}
Manager.java
package com.core.java;
import java.util.List;
//This class extends Employee class
public class Manager extends Employee{
//Instance variables
private double monthlyBonus;
private List<Employee> employeeList;
//Constructor
public Manager(String firstName, String lastname,
String cellnumber, String birthdate,int yearOfHiring, double
annualSalary, int vacationDays, int unusedVacationDays, double
monthlyBonus,
List<Employee> employeeList) {
super(firstName, lastname,
cellnumber, birthdate,yearOfHiring, annualSalary, vacationDays,
unusedVacationDays);
this.monthlyBonus =
monthlyBonus;
this.employeeList =
employeeList;
}
@Override
public String toString() {
return "Manager
"+super.toString()+" [monthlyBonus=" + monthlyBonus + ",
employeeList=" + employeeList + "]";
}
//Getters & setters
public double getMonthlyBonus() {
return monthlyBonus;
}
public void setMonthlyBonus(double monthlyBonus)
{
this.monthlyBonus =
monthlyBonus;
}
public List<Employee> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List<Employee>
employeeList) {
this.employeeList =
employeeList;
}
}
Company.java
package com.core.java;
import java.util.List;
//This class holds the company details
public class Company {
//Instance variables
private String companyName;
private int startingYear;
private List<Employee> employeeList;
private List<Manager> managerList;
//Constructor
public Company(String companyName, int startingYear,
List<Employee> employeeList, List<Manager> managerList)
{
super();
this.companyName =
companyName;
this.startingYear =
startingYear;
this.employeeList =
employeeList;
this.managerList =
managerList;
}
@Override
public String toString() {
return "Company [companyName=" +
companyName + ", startingYear=" + startingYear + ",
employeeList="
+ employeeList + ", managerList=" + managerList
+ "]";
}
//Getters and setters
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName =
companyName;
}
public int getStartingYear() {
return startingYear;
}
public void setStartingYear(int startingYear) {
this.startingYear =
startingYear;
}
public List<Employee> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List<Employee>
employeeList) {
this.employeeList =
employeeList;
}
public List<Manager> getManagerList() {
return managerList;
}
public void setManagerList(List<Manager>
managerList) {
this.managerList =
managerList;
}
}
MyCompany.java
package com.core.java;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
//This class operates on Person,employee,manager and Company
classes
public class MyCompany {
public static void main(String[] args) {
//Create a company object
Company company=new Company("ABC
pvt ltd", 2010, new ArrayList<>(), new
ArrayList<>());
//Create employees
Employee emp1=new
Employee("Tom","Cruise","100","Nov 14",2011, 50000, 90, 30);
Employee emp2=new
Employee("John","Abr","200","Jan 5",2010, 20000, 80, 25);
Employee emp3=new
Employee("AK","Einstien","50","Dec 20",2012, 30000, 70, 40);
//Create employee list
List<Employee> empList=new
ArrayList<>();
empList.add(emp1);
empList.add(emp2);
empList.add(emp3);
//Create Managers
Manager man1=new
Manager("Lisa","Ray","100","Jul 14",2012, 75000, 100,
50,10,empList);
empList=new
ArrayList<>();
empList.add(new
Employee("Sachin","Tend","100","Feb 1",2013, 80000, 90, 30));
empList.add(new
Employee("Ajay","J","50","Oct 19",2014, 25000, 90, 30));
Manager man2=new
Manager("Alice","John","70","Aug 14",2013, 80000, 120,
60,10,empList);
//Create manager list
List<Manager> manList=new
ArrayList<>();
manList.add(man1);
manList.add(man2);
company.setEmployeeList(empList);
company.setManagerList(manList);
//Sort by employee last name
Collections.sort(empList);
System.out.println("***********Displaying sorted list of
employees***********");
System.out.println(empList);
//Sort by manager last name
Collections.sort(manList);
System.out.println("***********Displaying sorted list of
managers***********");
System.out.println(manList);
}
}