Commit 8327b0883017fbead30c721f18d39284bad29aee

Authored by Thomas Røiri Aaslund
1 parent 43d60fab

add compareTo and clone to veh

Showing 3 changed files with 21 additions and 2 deletions   Show diff stats
Bicycle.java
... ... @@ -15,6 +15,13 @@ public class Bicycle extends Vehicle
15 15 public Calendar getProductionDate() { return this.productionDate; }
16 16 public void setProductionDate(Calendar value) { this.productionDate = value; }
17 17  
  18 + @Override
  19 + protected void performDeepCopy(Vehicle other) throws CloneNotSupportedException
  20 + {
  21 + if (!(other instanceof Bicycle)) throw new CloneNotSupportedException("wrong inst type");
  22 + Bicycle b = (Bicycle)other;
  23 + b.productionDate = (Calendar)this.productionDate.clone();
  24 + }
18 25  
19 26 public Bicycle()
20 27 {
... ...
Car.java
... ... @@ -16,6 +16,14 @@ public class Car extends Vehicle
16 16 public Calendar getProductionDate() { return this.productionDate; }
17 17 public void setProductionDate(Calendar value) { this.productionDate = value; }
18 18  
  19 + @Override
  20 + protected void performDeepCopy(Vehicle other) throws CloneNotSupportedException
  21 + {
  22 + if (!(other instanceof Car)) throw new CloneNotSupportedException("wrong inst type");
  23 + Car b = (Car)other;
  24 + b.productionDate = (Calendar)this.productionDate.clone();
  25 + }
  26 +
19 27 public Car()
20 28 {
21 29 this.productionDate = new GregorianCalendar();
... ...
Vehicle.java
... ... @@ -20,11 +20,15 @@ public abstract class Vehicle implements Comparable<Vehicle>, Cloneable
20 20 protected Scanner input = new Scanner(System.in);
21 21  
22 22 @Override
23   - public Object clone()
  23 + public Object clone() throws CloneNotSupportedException
24 24 {
25   -
  25 + Vehicle v = (Vehicle)super.clone();
  26 + v.performDeepCopy(this);
  27 + return v;
26 28 }
27 29  
  30 + protected abstract void performDeepCopy(Vehicle other) throws CloneNotSupportedException;
  31 +
28 32 @Override
29 33 public int compareTo(Vehicle rhs)
30 34 {
... ...