Advertisement
Guest User

readCarFile

a guest
Apr 21st, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. private void readCarFile(Stage primaryStage) {
  2. // Open a file dialog to let user choose a file.
  3. FileChooser fileChooser = new FileChooser();
  4. fileChooser.setTitle("Open file.");
  5. File f = fileChooser.showOpenDialog(primaryStage);
  6. // Will catch any errors if they occur.
  7. try {
  8. // Need both of these to properly read the file.
  9. FileReader fileReader = new FileReader(f);
  10. BufferedReader bufferedReader = new BufferedReader(fileReader);
  11.  
  12. String currentLine;
  13. // Looping through each line of the file and splitting it into an array based on commas.
  14. while ((currentLine = bufferedReader.readLine()) != null) {
  15. String s = currentLine.replaceAll("\\s+", "");
  16. String[] splitLine = s.split(", ");
  17. // Extract information from each line.
  18. String numbPlate = splitLine[0];
  19. String model = splitLine[1];
  20. String vehicleS = splitLine[2];
  21.  
  22. TypeOfVehicle typeOfVehicle = null;
  23.  
  24. // Convert string of vehicles number plates to actual types of vehicle object.
  25. for (TypeOfVehicle veh : TypeOfVehicle.getVehicle()) {
  26. if (veh.getBodyType().equals(vehicleS)) {
  27. typeOfVehicle = veh;
  28. break;
  29. }
  30. }
  31. String colour = splitLine[3];
  32. String mileage = splitLine[4];
  33. String accHistory = splitLine[5];
  34. String tType = splitLine[6];
  35. Double price = null;
  36. Date arrivalDate = null;
  37. Date sellDate = null;
  38. // If an arrival date is set.
  39. if (splitLine[2].equals("small") || splitLine[2].equals("large") ) {
  40. price = Double.parseDouble(splitLine[8]);
  41. if (splitLine.length >= 10) {
  42. // Convert string to Date based on conditions.
  43. String arriveDateStr = splitLine[9];
  44. String[] arriveDateStrA = arriveDateStr.split("-");
  45. arrivalDate = new GregorianCalendar(Integer.parseInt(arriveDateStrA[0]), Integer.parseInt(arriveDateStrA[1]) - 1,
  46. Integer.parseInt(arriveDateStrA[2])).getTime();
  47. if (splitLine.length == 11) {
  48. String sellDateStr = splitLine[10];
  49. String[] sellDateStrs = sellDateStr.split("-");
  50. sellDate = new GregorianCalendar(Integer.parseInt(sellDateStrs[0]), Integer.parseInt(sellDateStrs[1]) - 1,
  51. Integer.parseInt(sellDateStrs[2])).getTime();
  52. }
  53. }
  54. }
  55. if (splitLine[2] == "") {
  56. price = Double.parseDouble(splitLine[7]);
  57. if (splitLine.length >= 9) {
  58. // Convert string to Date based on conditions.
  59. String arriveDateStr = splitLine[8];
  60. String [] arriveDateStrA = arriveDateStr.split("-");
  61. arrivalDate = new GregorianCalendar(Integer.parseInt(arriveDateStrA[0]), Integer.parseInt(arriveDateStrA[1]) - 1,
  62. Integer.parseInt(arriveDateStrA[2])).getTime();
  63. // If an sell date is set.
  64. if (splitLine.length == 10) {
  65. // Convert string to Date based on guidelines.
  66. String sellDateStr = splitLine[9];
  67. String[] sellDateStrs = sellDateStr.split("-");
  68. sellDate = new GregorianCalendar(Integer.parseInt(sellDateStrs[0]), Integer.parseInt(sellDateStrs[1]) - 1,
  69. Integer.parseInt(sellDateStrs[2])).getTime();
  70. }
  71. }
  72.  
  73. } else {
  74. arrivalDate = new Date();
  75. }
  76.  
  77.  
  78. // If the typeOfVehicle supplied is in our defined list.
  79. if (typeOfVehicle != null) {
  80.  
  81. // Convert from Date object to LocalDate.
  82. LocalDate arrivalDateNew = arrivalDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  83. LocalDate sellDateNew = null;
  84. if (sellDate != null) {
  85. sellDateNew = sellDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  86. }
  87. // Create new car object (which adds itself to Car.listOfCars)
  88. new Car(numbPlate, typeOfVehicle, price, model, colour, mileage, accHistory, tType, arrivalDateNew, sellDateNew);
  89. }
  90. }
  91.  
  92.  
  93.  
  94. }
  95.  
  96.  
  97. // Instead of crashing, alert user of error.
  98. catch(IOException e){
  99. new Alert(Alert.AlertType.WARNING, "IOException occurred.").showAndWait();
  100. } catch(NullPointerException e){
  101. new Alert(Alert.AlertType.WARNING, "No file selected.").showAndWait();
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement