Commit 197c3c5175b902a6f81bdfcc163833d7163bd814

Authored by Thomas Røiri Aaslund
1 parent a8b6b88c

add driveable

Showing 4 changed files with 84 additions and 1 deletions   Show diff stats
Bicycle.java
... ... @@ -23,6 +23,8 @@ public class Bicycle extends Vehicle
23 23 b.productionDate = (Calendar)this.productionDate.clone();
24 24 }
25 25  
  26 +
  27 +
26 28 public Bicycle()
27 29 {
28 30 this.productionDate = new GregorianCalendar();
... ... @@ -58,6 +60,31 @@ public class Bicycle extends Vehicle
58 60 }
59 61  
60 62 @Override
  63 + public void accelerate(int speedFactor)
  64 + {
  65 + double spd = getSpeed();
  66 + if (spd == 0)
  67 + {
  68 + setSpeed(spd + 0.3 * speedFactor);
  69 + }
  70 + else
  71 + {
  72 + setSpeed(spd + 0.5 * speedFactor);
  73 + }
  74 +
  75 + if (getSpeed() > MAX_SPEED_BIKE) setSpeed(MAX_SPEED_BIKE);
  76 +
  77 + printSpeedChangeMessage("accellerated");
  78 + }
  79 +
  80 + @Override
  81 + public void brakes(int speedFactor)
  82 + {
  83 + setSpeed(getSpeed() / (0.5 * speedFactor));
  84 + printSpeedChangeMessage("decelerated");
  85 + }
  86 +
  87 + @Override
61 88 public String toString()
62 89 {
63 90 return String.format(
... ...
Car.java
... ... @@ -59,6 +59,31 @@ public class Car extends Vehicle
59 59 }
60 60  
61 61 @Override
  62 + public void accelerate(int speedFactor)
  63 + {
  64 + double spd = getSpeed();
  65 + if (spd == 0)
  66 + {
  67 + setSpeed(spd + 0.5 * speedFactor);
  68 + }
  69 + else
  70 + {
  71 + setSpeed(spd + speedFactor);
  72 + }
  73 +
  74 + if (getSpeed() > MAX_SPEED_CAR) setSpeed(MAX_SPEED_CAR);
  75 +
  76 + printSpeedChangeMessage("accellerated");
  77 + }
  78 +
  79 + @Override
  80 + public void brakes(int speedFactor)
  81 + {
  82 + setSpeed(getSpeed() / (speedFactor));
  83 + printSpeedChangeMessage("decelerated");
  84 + }
  85 +
  86 + @Override
62 87 public String toString()
63 88 {
64 89 return String.format(
... ...
Driveable.java 0 → 100644
  1 +
  2 +package vehicle;
  3 +
  4 +public interface Driveable
  5 +{
  6 + double MAX_SPEED_CAR = 250.00;
  7 + double MAX_SPEED_BIKE = 100.00;
  8 +
  9 + void accelerate(int speedFactor);
  10 +
  11 + void brakes(int speedFactor);
  12 +
  13 + void stop();
  14 +}
... ...
Vehicle.java
... ... @@ -5,7 +5,7 @@ import java.lang.*;
5 5 import java.util.*;
6 6 import java.text.*;
7 7  
8   -public abstract class Vehicle implements Comparable<Vehicle>, Cloneable
  8 +public abstract class Vehicle implements Comparable<Vehicle>, Cloneable, Driveable
9 9 {
10 10 protected static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd - HH:mm:ss");
11 11  
... ... @@ -31,12 +31,29 @@ public abstract class Vehicle implements Comparable&lt;Vehicle&gt;, Cloneable
31 31  
32 32 protected abstract void performDeepCopy(Vehicle other) throws CloneNotSupportedException;
33 33  
  34 + @Override
  35 + public void stop()
  36 + {
  37 + this.setSpeed(0);
  38 + System.out.println(String.format("%s %s is stopped", this.getClass().getSimpleName(), this.getName()));
  39 + }
  40 +
34 41 @Override
35 42 public int compareTo(Vehicle rhs)
36 43 {
37 44 return (int)Math.signum(this.price - rhs.price);
38 45 }
39 46  
  47 + protected void printSpeedChangeMessage(String desc)
  48 + {
  49 + System.out.println(String.format(String.format("%s %s: %s to %s",
  50 + this.getClass().getSimpleName(),
  51 + this.getName()),
  52 + desc,
  53 + getSpeed()
  54 + ));
  55 + }
  56 +
40 57 public Vehicle()
41 58 {
42 59 this.direction = 0;
... ...