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
@@ -15,6 +15,13 @@ public class Bicycle extends Vehicle @@ -15,6 +15,13 @@ public class Bicycle extends Vehicle
15 public Calendar getProductionDate() { return this.productionDate; } 15 public Calendar getProductionDate() { return this.productionDate; }
16 public void setProductionDate(Calendar value) { this.productionDate = value; } 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 public Bicycle() 26 public Bicycle()
20 { 27 {
@@ -16,6 +16,14 @@ public class Car extends Vehicle @@ -16,6 +16,14 @@ public class Car extends Vehicle
16 public Calendar getProductionDate() { return this.productionDate; } 16 public Calendar getProductionDate() { return this.productionDate; }
17 public void setProductionDate(Calendar value) { this.productionDate = value; } 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 public Car() 27 public Car()
20 { 28 {
21 this.productionDate = new GregorianCalendar(); 29 this.productionDate = new GregorianCalendar();
@@ -20,11 +20,15 @@ public abstract class Vehicle implements Comparable<Vehicle>, Cloneable @@ -20,11 +20,15 @@ public abstract class Vehicle implements Comparable<Vehicle>, Cloneable
20 protected Scanner input = new Scanner(System.in); 20 protected Scanner input = new Scanner(System.in);
21 21
22 @Override 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 @Override 32 @Override
29 public int compareTo(Vehicle rhs) 33 public int compareTo(Vehicle rhs)
30 { 34 {