CMPUT 301

Software Engineering

Practice w23am3q3


Adapter Design Pattern Refactoring (UML class diagram)

The code represents the university HR system. There are employees paid hourly (support staff, secretaries, assistants etc.) and students. Some students can be employed as teaching assistants. In this case, their hourly rate depends on their year. The Report class prepares reports on employees and teaching assistants. 

There is a problem with the Report class as it knows too much about students! 

Restructure this code with the Object Adapter design pattern and provide a UML class diagram depicting the structure and operations one would expect after refactoring this code to use an object adapter.

In your solution, the Report class must have one (and only one) method. Your solution must significantly reduce code duplication and enhance reuse. 

Draw the resulting UML class diagram of the refactored code. Provide the basic abstractions, attributes, methods, relationships, multiplicities, and navigabilities as appropriate.** 

class Employee {
    private String name;
    private String jobTitle;
    private double hourlyRate;
    public String getName() { return name; }
    public String getJobTitle() { return jobTitle; }
    public double getHourlyRate() { return hourlyRate; }
}

class Student {
    private String name;
    private String major;
    private int year;
    public String getName() { return name; }
    public String getMajor() { return major; }
    public int getYear() { return year; }
}

class Report {
    // Prints report for an employee (hourly staff)
    public void employeeReport(Employee e) {
        System.out.println("Name: " + e.getName());
        System.out.println("Job Title: " + e.getJobTitle());
        System.out.println("Hourly Rate: " + e.getHourlyRate());
    }

    // Prints report for a teaching assistant
    public void studentReport(Student s) {
        System.out.println("Name: " + s.getName());
        System.out.println("Job Title: Teaching Assistant");
        double hourlyRate = 0;
        if (s.getYear() == 1) {
            hourlyRate = 17;
        } else if (s.getYear() <= 3) {
            hourlyRate = 18;
        } else {
            hourlyRate = 19;
        }
        System.out.println("Hourly Rate: " + hourlyRate);
    }
}