The setDetails methods in both the Laundry and Transportation classes currently rely on lengthy if-else statements to determine specific behaviors based on type. This conditional logic is a code smell and can be replaced with polymorphism, which would simplify the code and make it more maintainable.
Example of Current Code:
public class Laundry extends Service {
public void setDetails() {
Scanner in = new Scanner(System.in);
System.out.print("Enter type of wash (1/2/3): ");
type = in.nextInt();
System.out.print("Enter quantity of clothes: ");
quantity = in.nextInt();
if (type == 1) {
cost = 10 * quantity;
} else if (type == 2) {
cost = 20 * quantity;
} else if (type == 3) {
cost = 30 * quantity;
} else {
cost = 0;
}
}
}
public class Transportation extends Service {
public void setDetails() {
Scanner in = new Scanner(System.in);
System.out.print("Enter type of transportation (1/2/3): ");
type = in.nextInt();
System.out.print("Enter number of people: ");
quantity = in.nextInt();
if (type == 1) {
cost = 5 * quantity;
} else if (type == 2) {
cost = 15 * quantity;
} else if (type == 3) {
cost = 25 * quantity;
} else {
cost = 0;
}
}
}
Proposed Refactoring:
The if-else logic should be replaced with a polymorphic structure. This can be achieved by introducing subclasses that represent each type of laundry or transportation. Each subclass will implement its own calculateCost method, eliminating the need for conditional checks.
Refactored Code Example:
public abstract class Service {
protected int quantity;
protected int cost;
public abstract void setDetails();
public abstract void calculateCost();
}
public class Laundry extends Service {
private int type;
@Override
public void setDetails() {
Scanner in = new Scanner(System.in);
System.out.print("Enter type of wash (1/2/3): ");
type = in.nextInt();
System.out.print("Enter quantity of clothes: ");
quantity = in.nextInt();
calculateCost();
}
@Override
public void calculateCost() {
switch (type) {
case 1 -> cost = 10 * quantity;
case 2 -> cost = 20 * quantity;
case 3 -> cost = 30 * quantity;
default -> cost = 0;
}
}
}
public class Transportation extends Service {
private int type;
@Override
public void setDetails() {
Scanner in = new Scanner(System.in);
System.out.print("Enter type of transportation (1/2/3): ");
type = in.nextInt();
System.out.print("Enter number of people: ");
quantity = in.nextInt();
calculateCost();
}
@Override
public void calculateCost() {
switch (type) {
case 1 -> cost = 5 * quantity;
case 2 -> cost = 15 * quantity;
case 3 -> cost = 25 * quantity;
default -> cost = 0;
}
}
}
Benefits of Refactoring:
- Simplifies Code: Polymorphism reduces the need for complex conditional logic, making the code easier to read and understand.
- Improves Maintainability: Each class handles its own logic, making future changes more localized and less prone to errors.
- Encourages Extensibility: Adding new types of services would simply involve creating new subclasses rather than modifying existing code.
Let me know if you need assistance with implementing this refactoring or have any further questions!
The
setDetailsmethods in both theLaundryandTransportationclasses currently rely on lengthyif-elsestatements to determine specific behaviors based ontype. This conditional logic is a code smell and can be replaced with polymorphism, which would simplify the code and make it more maintainable.Example of Current Code:
Proposed Refactoring:
The
if-elselogic should be replaced with a polymorphic structure. This can be achieved by introducing subclasses that represent eachtypeof laundry or transportation. Each subclass will implement its owncalculateCostmethod, eliminating the need for conditional checks.Refactored Code Example:
Benefits of Refactoring:
Let me know if you need assistance with implementing this refactoring or have any further questions!