Question

In: Computer Science

Alice is going to create a notebook including the profile of all her friends. For each...

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.

Solutions

Expert Solution

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);

              
   }

}


Related Solutions

A feature of each Facebook user’s profile is the number of “friends”, which indicates the user’s...
A feature of each Facebook user’s profile is the number of “friends”, which indicates the user’s social network connectedness. Among college students on Facebook, the average number of Facebook friends has been estimated to be around 250 from a 2008 study. An experiment was run to examine the relationship between the number of Facebook friends and the user’s perceived social attractiveness. A group of undergraduates were randomly assigned to observe one of three Facebook profiles. Everything about the profile was...
Create a Web Page Using HTML, CSS, JS, jQuery • Create a web profile, including any...
Create a Web Page Using HTML, CSS, JS, jQuery • Create a web profile, including any of the following: • Your hobbies, likes/dislikes, career aspirations, dream job, favorite animals/ pets, favorite superhero, etc. • You do not have to share personal information that you are uncomfortable with sharing. Follow these guidelines when creating your page: • Include at least one heading (h1, h2, etc.) in your web page. • Provide a quote from a book, movie, etc. • Include at...
IN SWIFT LANGUAGE In this lab we are going to create a game that uses all...
IN SWIFT LANGUAGE In this lab we are going to create a game that uses all the different types of control flow operators that exist in SWIFT LANGUAGE. The game will be a choose your own adventure game. The user will have to answer a series of questions and this will change the path of a story. This must be done by using if/else statements that are nested within other if/else statements. Your story must have four (4) different possible...
Emily and her friends sell newspaper ads for the Bronc to supplement their income each year....
Emily and her friends sell newspaper ads for the Bronc to supplement their income each year. From the data below, determine which person is the most productive (ads per hour) and which person produces the greatest ROI ($profit per $labor) under each of the possible scenarios A and B? Emily (Freshman) Yunjue (Junior) Tianqi (Sophomore) Lauren (Senior) # ads sold 100 50 200 35 # hours spent 40 15 85 10 total $revenue $400 $200 $700 $150 Productivity: (ads per...
Stephanie is going to contribute $300 on the first of each month, starting today, to her...
Stephanie is going to contribute $300 on the first of each month, starting today, to her retirement account. Her employer will provide a 50 percent match. In other words, her employer will contribute 50 percent of the amount Stephanie saves. If both Stephanie and her employer continue to do this and she can earn a monthly rate of 0.9 percent, how much will she have in her retirement account 35 years from now?
What effects does drug addiction have on SOCIETY, including family and friends?( 8pts) Explain each.
What effects does drug addiction have on SOCIETY, including family and friends?( 8pts) Explain each.
Anny, Betty, Cindy and Dolly were all friends at school. Subsequently each of the 6 pairs...
Anny, Betty, Cindy and Dolly were all friends at school. Subsequently each of the 6 pairs meet up; at each of the 6 meetings the pair involved quarrel with some fixed probability p, or become firm friends with probability q=1-p. Quarrels take place independently of each other. In future, if any of the 4 hears a rumour, then she tells it to her firm friends only. If Anny hears a rumour, what is the probability that (a) Dolly hears it...
Create an actual organizational flow chart for a fitness center including all the positions. After creating...
Create an actual organizational flow chart for a fitness center including all the positions. After creating your chart, briefly describe the role of each.
Dr. Joy wants to leave each of her 6 grandchildren $25,000 at her death. All of...
Dr. Joy wants to leave each of her 6 grandchildren $25,000 at her death. All of her grandchildren are under 16 years old. What are Dr. Joy’s options for leaving these bequests to her grandchildren under his Will?
Explain all the parties on a promissory note, including an indorser, and the liability each signing...
Explain all the parties on a promissory note, including an indorser, and the liability each signing party has on the note
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT