Skip to content

Commit

Permalink
Added UncheckedFileNotFoundException to handle delegation of FileNotF…
Browse files Browse the repository at this point in the history
…oundExceptions from reader to view. Added logging in case of unexpected IOExceptions
  • Loading branch information
Nikollai committed May 19, 2026
1 parent 2ea3eee commit fd86479
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/main/java/millions/App.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package millions;


import java.math.BigDecimal;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -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;

Expand Down Expand Up @@ -41,13 +43,24 @@ 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");
alert.setContentText(e.getMessage() + "\nPlease control the format of the selected file");

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);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/millions/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package millions.controller;


import java.math.BigDecimal;
import java.nio.file.Path;
import java.util.Comparator;
Expand All @@ -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;
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Stock> getStocksFromFile(Path filePath) {
try {
Expand All @@ -35,6 +38,8 @@ public List<Stock> getStocksFromFile(Path filePath) {

} catch (InvalidFormatException e) {
throw new InvalidFormatException(e.getMessage());
} catch (UncheckedFileNotFoundException e) {
throw new UncheckedFileNotFoundException(e.getMessage());
}
}
}
22 changes: 14 additions & 8 deletions src/main/java/millions/controller/fileIO/StockFileReader.java
Original file line number Diff line number Diff line change
@@ -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<String> readFile(Path path) {
public List<String> readFile(Path path) {
File file = new File(path.toString());
List<String> lines = new ArrayList<>();
try (Reader reader = new FileReader(file);
Expand All @@ -32,7 +33,12 @@ public List<String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package millions.controller.fileIO;

public class UncheckedFileNotFoundException extends RuntimeException {
public UncheckedFileNotFoundException(String message) {
super(message);
}
}

0 comments on commit fd86479

Please sign in to comment.