Skip to content

Commit

Permalink
Merge pull request #58 from IDATT2003-gruppe06/jar-file-support
Browse files Browse the repository at this point in the history
Jar file support
  • Loading branch information
mohenoen authored May 26, 2026
2 parents 5675cd0 + a61978a commit 58caeee
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>millions.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/Main.java

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/java/millions/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package millions;

public class Main {
public static void main(String[] args) {
App.main(args);
}
}
8 changes: 4 additions & 4 deletions src/main/java/millions/controller/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public void startGame(
throw new IllegalArgumentException("Pre run weeks cannot be negative");
}
try {
CSVFileHandler fileHandler = new CSVFileHandler();
List<Stock> stocks = fileHandler.getStocksFromFile(stockFilePath);
CSVFileHandler csvFileHandler = new CSVFileHandler();
List<Stock> stocks = csvFileHandler.getStocksFromFile(stockFilePath);

exchange = new Exchange("Exchange", stocks);
for (int i = 0; i < preRunWeeks; i++) {
Expand All @@ -39,10 +39,10 @@ public void startGame(

player = new Player(name, startingMoney);

} catch (FileNotFoundException e) {
throw new UncheckedFileNotFoundException(e.getMessage());
} catch (InvalidFormatException e) {
throw new InvalidFormatException(e.getMessage());
} catch (FileNotFoundException e) {
throw new UncheckedFileNotFoundException(e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package millions.controller.fileIO.CSV;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class InternalResourceUtil {
public File getFileFromJar(InputStream resource) throws IOException {
if (resource == null) {
throw new IOException("Resource not found is null");
}
File tempFile = File.createTempFile("javafx_app_", ".tmp");
tempFile.deleteOnExit();
Files.copy(resource, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
return tempFile;
}
}
14 changes: 9 additions & 5 deletions src/main/java/millions/view/StartView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package millions.view;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -17,6 +19,7 @@
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import millions.controller.fileIO.CSV.InternalResourceUtil;

/** The initial game setup screen where the player enters their info. */
public class StartView extends VBox {
Expand Down Expand Up @@ -107,12 +110,13 @@ public StartView(Stage stage) {

private File loadDefaultStocksFile() {
try {
URL resource = Objects.requireNonNull(getClass().getResource("/data/default-stocks.csv"));
return Paths.get(resource.toURI()).toFile();
} catch (URISyntaxException e) {
logger.log(Level.SEVERE, "Error accessing default stocks file", e);
throw new IllegalStateException("Could not load default stocks file", e);
InputStream resource = Objects.requireNonNull(getClass().getResourceAsStream("/data/default-stocks.csv"));
InternalResourceUtil converter = new InternalResourceUtil();
return converter.getFileFromJar(resource);
} catch (IOException e) {
logger.log(Level.SEVERE, "error loading default file: ", e);
}
return null;
}

/** Enables/Disables start button */
Expand Down

0 comments on commit 58caeee

Please sign in to comment.