Untitled
unknown
java
2 years ago
6.8 kB
2
Indexable
package a2_2001040028; import a2_2001040028.utils.*; /** * @overview PCs are devices which have basic information such as model, year and manufacturer, * and contain peripherals in it. * @attributes * model String * year Integer * manufacturer String * comps Set<String></String> * @object A typical PC is PC<m,y,n,c></m,y,n,c>, where model(m), year(y), manufacturer(n), comps(c). * mutable(model)=true /\ optional(model)=false /\ length(model)=20 /\ * mutable(year)=false /\ optional(year)=false /\ min(year)=1984 /\ * mutable(manufacture)=true /\ optional(manufacturer)=false /\ length(manufacturer)=15 /\ * mutable(comps)=true /\ optional(comps)=false * */ public class PC { //constants private static final int LENGTH_MODEL = 20; private static final int MIN_YEAR = 1984; private static final int LENGTH_MANUFACTURER = 15; @DomainConstraint(type="String", optional = false, length = LENGTH_MODEL ) private String model; @DomainConstraint(type="Integer", mutable = false, optional = false, min = MIN_YEAR) private int year; @DomainConstraint(type="String", mutable = false, optional = false, length = LENGTH_MANUFACTURER) private String manufacturer; @DomainConstraint(type="Set<String>", optional = false) private Set<String> comps; public PC(@AttrRef("String") String model, @AttrRef("Integer") int year, @AttrRef("String") String manufacturer, @AttrRef("Set<String>") Set<String> comps) throws NotPossibleException { //model is invalid if (!validateModel(model) ) throw new NotPossibleException("PC initialization: Invalid Model: " + model); //year is invalid if (!validateYear(year) ) throw new NotPossibleException("PC initialization: Invalid Year: " + year); //manufacturer is invalid if (!validateManufacturer(manufacturer)) throw new NotPossibleException("PC initialization: Invalid Manufacturer: " + manufacturer); //component is invalid if (!validateComps(comps)) throw new NotPossibleException("PC initialization: Invalid Components: " + comps); this.model = model; this.year = year; this.manufacturer = manufacturer; this.comps = comps; } /** * @effects * return model */ @DOpt(type = OptType.Observer) @AttrRef("model") public String getModel() { return model; } /** * @effects * return year */ @DOpt(type = OptType.Observer) @AttrRef("year") public int getYear() { return year; } /** * @effects * return manufacturer */ @DOpt(type = OptType.Observer) @AttrRef("manufacturer") public String getManufacturer() { return manufacturer; } /** * @effects * return comps */ @DOpt(type = OptType.Observer) @AttrRef("comps") public Set<String> getComps() { return comps; } /** * @effects * if model is valid * set this.model to model * return true * else * return false */ @DOpt(type = OptType.Mutator) @AttrRef("model") public boolean setModel(String model) { if (validateModel(model)) { this.model = model; return true; } return false; } /** * @modifies this.comps * @effects * if comp is already in this.comps * return false * else * insert comp to this.comps * return true */ @DOpt(type = OptType.MutatorAdd) @AttrRef("comps") public boolean insertComp(String comp) { for (String e: this.comps.getElements()) { if (e == comp) return false; } this.comps.insert(comp); return true; } /** * @modifies this.comps * @effects * if comp is in this.comps * remmove comp from this.comps * return true * else * return false */ @DOpt(type = OptType.MutatorRemove) @AttrRef("comps") public boolean removeComp(String comp){ for (String e: this.comps.getElements()) { if (e == comp) { this.comps.remove(comp); return false; } } return true; } /** * @effect * if model is valid * return true * else * return false */ private boolean validateModel(String model) { return (model != null && model.length() > 0 && model.length() <= LENGTH_MODEL); } /** * @effect * if year is valid * return true * else * return false */ private boolean validateYear(int year) { return year >= MIN_YEAR; } /** * @effect * if manufacturer is valid * return true * else * return false */ private boolean validateManufacturer(String manufacturer) { return (manufacturer != null && manufacturer.length() > 0 && manufacturer.length() <= LENGTH_MANUFACTURER); } /** * @effect * if manufacturer is valid * return true * else * return false */ private boolean validateComps(Set<String> comps) { return (comps != null && comps.size() >= 0); } @Override public String toString() { return "PC<" + getModel() +"," + getYear() +"," + getManufacturer() +"," + getComps().toString() +">"; } @Override public boolean equals (Object o) { if (!(o instanceof PC)) { return false; } if(!((PC) o).model.equals(this.model)) { return false; } if(((PC) o).year != this.year) { return false; } if(!((PC) o).manufacturer.equals(this.manufacturer)) { return false; } if(!((PC) o).comps.getElements().equals(this.comps.getElements())) { return false; } return true; } /** * @effects * if this satisfies abstract properties * return true * else * return false */ public boolean repOK() { return (validateModel(model) && validateYear(year) && validateManufacturer(manufacturer) && validateComps(comps)); } }
Editor is loading...