diff --git a/src/main/java/millions/App.java b/src/main/java/millions/App.java index cb1b7d2..9a83f2c 100644 --- a/src/main/java/millions/App.java +++ b/src/main/java/millions/App.java @@ -1,5 +1,6 @@ package millions; + import java.math.BigDecimal; import java.util.logging.Level; import java.util.logging.Logger; @@ -11,6 +12,7 @@ import millions.controller.GameController; import millions.controller.fileIO.CSV.CSVStockFileWriter; import millions.controller.fileIO.InvalidFormatException; +import millions.controller.fileIO.UncheckedFileNotFoundException; import millions.view.GameView; import millions.view.StartView; @@ -41,6 +43,7 @@ public void start(Stage stage) { stage.setScene(gameScene); } catch (InvalidFormatException e) { logger.log(Level.WARNING, "InvalidFormatException: " + e.getMessage()); + Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Error"); alert.setHeaderText("Error with selected file"); @@ -48,6 +51,16 @@ public void start(Stage stage) { alert.showAndWait(); + } catch(UncheckedFileNotFoundException e) { + logger.log(Level.WARNING, "FileNotFoundException: " + e.getMessage()); + + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Error"); + alert.setHeaderText("Error with selected file"); + alert.setContentText(e.getMessage()); + + alert.showAndWait(); + } catch (RuntimeException ex) { logger.log(Level.SEVERE, ex.getMessage()); System.exit(0); diff --git a/src/main/java/millions/controller/GameController.java b/src/main/java/millions/controller/GameController.java index f50c4f7..a151510 100644 --- a/src/main/java/millions/controller/GameController.java +++ b/src/main/java/millions/controller/GameController.java @@ -1,5 +1,6 @@ package millions.controller; + import java.math.BigDecimal; import java.nio.file.Path; import java.util.Comparator; @@ -8,6 +9,7 @@ import millions.controller.fileIO.CSV.CSVFileHandler; import millions.controller.fileIO.InvalidFormatException; +import millions.controller.fileIO.UncheckedFileNotFoundException; import millions.model.Exchange; import millions.model.Player; import millions.model.Stock; @@ -32,6 +34,9 @@ public void startGame( } player = new Player(name, startingMoney); + + } catch(UncheckedFileNotFoundException e) { + throw new UncheckedFileNotFoundException(e.getMessage()); } catch(InvalidFormatException e) { throw new InvalidFormatException(e.getMessage()); } diff --git a/src/main/java/millions/controller/fileIO/CSV/CSVFileHandler.java b/src/main/java/millions/controller/fileIO/CSV/CSVFileHandler.java index bcd783d..fd2c3fb 100644 --- a/src/main/java/millions/controller/fileIO/CSV/CSVFileHandler.java +++ b/src/main/java/millions/controller/fileIO/CSV/CSVFileHandler.java @@ -2,8 +2,10 @@ import millions.controller.fileIO.InvalidFormatException; import millions.controller.fileIO.StockFileReader; +import millions.controller.fileIO.UncheckedFileNotFoundException; import millions.model.Stock; + import java.nio.file.Path; import java.util.List; @@ -24,6 +26,7 @@ public CSVFileHandler() { * @param filePath Path to stock file * @return list of stock object created from parsed file * @throws InvalidFormatException Throws an InvalidFormatException received from parser + * @throws UncheckedFileNotFoundException Upon Receiving a FilenotFoundException */ public List getStocksFromFile(Path filePath) { try { @@ -35,6 +38,8 @@ public List getStocksFromFile(Path filePath) { } catch (InvalidFormatException e) { throw new InvalidFormatException(e.getMessage()); + } catch (UncheckedFileNotFoundException e) { + throw new UncheckedFileNotFoundException(e.getMessage()); } } } diff --git a/src/main/java/millions/controller/fileIO/StockFileReader.java b/src/main/java/millions/controller/fileIO/StockFileReader.java index f873896..f7121cf 100644 --- a/src/main/java/millions/controller/fileIO/StockFileReader.java +++ b/src/main/java/millions/controller/fileIO/StockFileReader.java @@ -1,28 +1,29 @@ package millions.controller.fileIO; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.Reader; + + +import java.io.*; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Reads a file and returns its lines as a list of strings. */ public class StockFileReader { - + private static final Logger logger = Logger.getLogger(StockFileReader.class.getName()); public StockFileReader() {} /** * Reads the file found at the specified path * @param path Path to the desired file * @return List of each line in the file as a string + * @throws UncheckedFileNotFoundException Upon encountering a FileNotFoundException */ - public List readFile(Path path) { + public List readFile(Path path) { File file = new File(path.toString()); List lines = new ArrayList<>(); try (Reader reader = new FileReader(file); @@ -32,7 +33,12 @@ public List readFile(Path path) { lines.add(line); } } catch (IOException e) { - e.printStackTrace(); + if (e instanceof FileNotFoundException) { + throw new UncheckedFileNotFoundException("Couldn't find file at specified path"); + } + else { + logger.log(Level.SEVERE, "Encountered unexpected IOException: ", e.getMessage()); + } } return lines; } diff --git a/src/main/java/millions/controller/fileIO/UncheckedFileNotFoundException.java b/src/main/java/millions/controller/fileIO/UncheckedFileNotFoundException.java new file mode 100644 index 0000000..0b4b02d --- /dev/null +++ b/src/main/java/millions/controller/fileIO/UncheckedFileNotFoundException.java @@ -0,0 +1,7 @@ +package millions.controller.fileIO; + +public class UncheckedFileNotFoundException extends RuntimeException { + public UncheckedFileNotFoundException(String message) { + super(message); + } +}