Skip to content

Commit

Permalink
fun with switch
Browse files Browse the repository at this point in the history
  • Loading branch information
JayPi4c committed Jun 18, 2024
1 parent 5faa925 commit 1ec1b33
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
16 changes: 7 additions & 9 deletions src/main/java/com/JayPi4c/RobbiSimulator/model/Territory.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public synchronized Item getItem(int x, int y) {
* @return the item that has been removed from the tile
*/
public Item removeItem(int x, int y) {
Item i = null;
Item i;
synchronized (this) {
Tile t = tiles[normalizeCoordinate(x, numberOfColumns)][normalizeCoordinate(y, numberOfRows)];
i = t.pickItem();
Expand Down Expand Up @@ -581,14 +581,12 @@ public boolean fromXML(InputStream stream) {
*/
private Item getItemFromParser(XMLStreamReader parser) {
String type = parser.getAttributeValue(null, "type");
if (type.equals("Nut")) {
return new Nut();
} else if (type.equals("Accu")) {
return new Accu();
} else if (type.equals("Screw")) {
return new Screw();
}
return null;
return switch (type) {
case "Nut" -> new Nut();
case "Accu" -> new Accu();
case "Screw" -> new Screw();
default -> null;
};
}

/**
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/com/JayPi4c/RobbiSimulator/model/TerritoryState.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ public TerritoryState(int numberOfColumns, int numberOfRows, Tile[][] tiles, Rob
this.tiles = new Tile[tiles.length][tiles[0].length];
for (int i = 0; i < tiles.length; i++) {
for (int j = 0; j < tiles[i].length; j++) {
Tile t = tiles[i][j];
Tile newTile;
if (t instanceof Hollow) {
newTile = new Hollow();
} else if (t instanceof PileOfScrap) {
newTile = new PileOfScrap();
} else if (t instanceof Stockpile stockpile) {
newTile = new Stockpile();
for (Item item : stockpile.getAllItems())
newTile.setItem(item);
} else {
newTile = new Tile();
newTile.setItem(t.getItem());
}
Tile newTile = switch (tiles[i][j]) {
case Hollow ignored:
yield new Hollow();
case PileOfScrap ignored:
yield new PileOfScrap();
case Stockpile stockpile:
Stockpile s = new Stockpile();
for (Item item : stockpile.getAllItems())
s.setItem(item);
yield s;
case Tile t:
Tile t2 = new Tile();
t2.setItem(t.getItem());
yield t2;
};
this.tiles[i][j] = newTile;

}
}
this.robbiState = new RobbiState(robbi.getX(), robbi.getY(), robbi.getFacing(), robbi.getItem());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static Locale getLocale() {
* @return true, if the saving was successful, false otherwise
*/
public static boolean finish() {
properties.put(LANGUAGE_PROPERTY, I18nUtils.getLocale().toString());
properties.put(LANGUAGE_PROPERTY, I18nUtils.getLocale().toLanguageTag());
properties.put(SOUNDS_PROPERTY, Boolean.toString(SoundManager.getSound()));
properties.put(DARKMODE_PROPERTY, Boolean.toString(SceneManager.getDarkmode()));
try (FileOutputStream fos = new FileOutputStream(DIR + FILE)) {
Expand Down

0 comments on commit 1ec1b33

Please sign in to comment.