Commit a8b6b88c82acd3b1760fc9c490e5e077a87214d9

Authored by Thomas Røiri Aaslund
1 parent 8327b088

add buyingDate to vehicle

Showing 2 changed files with 43 additions and 6 deletions   Show diff stats
TestVehicle.java
... ... @@ -53,7 +53,8 @@ public class TestVehicle
53 53 System.out.println("3......................Find vehicle by name");
54 54 System.out.println("4..............Show data about all vehicles");
55 55 System.out.println("5.......Change direction of a given vehicle");
56   - System.out.println("6..............................Exit program");
  56 + System.out.println("6.......................Test clone() method");
  57 + System.out.println("7..............................Exit program");
57 58 System.out.println("...............................Your choice?");
58 59 System.out.print(":");
59 60 int choice = scan.nextInt();
... ... @@ -113,6 +114,27 @@ public class TestVehicle
113 114 }
114 115 break;
115 116 case 6:
  117 + {
  118 + System.out.println("Vehicle name: ");
  119 + Vehicle toClone = findVehicle(arr, scan.next());
  120 + if (toClone == null) {
  121 + System.out.println("Not found");
  122 + }
  123 + else {
  124 + Vehicle theClone = (Vehicle)toClone.clone();
  125 + arr.add(theClone);
  126 +
  127 + theClone.setBuyingDate(new GregorianCalendar());
  128 +
  129 + System.out.println("Original:");
  130 + System.out.println(toClone);
  131 + System.out.println();
  132 + System.out.println("Clone:");
  133 + System.out.println(theClone);
  134 + }
  135 + }
  136 + break;
  137 + case 7:
116 138 scan.close();
117 139 System.exit(0);
118 140 default:
... ...
Vehicle.java
... ... @@ -7,15 +7,16 @@ import java.text.*;
7 7  
8 8 public abstract class Vehicle implements Comparable<Vehicle>, Cloneable
9 9 {
10   - protected static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
  10 + protected static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd - HH:mm:ss");
11 11  
12 12 String color;
13 13 String name;
14 14 String serialNumber;
15 15 int model;
16 16 int price;
17   - int direction = 0;
18   - double speed = 0;
  17 + int direction;
  18 + double speed;
  19 + Calendar buyingDate;
19 20  
20 21 protected Scanner input = new Scanner(System.in);
21 22  
... ... @@ -23,6 +24,7 @@ public abstract class Vehicle implements Comparable&lt;Vehicle&gt;, Cloneable
23 24 public Object clone() throws CloneNotSupportedException
24 25 {
25 26 Vehicle v = (Vehicle)super.clone();
  27 + v.buyingDate = (Calendar)this.buyingDate.clone();
26 28 v.performDeepCopy(this);
27 29 return v;
28 30 }
... ... @@ -37,10 +39,17 @@ public abstract class Vehicle implements Comparable&lt;Vehicle&gt;, Cloneable
37 39  
38 40 public Vehicle()
39 41 {
  42 + this.direction = 0;
  43 + this.speed = 0;
  44 + this.buyingDate = new GregorianCalendar();
40 45 }
41 46  
42 47 public Vehicle(String name, String color, String serialNumber, int model, int price, int direction)
43 48 {
  49 + this.direction = 0;
  50 + this.speed = 0;
  51 + this.buyingDate = new GregorianCalendar();
  52 +
44 53 this.color = color;
45 54 this.name = name;
46 55 this.serialNumber = serialNumber;
... ... @@ -90,6 +99,9 @@ public abstract class Vehicle implements Comparable&lt;Vehicle&gt;, Cloneable
90 99 public double getSpeed() { return this.speed; }
91 100 public void setSpeed(double value) { this.speed = value; }
92 101  
  102 + public Calendar getBuyingDate() { return this.buyingDate; }
  103 + public void setBuyingDate(Calendar value) { this.buyingDate = value; }
  104 +
93 105 public abstract void turnLeft(int degrees);
94 106 public abstract void turnRight(int degrees);
95 107  
... ... @@ -110,8 +122,11 @@ public abstract class Vehicle implements Comparable&lt;Vehicle&gt;, Cloneable
110 122 @Override
111 123 public String toString()
112 124 {
113   - return String.format("%s name: %s, color: %s, serial: %s, model: %s, price: %s, direction: %s",
114   - this.getClass().getSimpleName(), name, color, serialNumber, model, price, direction);
  125 + return String.format(
  126 + "%s name: %s, color: %s, serial: %s, model: %s, price: %s, direction: %s, buyingDate: %s",
  127 + this.getClass().getSimpleName(), name, color, serialNumber, model, price, direction,
  128 + DATE_FORMAT.format(buyingDate.getTime())
  129 + );
115 130 }
116 131  
117 132 }
... ...