Results 1 to 4 of 4
Thread: Refactoring
- 11-05-2009, 06:16 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
Refactoring
Hi, I am trying to learn about refactoring...I have been going through some tutorials, however I have an example below and would like some assistance in refactoring it step by step..Please help
import java.util.ArrayList;
import java.util.List;
public class Employees {
public static void main (String [] args) {
System.out.println ("Started program");
// 1. Create a list of tasks to complete
List<Task> tasks = new ArrayList<Task> ();
tasks.add (new Task("temperature", "Write a program to convert celsius to fahrenheit", 1));
tasks.add (new Task("graphical interface", "Write a graphical interface for our new application", 5));
tasks.add (new Task("bank application", "Write a bank management system", 10));
// 2. Create a list of Employees to complete them
List<Employee> employees = new ArrayList<Employee> ();
employees.add (new Employee("Fred", "PROGRAMMER"));
employees.add (new Employee("Hannah", "PROGRAMMER"));
employees.add (new Employee("Rahul", "PROGRAMMER"));
employees.add (new Employee("Ginger", "TESTER"));
employees.add (new Employee("John", "TESTER"));
// 3. Various tasks are performed by the Employees
employees.get(0).give(tasks.get(0));
employees.get(1).give(tasks.get(1));
employees.get(2).give(tasks.get(2));
employees.get(0).give(tasks.get(1));
employees.get(3).give(tasks.get(0));
// 4. Print out a summary of how much to pay each Employee
for (Employee e : employees) {
int wage = 0;
if (e.getRole () == "PROGRAMMER") {
wage = 15 * e.getD ();
} else if (e.getRole () == "TESTER") {
wage = 10 * e.getD ();
}
System.out.println ("Employee " + e.getName() + " is a " + e.getRole() + " has worked " +
e.getD() + " days and earns £" + wage);
}
}
}
class Employee {
private String _name;
private String _role;
private int _d;
public Employee (String name, String role) {
_name = name;
_role = role;
_d = 0;
}
public String getName () {
return _name;
}
public String getRole () {
return _role;
}
public int getD () {
return _d;
}
public void give (Task task) {
if (_role == "PROGRAMMER") {
// Programmer will do the task if it is not written yet
if (!task.isWritten ()) {
task.setWritten ();
_d += task.getDuration ();
}
} else if (_role == "TESTER") {
// Tester will do the task if it is written but not yet tested
if (task.isWritten () && !task.isTested()) {
task.setTested ();
_d += task.getDuration ();
}
}
}
}
// The Task class holds information relating to a programming task:
// its name and description, whether it has been written or tested,
// and how long it takes, in days (we assume it takes just as long to
// write as to test). A task can only be tested if it has been written.
class Task {
private String _name;
private String _description;
private int _duration;
private boolean _flag1;
private boolean _flag2;
public Task (String name, String description, int duration) {
_name = name;
_description = description;
_duration = duration;
_flag1 = false;
_flag2 = false;
}
public String getName () {
return _name;
}
public String getDescription () {
return _description;
}
public int getDuration () {
return _duration;
}
public boolean isWritten () {
return _flag1;
}
public boolean isTested () {
return _flag2;
}
// Set Task as having been written.
public void setWritten () {
_flag1 = true;
}
// Set Task as having been tested. A task can only be
// tested if it has been written.
public void setTested () {
if (_flag1) {
_flag2 = true;
}
}
}
- 11-06-2009, 03:19 AM #2
In what way do you want to refactor the code? Refactoring is a process - you take code and convert it to a "cleaner" or more usable form that performs an equivalent task.
CodesAway - codesaway.info
writing tools that make writing code a little easier
- 11-06-2009, 09:45 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
The first step would be to format the code, and stick it in the code tags...:)
- 11-06-2009, 11:18 AM #4


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks