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
@@ -3,3 +3,47 @@ package vehicle; @@ -3,3 +3,47 @@ package vehicle;
3 3
4 import java.lang.*; 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 \ No newline at end of file 50 \ No newline at end of file
@@ -3,3 +3,47 @@ package vehicle; @@ -3,3 +3,47 @@ package vehicle;
3 3
4 import java.lang.*; 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 \ No newline at end of file 50 \ No newline at end of file
@@ -25,17 +25,26 @@ public class TestVehicle @@ -25,17 +25,26 @@ public class TestVehicle
25 System.exit(1); 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 private void menuLoop() throws IOException, CloneNotSupportedException 39 private void menuLoop() throws IOException, CloneNotSupportedException
30 { 40 {
31 Scanner scan = new Scanner(System.in); 41 Scanner scan = new Scanner(System.in);
32 ArrayList<Vehicle> arr = new ArrayList<Vehicle>(); 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 while(true) 49 while(true)
41 { 50 {
@@ -45,25 +54,63 @@ public class TestVehicle @@ -45,25 +54,63 @@ public class TestVehicle
45 System.out.println("4..............Show data about all vehicles"); 54 System.out.println("4..............Show data about all vehicles");
46 System.out.println("5.......Change direction of a given vehicle"); 55 System.out.println("5.......Change direction of a given vehicle");
47 System.out.println("6..............................Exit program"); 56 System.out.println("6..............................Exit program");
48 - System.out.println(".............................Your choice?"); 57 + System.out.println("...............................Your choice?");
  58 + System.out.print(":");
49 int choice = scan.nextInt(); 59 int choice = scan.nextInt();
50 60
51 switch (choice) 61 switch (choice)
52 { 62 {
53 case 1: 63 case 1:
54 //legg til en ny bil 64 //legg til en ny bil
  65 + {
  66 + Car car = new Car();
  67 + car.setAllFields();
  68 + arr.add(car);
  69 + }
55 break; 70 break;
56 case 2: 71 case 2:
57 //legg til en ny sykkel 72 //legg til en ny sykkel
  73 + {
  74 + Bicycle bic = new Bicycle();
  75 + bic.setAllFields();
  76 + arr.add(bic);
  77 + }
58 break; 78 break;
59 case 3: 79 case 3:
60 //vis info om gitt kjøretøy 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 break; 87 break;
62 case 4: 88 case 4:
63 //vis info om alle kjøretøy 89 //vis info om alle kjøretøy
  90 + for (Vehicle v2 : arr)
  91 + {
  92 + System.out.println(v2);
  93 + }
64 break; 94 break;
65 case 5: 95 case 5:
66 // Finn kjøretøy med gitt navn, sett ny retning 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 break; 114 break;
68 case 6: 115 case 6:
69 scan.close(); 116 scan.close();
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 package vehicle; 2 package vehicle;
3 3
4 import java.lang.*; 4 import java.lang.*;
  5 +import java.util.*;
5 6
6 public abstract class Vehicle 7 public abstract class Vehicle
7 { 8 {
@@ -10,13 +11,46 @@ public abstract class Vehicle @@ -10,13 +11,46 @@ public abstract class Vehicle
10 String serialNumber; 11 String serialNumber;
11 int model; 12 int model;
12 int price; 13 int price;
13 - int direction; 14 + int direction = 0;
14 double speed = 0; 15 double speed = 0;
15 16
  17 + protected Scanner input = new Scanner(System.in);
  18 +
  19 +
16 public Vehicle() 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 public String getColor() { return this.color; } 54 public String getColor() { return this.color; }
21 public void setColor(String value) { this.color = value; } 55 public void setColor(String value) { this.color = value; }
22 56
@@ -38,17 +72,28 @@ public abstract class Vehicle @@ -38,17 +72,28 @@ public abstract class Vehicle
38 public double getSpeed() { return this.speed; } 72 public double getSpeed() { return this.speed; }
39 public void setSpeed(double value) { this.speed = value; } 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 }