Tester
public class Tester {
public static void main(String[] args) {
int result = 1;
Mechanism mech = new Computer(1.0, 1.0, false);
result += mech.reportProblems();
mech = new Car("Daewoo", 2);
result += mech.reportProblems();
}
}
Car.java
public class Car implements Mechanism {
public static final int RIGHT_NUM_OF_WHEELS = 4;
public static final String PROBLEMATIC_BRAND = "Daewoo";
public static final String GOOD_BRAND = "BMW";
private String brand;
private int numOfWheels;
public Car(String brand, int numOfWheels) {
this.brand = brand;
this.numOfWheels = numOfWheels;
}
public String getBrand() {
return brand;
}
public int getNumOfWheels() {
return numOfWheels;
}
public void getFixed() {
if (numOfWheels != RIGHT_NUM_OF_WHEELS)
numOfWheels = RIGHT_NUM_OF_WHEELS;
if (brand.equalsIgnoreCase(PROBLEMATIC_BRAND))
brand = GOOD_BRAND;
}
public int reportProblems() {
int num = 0;
if (numOfWheels < RIGHT_NUM_OF_WHEELS)
num += (RIGHT_NUM_OF_WHEELS - numOfWheels);
if (numOfWheels > RIGHT_NUM_OF_WHEELS)
num += (numOfWheels - RIGHT_NUM_OF_WHEELS);
if (brand.equalsIgnoreCase(PROBLEMATIC_BRAND))
num += 1;
return num;
}
}
Computer.java
public class Computer implements Mechanism {
public static final double MIN_REQUIRED_SPEED = 2.0;
public static final double MIN_REQUIRED_CAPASITY = 1.0;
private double processorSpeed;
private double memoryCpacity;
private boolean networked;
public Computer(double processorSpeed, double memoryCpacity, boolean netwroked) {
this.processorSpeed = processorSpeed;
this.memoryCpacity = memoryCpacity;
this.networked = netwroked;
}
public double getProcessorSpeed() {
return processorSpeed;
}
public void changeProcessor(double newProcessorSpeed){
processorSpeed = newProcessorSpeed;
}
public double getMemoryCpacity() {
return memoryCpacity;
}
public void addMemory(double newMemory) {
memoryCpacity += newMemory;
}
public boolean isNetwroked() {
return networked;
}
public void connect() {
networked = true;
}
public void disconnect() {
networked = false;
}
public void getFixed() {
if (processorSpeed < MIN_REQUIRED_SPEED)
changeProcessor(MIN_REQUIRED_SPEED);
if (memoryCpacity < MIN_REQUIRED_CAPASITY)
addMemory(MIN_REQUIRED_CAPASITY-memoryCpacity);
connect();
}
public int reportProblems() {
int num = 0;
if (processorSpeed < MIN_REQUIRED_SPEED)
num += 1;
if (memoryCpacity < MIN_REQUIRED_CAPASITY)
num += 1;
if (!networked)
num += 1;
return num;
}
}
Mechanism.java
public interface Mechanism {
int reportProblems();
void getFixed();
}