Commit 7bc0946afae94ff39c82f7b57540d427bd2d91c4

Authored by Thomas Røiri Aaslund
1 parent edf22808

implement main functionality

Showing 4 changed files with 196 additions and 16 deletions   Show diff stats
Bicycle.java
... ... @@ -3,3 +3,47 @@ package vehicle;
3 3  
4 4 import java.lang.*;
5 5  
  6 +public class Bicycle extends Vehicle
  7 +{
  8 + int gears;
  9 +
  10 + public int getGears() { return this.gears; }
  11 + public void setGears(int value) { this.gears = value; }
  12 +
  13 + public Bicycle()
  14 + {
  15 + }
  16 +
  17 + public Bicycle(String name, String color, int price, int model, String serialNumber, int direction, int gears)
  18 + {
  19 + super(name, color, serialNumber, model, price, direction);
  20 + this.gears = gears;
  21 + }
  22 +
  23 + @Override
  24 + public void setAllFields()
  25 + {
  26 + super.setAllFields();
  27 +
  28 + System.out.print("Gears: ");
  29 + this.gears = input.nextInt();
  30 + }
  31 +
  32 + @Override
  33 + public void turnLeft(int degrees)
  34 + {
  35 + turnLeftInternal(degrees);
  36 + }
  37 +
  38 + @Override
  39 + public void turnRight(int degrees)
  40 + {
  41 + turnRightInternal(degrees);
  42 + }
  43 +
  44 + @Override
  45 + public String toString()
  46 + {
  47 + return String.format("%s, gears: %s", super.toString(), gears);
  48 + }
  49 +}
6 50 \ No newline at end of file
... ...
Car.java
... ... @@ -3,3 +3,47 @@ package vehicle;
3 3  
4 4 import java.lang.*;
5 5  
  6 +public class Car extends Vehicle
  7 +{
  8 + int power;
  9 +
  10 + public int getPower() { return this.power; }
  11 + public void setPower(int value) { this.power = value; }
  12 +
  13 + public Car()
  14 + {
  15 + }
  16 +
  17 + public Car(String name, String color, int price, int model, String serialNumber, int direction, int power)
  18 + {
  19 + super(name, color, serialNumber, model, price, direction);
  20 + this.power = power;
  21 + }
  22 +
  23 + @Override
  24 + public void setAllFields()
  25 + {
  26 + super.setAllFields();
  27 +
  28 + System.out.print("Power: ");
  29 + this.power = input.nextInt();
  30 + }
  31 +
  32 + @Override
  33 + public void turnLeft(int degrees)
  34 + {
  35 + turnLeftInternal(degrees);
  36 + }
  37 +
  38 + @Override
  39 + public void turnRight(int degrees)
  40 + {
  41 + turnRightInternal(degrees);
  42 + }
  43 +
  44 + @Override
  45 + public String toString()
  46 + {
  47 + return String.format("%s, power: %s", super.toString(), power);
  48 + }
  49 +}
6 50 \ No newline at end of file
... ...
TestVehicle.java
... ... @@ -25,17 +25,26 @@ public class TestVehicle
25 25 System.exit(1);
26 26 }
27 27 }
  28 +
  29 + static Vehicle findVehicle(ArrayList<Vehicle> arr, String name)
  30 + {
  31 + for (Vehicle v : arr)
  32 + {
  33 + if (v.getName().equals(name)) return v;
  34 + }
  35 +
  36 + return null;
  37 + }
28 38  
29 39 private void menuLoop() throws IOException, CloneNotSupportedException
30 40 {
31 41 Scanner scan = new Scanner(System.in);
32 42 ArrayList<Vehicle> arr = new ArrayList<Vehicle>();
33   - Vehicle vehicle;
34 43  
35   - //arr.add(new Car("Volvo","Black",85000,2010,"1010-11",163,0));
36   - //arr.add(new Bicycle("Diamant","yellow",4000,1993,"BC100",10,0));
37   - //arr.add(new Car("Ferrari Testarossa","red",1200000,1996,"A112",350,0));
38   - //arr.add(new Bicycle("DBS","pink",5000,1994,"42",10,0));
  44 + arr.add(new Car("Volvo","Black",85000,2010,"1010-11",163,0));
  45 + arr.add(new Bicycle("Diamant","yellow",4000,1993,"BC100",10,0));
  46 + arr.add(new Car("Ferrari Testarossa","red",1200000,1996,"A112",350,0));
  47 + arr.add(new Bicycle("DBS","pink",5000,1994,"42",10,0));
39 48  
40 49 while(true)
41 50 {
... ... @@ -45,25 +54,63 @@ public class TestVehicle
45 54 System.out.println("4..............Show data about all vehicles");
46 55 System.out.println("5.......Change direction of a given vehicle");
47 56 System.out.println("6..............................Exit program");
48   - System.out.println(".............................Your choice?");
  57 + System.out.println("...............................Your choice?");
  58 + System.out.print(":");
49 59 int choice = scan.nextInt();
50 60  
51 61 switch (choice)
52 62 {
53 63 case 1:
54 64 //legg til en ny bil
  65 + {
  66 + Car car = new Car();
  67 + car.setAllFields();
  68 + arr.add(car);
  69 + }
55 70 break;
56 71 case 2:
57 72 //legg til en ny sykkel
  73 + {
  74 + Bicycle bic = new Bicycle();
  75 + bic.setAllFields();
  76 + arr.add(bic);
  77 + }
58 78 break;
59 79 case 3:
60 80 //vis info om gitt kjøretøy
  81 + {
  82 + System.out.println("Vehicle name: ");
  83 + Vehicle v = findVehicle(arr, scan.next());
  84 + if (v != null) System.out.println(v);
  85 + else System.out.println("Not found");
  86 + }
61 87 break;
62 88 case 4:
63 89 //vis info om alle kjøretøy
  90 + for (Vehicle v2 : arr)
  91 + {
  92 + System.out.println(v2);
  93 + }
64 94 break;
65 95 case 5:
66 96 // Finn kjøretøy med gitt navn, sett ny retning
  97 + {
  98 + System.out.println("Vehicle name: ");
  99 + Vehicle v3 = findVehicle(arr, scan.next());
  100 + if (v3 == null) {
  101 + System.out.println("Not found");
  102 + }
  103 + else {
  104 + System.out.println("Direction [L/R]: ");
  105 + String dir = scan.next().toLowerCase().trim();
  106 + System.out.println("# of deg's: ");
  107 + int offs = scan.nextInt();
  108 +
  109 + if (dir == "l") v3.turnLeft(offs);
  110 + else if (dir == "r") v3.turnRight(offs);
  111 + else System.out.println("Invalid dir");
  112 + }
  113 + }
67 114 break;
68 115 case 6:
69 116 scan.close();
... ...
Vehicle.java
... ... @@ -2,6 +2,7 @@
2 2 package vehicle;
3 3  
4 4 import java.lang.*;
  5 +import java.util.*;
5 6  
6 7 public abstract class Vehicle
7 8 {
... ... @@ -10,13 +11,46 @@ public abstract class Vehicle
10 11 String serialNumber;
11 12 int model;
12 13 int price;
13   - int direction;
  14 + int direction = 0;
14 15 double speed = 0;
15 16  
  17 + protected Scanner input = new Scanner(System.in);
  18 +
  19 +
16 20 public Vehicle()
17 21 {
18 22 }
19 23  
  24 + public Vehicle(String name, String color, String serialNumber, int model, int price, int direction)
  25 + {
  26 + this.color = color;
  27 + this.name = name;
  28 + this.serialNumber = serialNumber;
  29 + this.model = model;
  30 + this.price = price;
  31 + this.direction = direction;
  32 + }
  33 +
  34 + public void setAllFields()
  35 + {
  36 + System.out.println(String.format("Input %s data: ", this.getClass().getSimpleName()));
  37 +
  38 + System.out.print("Name: ");
  39 + this.name = input.next();
  40 +
  41 + System.out.print("Color: ");
  42 + this.color = input.next();
  43 +
  44 + System.out.print("Price: ");
  45 + this.price = input.nextInt();
  46 +
  47 + System.out.print("Model: ");
  48 + this.model = input.nextInt();
  49 +
  50 + System.out.print("Serial #: ");
  51 + this.serialNumber = input.next();
  52 + }
  53 +
20 54 public String getColor() { return this.color; }
21 55 public void setColor(String value) { this.color = value; }
22 56  
... ... @@ -38,17 +72,28 @@ public abstract class Vehicle
38 72 public double getSpeed() { return this.speed; }
39 73 public void setSpeed(double value) { this.speed = value; }
40 74  
41   - public Vehicle(String name, String color, String serialNumber, int model, int price, int direction)
  75 + public abstract void turnLeft(int degrees);
  76 + public abstract void turnRight(int degrees);
  77 +
  78 + protected void turnLeftInternal(int degrees)
42 79 {
43   - this.color = color;
44   - this.name = name;
45   - this.serialNumber = serialNumber;
46   - this.model = model;
47   - this.price = price;
48   - this.direction = direction;
  80 + int dir = (this.getDirection() + degrees);
  81 + while (dir < 0) dir += 360;
  82 + this.setDirection(dir);
49 83 }
50 84  
51   - public abstract void turnLeft(int degrees);
52   - public abstract void turnRight(int degrees);
  85 + protected void turnRightInternal(int degrees)
  86 + {
  87 + int dir = (this.getDirection() - degrees);
  88 + while (dir >= 360) dir -= 360;
  89 + this.setDirection(dir);
  90 + }
  91 +
  92 + @Override
  93 + public String toString()
  94 + {
  95 + return String.format("%s name: %s, color: %s, serial: %s, model: %s, price: %s, direction: %s",
  96 + this.getClass().getSimpleName(), name, color, serialNumber, model, price, direction);
  97 + }
53 98  
54 99 }
... ...