Commit b129193c authored by Stefan Michel's avatar Stefan Michel

final Commit

parent e3792f46
<?xml version="1.0" encoding="UTF-8"?>
<component inherit-compiler-output="true" inheritJdk="true">
<output-test url="file://$MODULE_DIR$/out/test/StefanMichelSimulator"/>
<component inheritJdk="true">
<exclude-output/>
<contentEntry url="file://$MODULE_DIR$"/>
</component>
# Ignore everything in this directory
*
# Except this file
!.gitignore
\ No newline at end of file
......@@ -4,7 +4,6 @@ import controller.HamsterController;
import controller.IOController;
import controller.TerrainController;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.stage.Stage;
import model.DatabaseManager;
......@@ -28,7 +27,7 @@ public class Main extends Application {
public static final List<String> activePrograms = new ArrayList<>();
private IOController ioController;
private ThreadManager threadManager;
private NetworkManager networkManager = NetworkManager.getInstance();
private final NetworkManager networkManager = NetworkManager.getInstance();
public static void main(String[] args) {
launch(args);
......
......@@ -53,9 +53,8 @@ public class DataBaseEntryDialogBox {
okayButton.setDisable(true);
filename.textProperty().addListener((observable, oldValue, newValue) -> {
okayButton.setDisable(newValue.trim().isEmpty());
});
filename.textProperty().addListener((observable, oldValue, newValue) ->
okayButton.setDisable(newValue.trim().isEmpty()));
dialog.getDialogPane().setContent(grid);
......
......@@ -53,9 +53,8 @@ public class GetEntryByTagDialogBox {
okayButton.setDisable(true);
filename.textProperty().addListener((observable, oldValue, newValue) -> {
okayButton.setDisable(newValue.trim().isEmpty() || newValue.contains(" "));
});
filename.textProperty().addListener((observable, oldValue, newValue) ->
okayButton.setDisable(newValue.trim().isEmpty() || newValue.contains(" ")));
dialog.getDialogPane().setContent(grid);
......
......@@ -70,6 +70,7 @@ public class IOController {
javac.run(null, null, err, "./programme/" + name + ".java") == 0;
if (!success) {
new Alert(Alert.AlertType.ERROR, err.toString()).showAndWait();
mainViewCreationFX.setFooterText("Kompilieren fehlgeschlagen!");
} else {
setUpHamster(name);
mainViewCreationFX.setFooterText("Kompilieren erfolgreich");
......
......@@ -10,12 +10,12 @@ import java.util.ResourceBundle;
public class LanguageManager {
private static LanguageManager ourInstance = new LanguageManager();
private static final LanguageManager ourInstance = new LanguageManager();
private ResourceBundle bundle;
private Properties properties;
private final Properties properties;
private Locale locale;
private String propertiesPath = "resources/simulator.properties";
private String bundlePath = "language";
private final String propertiesPath = "resources/simulator.properties";
private final String bundlePath = "language";
public static LanguageManager getInstance() {
return ourInstance;
......
......@@ -32,7 +32,7 @@ public class PropertiesManager {
// pruefen, ob propertiesPath korrekt
String role = prop.getProperty("role");
if (!(role.equals("student") || role.equals("tutor"))) {
Files.delete(Paths.get("resources/simulator.propertiesPath"));
Files.delete(Paths.get("resources" + propertiesPath));
setUpDefaultProps();
}
}
......
......@@ -68,7 +68,7 @@ public class SetTerrainNameDialogBox {
return null;
});
Optional<String> result = null;
Optional<String> result;
try {
result = dialog.showAndWait();
return result.get();
......
......@@ -209,7 +209,7 @@ public class TerrainController {
public void loadTerrainFromXMLDoc(Document document) {
Element docElement = document.getDocumentElement();
//TODO: setHamsterCorns hinzufuegen
terrain.setName(getValueByTag("name", docElement));
terrain.setHamsterName(getValueByTag("hamstername", docElement));
terrain.setMaxCols(Integer.parseInt(getValueByTag("maxcols", docElement)));
......
......@@ -15,26 +15,21 @@ import java.util.Map;
*/
public class DerbyDatabase {
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String dbName = "StefanHamsterDB";
String connectionURL = "jdbc:derby:" + dbName + ";create=true";
private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private String dbName = "StefanHamsterDB";
private String connectionURL = "jdbc:derby:" + dbName + ";create=true";
String createExampleTable = "CREATE TABLE Example (e_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), code VARCHAR(1000), terrain VARCHAR(8000))";
String createTagTable = "CREATE TABLE Tags (t_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), name VARCHAR (64))";
String createExampleTagsTable = "CREATE TABLE ExampleTags (e_id INT NOT NULL , t_id INT NOT NULL)";
private String createExampleTable = "CREATE TABLE Example (e_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), code VARCHAR(1000), terrain VARCHAR(8000))";
private String createTagTable = "CREATE TABLE Tags (t_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), name VARCHAR (64))";
private String createExampleTagsTable = "CREATE TABLE ExampleTags (e_id INT NOT NULL , t_id INT NOT NULL)";
String insertExample = "INSERT INTO Example(CODE, TERRAIN) VALUES (?, ?)";
String insertTag = "INSERT INTO Tags(NAME) VALUES (?)";
String insertExampleTag = "INSERT INTO ExampleTags VALUES (?,?)";
private String insertExample = "INSERT INTO Example(CODE, TERRAIN) VALUES (?, ?)";
private String insertTag = "INSERT INTO Tags(NAME) VALUES (?)";
private String insertExampleTag = "INSERT INTO ExampleTags VALUES (?,?)";
String queryExampleByTag = "SELECT DISTINCT e. e_id " +
"FROM EXAMPLE e" +
" INNER JOIN EXAMPLETAGS et ON et.e_id = e.e_id" +
" INNER JOIN TAGS t ON t.t_id = et.t_id " +
"WHERE t.name IN (?)";
String queryExampleId = "SELECT E_ID FROM EXAMPLE WHERE CODE = ? AND TERRAIN = ?";
String queryTag = "SELECT T_ID FROM tags WHERE name = ?";
String queryExampleById = "SELECT CODE, TERRAIN FROM EXAMPLE WHERE E_ID = ?";
private String queryExampleId = "SELECT E_ID FROM EXAMPLE WHERE CODE = ? AND TERRAIN = ?";
private String queryTag = "SELECT T_ID FROM tags WHERE name = ?";
private String queryExampleById = "SELECT CODE, TERRAIN FROM EXAMPLE WHERE E_ID = ?";
public DerbyDatabase() {
......@@ -79,7 +74,7 @@ public class DerbyDatabase {
preparedStatement.execute();
} catch (SQLException e) {
System.err.println("example Table already exists");
}
try {
conn.prepareStatement(createTagTable).execute();
......@@ -88,7 +83,6 @@ public class DerbyDatabase {
preparedStatement.execute();
} catch (SQLException e) {
System.err.println("tag Table already exists");
}
try {
conn.prepareStatement(createExampleTagsTable).execute();
......@@ -103,7 +97,7 @@ public class DerbyDatabase {
preparedStatement.execute();
} catch (SQLException e) {
System.err.println("exampleTags Table already exists");
}
}
......@@ -135,6 +129,11 @@ public class DerbyDatabase {
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(connectionURL);
String queryExampleByTag = "SELECT DISTINCT e. e_id " +
"FROM EXAMPLE e" +
" INNER JOIN EXAMPLETAGS et ON et.e_id = e.e_id" +
" INNER JOIN TAGS t ON t.t_id = et.t_id " +
"WHERE t.name IN (?)";
PreparedStatement preparedStatement = conn.prepareStatement(queryExampleByTag);
preparedStatement.setString(1, tag);
ResultSet resultSet = preparedStatement.executeQuery();
......
......@@ -19,7 +19,7 @@ public class Terrain extends Observable implements Serializable {
public final static int EAST = 1;
public final static int SOUTH = 2;
public final static int WEST = 3;
final static int WALL = -1;
private final static int WALL = -1;
public final static int MAXCORNS = 9;
private final static int MINTILES = 5;
private String name = "DefaultTerrain";
......@@ -27,15 +27,15 @@ public class Terrain extends Observable implements Serializable {
private transient Hamster hamster;
private String hamsterName = "DefaultHamster";
int maxCols = 10;
int maxRows = 10;
private int maxCols = 10;
private int maxRows = 10;
int hamsterRow = 0;
int hamsterCol = 0;
int hamsterDirection = SOUTH;
int hamsterCorns = 0;
private int hamsterRow = 0;
private int hamsterCol = 0;
private int hamsterDirection = SOUTH;
private int hamsterCorns = 0;
int[][] map;
private int[][] map;
private double speed = 450;
......@@ -67,7 +67,7 @@ public class Terrain extends Observable implements Serializable {
}
//gibt die anzahl an koernern zurueck die auf dem gleichen feld liegen wie der Hamster
public int foundCorn() {
public synchronized int foundCorn() {
if (map[hamsterCol][hamsterRow] > 0) {
return map[hamsterCol][hamsterRow];
} else
......@@ -75,7 +75,7 @@ public class Terrain extends Observable implements Serializable {
}
// fuegt dem Feld ein korn hinzu
public void addCorn(int col, int row) {
public synchronized void addCorn(int col, int row) {
if (this.map[col][row] < MAXCORNS && this.map[col][row] >= 0) {
this.map[col][row]++;
setChanged();
......@@ -86,7 +86,7 @@ public class Terrain extends Observable implements Serializable {
}
// entfernt ein Feld
public void deleteTile(int col, int row) {
public synchronized void deleteTile(int col, int row) {
if (row > 0 || col > 0 || row > maxCols || col > maxRows) {
this.map[col][row] = 0;
setChanged();
......@@ -94,7 +94,7 @@ public class Terrain extends Observable implements Serializable {
}
}
public void addWall(int col, int row) {
public synchronized void addWall(int col, int row) {
if (!(col == hamsterCol && row == hamsterRow)) {
this.map[col][row] = WALL;
setChanged();
......@@ -102,7 +102,7 @@ public class Terrain extends Observable implements Serializable {
}
}
public void putDown() {
public synchronized void putDown() {
if (this.hamsterCorns > 0) {
this.hamsterCorns--;
this.map[this.hamsterCol][this.hamsterRow]++;
......@@ -111,9 +111,52 @@ public class Terrain extends Observable implements Serializable {
notifyObservers();
}
public synchronized void turnLeft() {
if (this.hamsterDirection != Terrain.NORTH) {
this.hamsterDirection = this.hamsterDirection - 1;
} else {
this.hamsterDirection = Terrain.WEST;
}
setChanged();
notifyObservers();
}
public synchronized void pickUp() {
if (this.foundCorn() > 0) {
this.hamsterCorns++;
this.map[this.hamsterCol][this.hamsterRow]--;
} else throw new NoCornException();
setChanged();
notifyObservers();
}
public void notifyObserversWithThreads() {
super.notifyObservers();
if (hamster != null && hamster.isAlive()) {
try {
Thread.sleep((long) (-1 * speed + 1000));
} catch (InterruptedException e) {
}
}
}
//prueft, ob sich an dieser position eine wand befindet
public synchronized boolean checkWall(int col, int row) {
return this.map[col][row] == WALL;
}
public synchronized int getCornAmount(int col, int row) {
return this.map[col][row];
}
public synchronized int getMaxRows() {
return maxRows;
}
// veraendert die anzahl der zeilen und prueft ob der hamster
// sich im verkleinerten bereich befindet
public void setMaxRows(int maxRows) {
public synchronized void setMaxRows(int maxRows) {
if (maxRows > MINTILES) {
if (maxRows < this.hamsterRow) {
if (this.map[0][0] < 0) {
......@@ -124,7 +167,7 @@ public class Terrain extends Observable implements Serializable {
}
int[][] newMap = new int[maxCols][maxRows];
//TODO: alternative zur if else finden
if (this.maxRows < maxRows) {
for (int i = 0; i < map.length; i++) {
System.arraycopy(map[i], 0, newMap[i], 0, map[i].length);
......@@ -141,8 +184,16 @@ public class Terrain extends Observable implements Serializable {
}
}
// ----------------------
// TRIVIAL GETTER SETTER
// ----------------------
public synchronized int getMaxCols() {
return maxCols;
}
// analog zu setMaxRows
public void setMaxCols(int maxCols) {
public synchronized void setMaxCols(int maxCols) {
if (maxCols > MINTILES) {
if (maxCols < this.hamsterCol) {
if (this.map[0][0] < 0) {
......@@ -169,137 +220,80 @@ public class Terrain extends Observable implements Serializable {
}
}
public void turnLeft() {
if (this.hamsterDirection != Terrain.NORTH) {
this.hamsterDirection = this.hamsterDirection - 1;
} else {
this.hamsterDirection = Terrain.WEST;
}
setChanged();
notifyObservers();
}
public void pickUp() {
if (this.foundCorn() > 0) {
this.hamsterCorns++;
this.map[this.hamsterCol][this.hamsterRow]--;
} else throw new NoCornException();
setChanged();
notifyObservers();
}
public void notifyObserversWithThreads() {
super.notifyObservers();
if (hamster != null && hamster.isAlive()) {
try {
Thread.sleep((long) (-1 * speed + 1000));
} catch (InterruptedException e) {
try {
hamster.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
//prueft, ob sich an dieser position eine wand befindet
public boolean checkWall(int col, int row) {
return this.map[col][row] == WALL;
}
public int getCornAmount(int col, int row) {
return this.map[col][row];
}
// ----------------------
// TRIVIAL GETTER SETTER
// ----------------------
public int getMaxRows() {
return maxRows;
}
public int getMaxCols() {
return maxCols;
}
public int getHamsterCorns() {
public synchronized int getHamsterCorns() {
return this.hamsterCorns;
}
public int getHamsterRow() {
public synchronized int getHamsterRow() {
return hamsterRow;
}
public int getHamsterCol() {
public synchronized int getHamsterCol() {
return hamsterCol;
}
public int getHamsterDirection() {
public synchronized int getHamsterDirection() {
return this.hamsterDirection;
}
public String getHamsterName() {
public synchronized void setHamsterDirection(int direction) throws IllegalArgumentException {
if (direction >= NORTH && direction <= WEST) {
this.hamsterDirection = direction;
setChanged();
notifyObservers(this);
} else {
throw new IllegalArgumentException("ungültige Richtung");
}
}
public synchronized String getHamsterName() {
return hamsterName;
}
public void setHamsterName(String hamsterName) {
public synchronized void setHamsterName(String hamsterName) {
this.hamsterName = hamsterName;
}
public Hamster getHamster() {
public synchronized Hamster getHamster() {
return hamster;
}
public boolean isRunningThread() {
return hamster.isRunning();
}
public void setChanged() {
super.setChanged();
public synchronized void setHamster(Hamster hamster) {
this.hamster = hamster;
hamster.setTerrain(this);
}
// TODO: evtl loeschen
public void setHamsterDirection(int direction) throws IllegalArgumentException {
if (direction >= NORTH && direction <= WEST) {
this.hamsterDirection = direction;
setChanged();
notifyObservers(this);
} else {
throw new IllegalArgumentException("ungültige Richtung");
}
public synchronized boolean isRunningThread() {
return hamster.isRunning();
}
public void setHamster(Hamster hamster) {
this.hamster = hamster;
hamster.setTerrain(this);
public synchronized void setChanged() {
super.setChanged();
}
public void setSpeed(double speed) {
public synchronized void setSpeed(double speed) {
this.speed = speed;
}
public String getName() {
public synchronized String getName() {
return name;
}
public void setName(String name) {
public synchronized void setName(String name) {
this.name = name;
}
private int[][] getMap() {
private synchronized int[][] getMap() {
return map;
}
public void setMap(int[][] map) {
public synchronized void setMap(int[][] map) {
this.map = map;
setChanged();
notifyObservers();
}
public void setUpTerrain(Terrain newTerrain) {
public synchronized void setUpTerrain(Terrain newTerrain) {
this.setHamsterName(newTerrain.getHamsterName());
this.setName(newTerrain.getName());
this.setMaxCols(newTerrain.getMaxCols());
......
......@@ -31,7 +31,7 @@ public class NetworkManager {
return ourInstance;
}
// TODO: vielleicht ueber State Pattern ?
private void setUpNetwork(String role, String host, int port) {
if (role.equals("tutor")) {
setUpTutor(port);
......
package network;
import java.rmi.ConnectException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
......
......@@ -398,8 +398,9 @@ public class MainViewCreationFX extends BorderPane implements Observer {
private Menu buildHamsterMenu() {
//TODO: koerner anzeigen im alert
MenuItem koernerImMaul = new MenuItem(langManager.getString("corns"));
koernerImMaul.setOnAction(event -> new Alert(Alert.AlertType.INFORMATION, "Corns: " + String.valueOf(terrain.getHamsterCorns())).show());
MenuItem linksUm = new MenuItem(langManager.getString("left"));
linksUm.setAccelerator(new KeyCodeCombination(KeyCode.L, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN));
......@@ -445,7 +446,7 @@ public class MainViewCreationFX extends BorderPane implements Observer {
public void openFileChooser() {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new ExtensionFilter("Java Files", "*.java"));
fileChooser.setInitialDirectory(new File("terrains/"));
fileChooser.setInitialDirectory(new File("programme/"));
File file = fileChooser.showOpenDialog(this.getScene().getWindow());
if (file != null) {
// StringBuilder schneller, da nicht synchronized
......@@ -540,6 +541,8 @@ public class MainViewCreationFX extends BorderPane implements Observer {
gib.setTooltip(new Tooltip(langManager.getString("give")));
// hamsterbutton funktionen werden gesetzt
kornImMaul.setOnAction(event ->
new Alert(Alert.AlertType.INFORMATION, "Corn: " + String.valueOf(terrain.getHamsterCorns())).show());
linksUm.setOnAction(event -> this.hamsterController.turnLeft());
vor.setOnAction(event -> this.hamsterController.forward());
nimm.setOnAction(event -> this.hamsterController.pickUp());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment