Created
November 12, 2015 10:09
-
-
Save feupeu/4a43607d0755554c0a2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dk.cs.aau.sw705e15.model; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Computer extends Component { | |
private CentralProcessingUnit centralProcessingUnit; | |
private RandomAccessMemory randomAccessMemory; | |
private Storage primaryStorage; | |
private Storage secondaryStorage; | |
private GraphicalProcessingUnit graphicalProcessingUnit; | |
private NetworkInterface primaryNetworkInterface; | |
private NetworkInterface secondaryNetworkInterface; | |
private Display display; | |
private AudioInterface audioInterface; | |
private Battery battery; | |
public Computer(String name) { | |
super(name); | |
} | |
public List<Component> getComponents() { | |
return new ArrayList<Component>(){{ | |
add(getCentralProcessingUnit()); | |
add(getRandomAccessMemory()); | |
add(getPrimaryStorage()); | |
add(getSecondaryStorage()); | |
add(getGraphicalProcessingUnit()); | |
add(getPrimaryNetworkInterface()); | |
add(getSecondaryNetworkInterface()); | |
add(getDisplay()); | |
add(getAudioInterface()); | |
add(getBattery()); | |
}}; | |
} | |
public CentralProcessingUnit getCentralProcessingUnit() { | |
return centralProcessingUnit; | |
} | |
public Computer setCentralProcessingUnit(CentralProcessingUnit centralProcessingUnit) { | |
this.centralProcessingUnit = centralProcessingUnit; | |
return this; | |
} | |
public RandomAccessMemory getRandomAccessMemory() { | |
return randomAccessMemory; | |
} | |
public Computer setRandomAccessMemory(RandomAccessMemory randomAccessMemory) { | |
this.randomAccessMemory = randomAccessMemory; | |
return this; | |
} | |
public Storage getPrimaryStorage() { | |
return primaryStorage; | |
} | |
public Computer setPrimaryStorage(Storage primaryStorage) { | |
this.primaryStorage = primaryStorage; | |
return this; | |
} | |
public Storage getSecondaryStorage() { | |
return secondaryStorage; | |
} | |
public Computer setSecondaryStorage(Storage secondaryStorage) { | |
this.secondaryStorage = secondaryStorage; | |
return this; | |
} | |
public GraphicalProcessingUnit getGraphicalProcessingUnit() { | |
return graphicalProcessingUnit; | |
} | |
public Computer setGraphicalProcessingUnit(GraphicalProcessingUnit graphicalProcessingUnit) { | |
this.graphicalProcessingUnit = graphicalProcessingUnit; | |
return this; | |
} | |
public NetworkInterface getPrimaryNetworkInterface() { | |
return primaryNetworkInterface; | |
} | |
public Computer setPrimaryNetworkInterface(NetworkInterface primaryNetworkInterface) { | |
this.primaryNetworkInterface = primaryNetworkInterface; | |
return this; | |
} | |
public NetworkInterface getSecondaryNetworkInterface() { | |
return secondaryNetworkInterface; | |
} | |
public Computer setSecondaryNetworkInterface(NetworkInterface secondaryNetworkInterface) { | |
this.secondaryNetworkInterface = secondaryNetworkInterface; | |
return this; | |
} | |
public Display getDisplay() { | |
return display; | |
} | |
public Computer setDisplay(Display display) { | |
this.display = display; | |
return this; | |
} | |
public AudioInterface getAudioInterface() { | |
return audioInterface; | |
} | |
public Computer setAudioInterface(AudioInterface audioInterface) { | |
this.audioInterface = audioInterface; | |
return this; | |
} | |
public Battery getBattery() { | |
return battery; | |
} | |
public Computer setBattery(Battery battery) { | |
this.battery = battery; | |
return this; | |
} | |
@Override | |
public double difference(Object obj) { | |
// If obj is null, we are 100% different | |
if(obj == null) { | |
return 0d; | |
} | |
// Check if the obj is a computer | |
if(obj instanceof Computer) { | |
double score = 0d; | |
final Computer us = this; | |
final Computer them = (Computer) obj; | |
// Add the score for audio interfaces | |
final AudioInterface ourAudioInterface = us.getAudioInterface(); | |
final AudioInterface theirAudioInterface = them.getAudioInterface(); | |
if(ourAudioInterface == null && theirAudioInterface == null) score += 100d; // No component is set | |
else if(ourAudioInterface == null || theirAudioInterface == null) score += 0d; // One component not set | |
else score += ourAudioInterface.difference(theirAudioInterface); // Both components set | |
// Add the score for battery | |
final Battery ourBattery = us.getBattery(); | |
final Battery theirBattery = them.getBattery(); | |
if(ourBattery == null && theirBattery == null) score += 100d; // No component is set | |
else if(ourBattery == null || theirBattery == null) score += 0d; // One component not set | |
else score += ourBattery.difference(theirBattery); // Both components set | |
// Add the score for central processing unit | |
final CentralProcessingUnit ourCentralProcessingUnit = us.getCentralProcessingUnit(); | |
final CentralProcessingUnit theirCentralProcessingUnit = them.getCentralProcessingUnit(); | |
if(ourCentralProcessingUnit == null && theirCentralProcessingUnit == null) score += 100d; // No component is set | |
else if(ourCentralProcessingUnit == null || theirCentralProcessingUnit == null) score += 0d; // One component not set | |
else score += ourCentralProcessingUnit.difference(theirCentralProcessingUnit); // Both components set | |
// Add the score for display | |
final Display ourDisplay = us.getDisplay(); | |
final Display theirDisplay = them.getDisplay(); | |
if(ourDisplay == null && theirDisplay == null) score += 100d; // No component is set | |
else if(ourDisplay == null || theirDisplay == null) score += 0d; // One component not set | |
else score += ourDisplay.difference(theirDisplay); // Both components set | |
// Add the score for graphicalProcessingUnit | |
final GraphicalProcessingUnit ourGraphicalProcessingUnit = us.getGraphicalProcessingUnit(); | |
final GraphicalProcessingUnit theirGraphicalProcessingUnit = them.getGraphicalProcessingUnit(); | |
if(ourGraphicalProcessingUnit == null && theirGraphicalProcessingUnit == null) score += 100d; // No component is set | |
else if(ourGraphicalProcessingUnit == null || theirGraphicalProcessingUnit == null) score += 0d; // One component not set | |
else score += ourGraphicalProcessingUnit.difference(theirGraphicalProcessingUnit); // Both components set | |
// Add the score for our primary and secondary and their primary and secondary network interface | |
final NetworkInterface ourPrimaryNetworkInterface = us.getPrimaryNetworkInterface(); | |
final NetworkInterface ourSecondaryNetworkInterface = us.getSecondaryNetworkInterface(); | |
final NetworkInterface theirPrimaryNetworkInterface = them.getPrimaryNetworkInterface(); | |
final NetworkInterface theirSecondaryNetworkInterface = them.getSecondaryNetworkInterface(); | |
// No network interfaces at all | |
if(ourPrimaryNetworkInterface == null && ourSecondaryNetworkInterface == null | |
&& theirPrimaryNetworkInterface == null && theirSecondaryNetworkInterface == null) score += 100d; | |
// We have a primary, they have a primary | |
else if(ourPrimaryNetworkInterface != null && ourSecondaryNetworkInterface == null | |
&& theirPrimaryNetworkInterface != null && theirSecondaryNetworkInterface == null) score += ourPrimaryNetworkInterface.difference(theirPrimaryNetworkInterface); | |
// We have a primary, they have a secondary | |
else if(ourPrimaryNetworkInterface != null && ourSecondaryNetworkInterface == null | |
&& theirPrimaryNetworkInterface == null && theirSecondaryNetworkInterface != null) score += ourPrimaryNetworkInterface.difference(theirSecondaryNetworkInterface); | |
// We have a secondary, they have a primary | |
else if(ourPrimaryNetworkInterface == null && ourSecondaryNetworkInterface != null | |
&& theirPrimaryNetworkInterface != null && theirSecondaryNetworkInterface == null) score += ourSecondaryNetworkInterface.difference(theirPrimaryNetworkInterface); | |
// We have a secondary, they have a secondary | |
else if(ourPrimaryNetworkInterface == null && ourSecondaryNetworkInterface != null | |
&& theirPrimaryNetworkInterface == null && theirSecondaryNetworkInterface != null) score += ourSecondaryNetworkInterface.difference(theirSecondaryNetworkInterface); | |
else score += 0d; | |
// Add the score for random access memory | |
final RandomAccessMemory ourRandomAccessMemory = us.getRandomAccessMemory(); | |
final RandomAccessMemory theirRandomAccessMemory = them.getRandomAccessMemory(); | |
if(ourRandomAccessMemory == null && theirRandomAccessMemory == null) score += 100d; // No component is set | |
else if(ourRandomAccessMemory == null || theirRandomAccessMemory == null) score += 0d; // One component not set | |
else score += ourRandomAccessMemory.difference(theirRandomAccessMemory); // Both components set | |
// Add the score for our primary and secondary and their primary and secondary storage | |
final Storage ourPrimaryStorage = us.getPrimaryStorage(); | |
final Storage ourSecondaryStorage = us.getSecondaryStorage(); | |
final Storage theirPrimaryStorage = them.getPrimaryStorage(); | |
final Storage theirSecondaryStorage = them.getSecondaryStorage(); | |
// No storage at all | |
if(ourPrimaryStorage == null && ourSecondaryStorage == null | |
&& theirPrimaryStorage == null && theirSecondaryStorage == null) score += 100d; | |
// We have a primary, they have a primary | |
else if(ourPrimaryStorage != null && ourSecondaryStorage == null | |
&& theirPrimaryStorage != null && theirSecondaryStorage == null) score += ourPrimaryStorage.difference(theirPrimaryStorage); | |
// We have a primary, they have a secondary | |
else if(ourPrimaryStorage != null && ourSecondaryStorage == null | |
&& theirPrimaryStorage == null && theirSecondaryStorage != null) score += ourPrimaryStorage.difference(theirSecondaryStorage); | |
// We have a secondary, they have a primary | |
else if(ourPrimaryStorage == null && ourSecondaryStorage != null | |
&& theirPrimaryStorage != null && theirSecondaryStorage == null) score += ourSecondaryStorage.difference(theirPrimaryStorage); | |
// We have a secondary, they have a secondary | |
else if(ourPrimaryStorage == null && ourSecondaryStorage != null | |
&& theirPrimaryStorage == null && theirSecondaryStorage != null) score += ourSecondaryStorage.difference(theirSecondaryStorage); | |
else score += 0d; | |
return score; | |
} | |
// Check if the obj is a component (but not a computer) | |
else if (obj instanceof Component) { | |
final Computer c = new Computer(getName()); | |
// If obj is storage, compare it to both primary and secondary storage | |
if(obj instanceof Storage) { | |
final Computer ca = new Computer(getName()); | |
final double diff1 = this.difference(c.setPrimaryStorage((Storage) obj)); | |
final double diff2 = this.difference(ca.setSecondaryStorage((Storage) obj)); | |
return Math.max(diff1, diff2); | |
} | |
// If obj is a network interface, compare it to both wired and wireless network | |
if(obj instanceof NetworkInterface) { | |
final Computer ca = new Computer(getName()); | |
final double diff1 = this.difference(c.setPrimaryNetworkInterface((NetworkInterface) obj)); | |
final double diff2 = this.difference(ca.setSecondaryNetworkInterface((NetworkInterface) obj)); | |
return Math.max(diff1, diff2); | |
} | |
// Obj it neither storage nor network interface, so set the correct property and compare us to the new computer | |
if(obj instanceof AudioInterface) c.setAudioInterface((AudioInterface) obj); | |
else if(obj instanceof Battery) c.setBattery((Battery) obj); | |
else if(obj instanceof CentralProcessingUnit) c.setCentralProcessingUnit((CentralProcessingUnit) obj); | |
else if(obj instanceof Display) c.setDisplay((Display) obj); | |
else if(obj instanceof GraphicalProcessingUnit) c.setGraphicalProcessingUnit((GraphicalProcessingUnit) obj); | |
else if(obj instanceof RandomAccessMemory) c.setRandomAccessMemory((RandomAccessMemory) obj); | |
// Return the difference between the new computer and us | |
return this.difference(c) / 8; | |
} | |
// We cannot compare it | |
else { | |
return 0d; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment