Commit da7fa517ef6008c7adf6d7b88bf86947d84891e3

Authored by Thomas Røiri Aaslund
1 parent f4384a0b

part 4

Bicycle.java
... ... @@ -3,6 +3,7 @@ package vehicle;
3 3  
4 4 import java.lang.*;
5 5 import java.util.*;
  6 +import java.io.*;
6 7  
7 8 public class Bicycle extends Vehicle
8 9 {
... ... @@ -94,4 +95,21 @@ public class Bicycle extends Vehicle
94 95 DATE_FORMAT.format(productionDate.getTime())
95 96 );
96 97 }
  98 +
  99 + @Override
  100 + public void writeData(PrintWriter out) throws IOException
  101 + {
  102 + super.writeData(out);
  103 + writeValue(out, productionDate);
  104 + writeValue(out, gears);
  105 + }
  106 +
  107 + @Override
  108 + public void readData(Scanner in) throws IOException
  109 + {
  110 + super.readData(in);
  111 + productionDate = readCalendar(in);
  112 + gears = in.nextInt();
  113 + }
  114 +
97 115 }
98 116 \ No newline at end of file
... ...
Car.java
... ... @@ -3,7 +3,7 @@ package vehicle;
3 3  
4 4 import java.lang.*;
5 5 import java.util.*;
6   -
  6 +import java.io.*;
7 7  
8 8 public class Car extends Vehicle
9 9 {
... ... @@ -93,4 +93,22 @@ public class Car extends Vehicle
93 93 DATE_FORMAT.format(productionDate.getTime())
94 94 );
95 95 }
  96 +
  97 + @Override
  98 + public void writeData(PrintWriter out) throws IOException
  99 + {
  100 + super.writeData(out);
  101 + writeValue(out, productionDate);
  102 + writeValue(out, power);
  103 + }
  104 +
  105 + @Override
  106 + public void readData(Scanner in) throws IOException
  107 + {
  108 + super.readData(in);
  109 + productionDate = readCalendar(in);
  110 + power = in.nextInt();
  111 + }
  112 +
  113 +
96 114 }
97 115 \ No newline at end of file
... ...
Fileable.java 0 → 100644
  1 +
  2 +package vehicle;
  3 +
  4 +import java.io.IOException;
  5 +import java.io.PrintWriter;
  6 +import java.util.Scanner;
  7 +
  8 +public interface Fileable
  9 +{
  10 + void writeData(PrintWriter out) throws IOException;
  11 + void readData(Scanner in) throws IOException;
  12 +}
... ...
TestVehicle.java
... ... @@ -40,13 +40,29 @@ public class TestVehicle
40 40 {
41 41 Scanner scan = new Scanner(System.in);
42 42 ArrayList<Vehicle> arr = new ArrayList<Vehicle>();
  43 +
  44 + String wdirStr = new java.io.File( "." ).getCanonicalPath();
  45 + File wdir = new File(wdirStr);
  46 +
  47 + for (File file : wdir.listFiles(Vehicle.FILE_FILTER))
  48 + {
  49 + Vehicle loaded = Vehicle.fromFile(file);
  50 + arr.add(loaded);
  51 + System.out.println(String.format("Loaded vehicle: %s", loaded));
  52 + }
43 53  
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));
48   -
49   - while(true)
  54 + if (arr.size() == 0)
  55 + {
  56 + arr.add(new Car("Volvo","Black",85000,2010,"1010-11",163,0));
  57 + arr.add(new Bicycle("Diamant","yellow",4000,1993,"BC100",10,0));
  58 + arr.add(new Car("Ferrari Testarossa","red",1200000,1996,"A112",350,0));
  59 + arr.add(new Bicycle("DBS","pink",5000,1994,"42",10,0));
  60 +
  61 + System.out.println("Loaded default vehicle set");
  62 + }
  63 +
  64 + boolean doExit = false;
  65 + while(!doExit)
50 66 {
51 67 System.out.println("1...................................New car");
52 68 System.out.println("2...............................New bicycle");
... ... @@ -55,6 +71,8 @@ public class TestVehicle
55 71 System.out.println("5.......Change direction of a given vehicle");
56 72 System.out.println("6.......................Test clone() method");
57 73 System.out.println("7..................Test driveable interface");
  74 + System.out.println("8..............................Save vehicle");
  75 + System.out.println("9..............................Load vehicle");
58 76 System.out.println("0..............................Exit program");
59 77 System.out.println("...............................Your choice?");
60 78 System.out.print(":");
... ... @@ -114,7 +132,7 @@ public class TestVehicle
114 132 }
115 133 }
116 134 break;
117   - case 6:
  135 + case 6: // test clone
118 136 {
119 137 System.out.println("Vehicle name: ");
120 138 Vehicle toClone = findVehicle(arr, scan.next());
... ... @@ -134,7 +152,7 @@ public class TestVehicle
134 152 System.out.println(theClone);
135 153 }
136 154 }
137   - case 7:
  155 + case 7: // speed
138 156 {
139 157 System.out.println("Make 2 vehicles: ");
140 158 Vehicle nv1 = new Bicycle("BMX","color",4000,1993,"BC133",10,0);
... ... @@ -153,13 +171,64 @@ public class TestVehicle
153 171 nv2.stop();
154 172 }
155 173 break;
156   - case 0:
  174 + case 8: // save
  175 + {
  176 + System.out.print("Which vehicle?: ");
  177 + String name = scan.next();
  178 + Vehicle saveV = findVehicle(arr, name);
  179 + if (saveV == null)
  180 + {
  181 + System.out.println("Not found");
  182 + }
  183 + else
  184 + {
  185 + System.out.print("Save - File name (full path): ");
  186 + String saveFilename = scan.next();
  187 + File saveFile = new File(saveFilename);
  188 + saveV.toFile(saveFile);
  189 + }
  190 + }
  191 + break;
  192 + case 9: // load
  193 + {
  194 + System.out.print("Load - File name (full path): ");
  195 + String loadFilename = scan.next();
  196 + File loadFile = new File(loadFilename);
  197 +
  198 + try
  199 + {
  200 + Vehicle loadV = Vehicle.fromFile(loadFile);
  201 + System.out.println(loadV);
  202 + System.out.print("Add to retinue?: ");
  203 + String yesNo = scan.next().toLowerCase().trim();
  204 + if (yesNo.equals("y"))
  205 + {
  206 + arr.add(loadV);
  207 + System.out.println("Added.");
  208 + }
  209 + }
  210 + catch (Exception ex)
  211 + {
  212 + System.out.println(ex);
  213 + }
  214 + }
  215 + break;
  216 + case 0: // exit
157 217 scan.close();
158   - System.exit(0);
  218 + doExit = true;
  219 + break;
159 220 default:
160 221 System.out.println("Wrong input!");
161 222 }
162 223 }
  224 +
  225 + for (Vehicle veh : arr)
  226 + {
  227 + File saveAs =
  228 + java.nio.file.Paths.get(wdir.getPath(), veh.name + Vehicle.FILE_EXTENSION).toFile();
  229 + veh.toFile(saveAs);
  230 + System.out.println(String.format("Saved vehicle: %s", veh));
  231 + }
163 232 }
164 233 }
165 234  
... ...
Vehicle.java
... ... @@ -3,11 +3,23 @@ package vehicle;
3 3  
4 4 import java.lang.*;
5 5 import java.util.*;
  6 +import java.io.*;
6 7 import java.text.*;
7 8  
8   -public abstract class Vehicle implements Comparable<Vehicle>, Cloneable, Driveable
  9 +
  10 +public abstract class Vehicle implements Comparable<Vehicle>, Cloneable, Driveable, Fileable
9 11 {
10   - protected static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd - HH:mm:ss");
  12 + protected static final String CSV_DELIMETER = ",";
  13 +
  14 + protected static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
  15 +
  16 + public static final String FILE_EXTENSION = ".vehicle";
  17 +
  18 + public static final FilenameFilter FILE_FILTER = new FilenameFilter() {
  19 + public boolean accept(File directory, String fileName) {
  20 + return fileName.endsWith(FILE_EXTENSION);
  21 + }
  22 + };
11 23  
12 24 String color;
13 25 String name;
... ... @@ -20,6 +32,103 @@ public abstract class Vehicle implements Comparable&lt;Vehicle&gt;, Cloneable, Driveab
20 32  
21 33 protected Scanner input = new Scanner(System.in);
22 34  
  35 + public static Vehicle fromFile(File file)
  36 + throws FileNotFoundException, IOException
  37 + {
  38 + try ( Scanner in = new Scanner(file).useLocale(Locale.US) )
  39 + {
  40 + in.useDelimiter(CSV_DELIMETER);
  41 + String vehClass = in.next();
  42 + Class veh1 = Class.forName(vehClass);
  43 + Vehicle veh = (Vehicle)veh1.newInstance();
  44 + veh.readData(in);
  45 + return veh;
  46 + }
  47 + catch (ReflectiveOperationException ex)
  48 + // ClassNotFoundException+InstantiationException+IllegalAccessException
  49 + {
  50 + throw new IOException("Invalid vehicle class", ex);
  51 + }
  52 + }
  53 +
  54 + public void toFile(File file) throws IOException
  55 + {
  56 + try ( PrintWriter out = new PrintWriter(file) )
  57 + {
  58 + writeValue(out, this.getClass().getName());
  59 + this.writeData(out);
  60 + }
  61 + }
  62 +
  63 + protected static void writeValue(PrintWriter out, String strValue)
  64 + {
  65 + out.format(Locale.US, "%s%s", strValue, CSV_DELIMETER);
  66 + }
  67 +
  68 + protected static void writeValue(PrintWriter out, Calendar value)
  69 + {
  70 + String strValue = DATE_FORMAT.format(value.getTime());
  71 + writeValue(out, strValue);
  72 + }
  73 +
  74 + protected static void writeValue(PrintWriter out, int value)
  75 + {
  76 + String strValue = String.valueOf(value);
  77 + writeValue(out, strValue);
  78 + }
  79 +
  80 + protected static void writeValue(PrintWriter out, double value)
  81 + {
  82 + String strValue = String.valueOf(value);
  83 + writeValue(out, strValue);
  84 + }
  85 +
  86 + protected static Calendar readCalendar(Scanner in) throws IOException
  87 + {
  88 + Date date;
  89 + String dstr = in.next();
  90 +
  91 + try
  92 + {
  93 + date = DATE_FORMAT.parse(dstr);
  94 + }
  95 + catch (ParseException ex)
  96 + {
  97 + throw new IOException("Invalid date/time format: " + dstr, ex);
  98 + }
  99 +
  100 + Calendar cal = new GregorianCalendar();
  101 + cal.setTime(date);
  102 + return cal;
  103 + }
  104 +
  105 + @Override
  106 + public void writeData(PrintWriter out) throws IOException
  107 + {
  108 + writeValue(out, color);
  109 + writeValue(out, name);
  110 + writeValue(out, serialNumber);
  111 + writeValue(out, model);
  112 + writeValue(out, price);
  113 + writeValue(out, direction);
  114 + writeValue(out, speed);
  115 + writeValue(out, buyingDate);
  116 + }
  117 +
  118 + @Override
  119 + public void readData(Scanner in) throws IOException
  120 + {
  121 + this.color = in.next();
  122 + this.name = in.next();
  123 + this.serialNumber = in.next();
  124 +
  125 + this.model = in.nextInt();
  126 + this.price = in.nextInt();
  127 + this.direction = in.nextInt();
  128 + this.speed = in.nextDouble();
  129 + this.buyingDate = readCalendar(in);
  130 + }
  131 +
23 132 @Override
24 133 public Object clone() throws CloneNotSupportedException
25 134 {
... ...