Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void readCarFile(Stage primaryStage) {
- // Open a file dialog to let user choose a file.
- FileChooser fileChooser = new FileChooser();
- fileChooser.setTitle("Open file.");
- File f = fileChooser.showOpenDialog(primaryStage);
- // Will catch any errors if they occur.
- try {
- // Need both of these to properly read the file.
- FileReader fileReader = new FileReader(f);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- String currentLine;
- // Looping through each line of the file and splitting it into an array based on commas.
- while ((currentLine = bufferedReader.readLine()) != null) {
- String s = currentLine.replaceAll("\\s+", "");
- String[] splitLine = s.split(", ");
- // Extract information from each line.
- String numbPlate = splitLine[0];
- String model = splitLine[1];
- String vehicleS = splitLine[2];
- TypeOfVehicle typeOfVehicle = null;
- // Convert string of vehicles number plates to actual types of vehicle object.
- for (TypeOfVehicle veh : TypeOfVehicle.getVehicle()) {
- if (veh.getBodyType().equals(vehicleS)) {
- typeOfVehicle = veh;
- break;
- }
- }
- String colour = splitLine[3];
- String mileage = splitLine[4];
- String accHistory = splitLine[5];
- String tType = splitLine[6];
- Double price = null;
- Date arrivalDate = null;
- Date sellDate = null;
- // If an arrival date is set.
- if (splitLine[2].equals("small") || splitLine[2].equals("large") ) {
- price = Double.parseDouble(splitLine[8]);
- if (splitLine.length >= 10) {
- // Convert string to Date based on conditions.
- String arriveDateStr = splitLine[9];
- String[] arriveDateStrA = arriveDateStr.split("-");
- arrivalDate = new GregorianCalendar(Integer.parseInt(arriveDateStrA[0]), Integer.parseInt(arriveDateStrA[1]) - 1,
- Integer.parseInt(arriveDateStrA[2])).getTime();
- if (splitLine.length == 11) {
- String sellDateStr = splitLine[10];
- String[] sellDateStrs = sellDateStr.split("-");
- sellDate = new GregorianCalendar(Integer.parseInt(sellDateStrs[0]), Integer.parseInt(sellDateStrs[1]) - 1,
- Integer.parseInt(sellDateStrs[2])).getTime();
- }
- }
- }
- if (splitLine[2] == "") {
- price = Double.parseDouble(splitLine[7]);
- if (splitLine.length >= 9) {
- // Convert string to Date based on conditions.
- String arriveDateStr = splitLine[8];
- String [] arriveDateStrA = arriveDateStr.split("-");
- arrivalDate = new GregorianCalendar(Integer.parseInt(arriveDateStrA[0]), Integer.parseInt(arriveDateStrA[1]) - 1,
- Integer.parseInt(arriveDateStrA[2])).getTime();
- // If an sell date is set.
- if (splitLine.length == 10) {
- // Convert string to Date based on guidelines.
- String sellDateStr = splitLine[9];
- String[] sellDateStrs = sellDateStr.split("-");
- sellDate = new GregorianCalendar(Integer.parseInt(sellDateStrs[0]), Integer.parseInt(sellDateStrs[1]) - 1,
- Integer.parseInt(sellDateStrs[2])).getTime();
- }
- }
- } else {
- arrivalDate = new Date();
- }
- // If the typeOfVehicle supplied is in our defined list.
- if (typeOfVehicle != null) {
- // Convert from Date object to LocalDate.
- LocalDate arrivalDateNew = arrivalDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
- LocalDate sellDateNew = null;
- if (sellDate != null) {
- sellDateNew = sellDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
- }
- // Create new car object (which adds itself to Car.listOfCars)
- new Car(numbPlate, typeOfVehicle, price, model, colour, mileage, accHistory, tType, arrivalDateNew, sellDateNew);
- }
- }
- }
- // Instead of crashing, alert user of error.
- catch(IOException e){
- new Alert(Alert.AlertType.WARNING, "IOException occurred.").showAndWait();
- } catch(NullPointerException e){
- new Alert(Alert.AlertType.WARNING, "No file selected.").showAndWait();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement