Skip to content

Commit

Permalink
Merge pull request #116 from B1G-SAM/File-Handling
Browse files Browse the repository at this point in the history
Change implementation of printing of shopkeeper
  • Loading branch information
B1G-SAM committed Apr 13, 2024
2 parents 6406418 + 8186e10 commit 43f9178
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/main/java/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import textbox.TextBox;
import map.BaseMap;

import java.io.FileNotFoundException;
import java.util.Scanner;


Expand All @@ -20,7 +21,7 @@ public Command() {

public abstract void execute();

public void execute(Scanner in) {
public void execute(Scanner in) throws FileNotFoundException {
}

public void execute(PlayerStatus playerStatus) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/command/fight/FightingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import map.BaseMap;
import map.ShopMap;

import java.io.FileNotFoundException;
import java.util.Scanner;

import static map.BaseMap.mapIndex;
Expand All @@ -21,7 +22,7 @@ public void execute() {
}

@Override
public void execute(Scanner in) {
public void execute(Scanner in) throws FileNotFoundException {
if (currentMapForCommand instanceof map.BattleInterface.BattleInterface) {
currentMapForCommand.enableFight(in);
BaseMap.currentMap = mapIndex.get(FIRST_MAP_IDENTITY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public class CalculaChroniclesOfTheAlgorithmicKingdom {
public static final int START_DAMAGE = 5;
public static final PlayerInventory PLAYER_INVENTORY = new PlayerInventory();

public static void main(String[] args) throws InterruptedException {
public static void main(String[] args) throws InterruptedException, FileNotFoundException {
new CalculaChroniclesOfTheAlgorithmicKingdom().startGame();
}

public void startGame() throws InterruptedException {
public void startGame() throws InterruptedException, FileNotFoundException {
Scanner in = new Scanner(System.in);

InventoryItemsStorage inventoryItemsStorage = new InventoryItemsStorage();
Expand Down Expand Up @@ -149,7 +149,8 @@ private static void printMessageUnderMap(Command userCommand, Ui ui, PlayerStatu
}
}

private static void executeCommand(Command userCommand, Scanner in, PlayerStatus playerStatus) {
private static void executeCommand(Command userCommand, Scanner in, PlayerStatus playerStatus)
throws FileNotFoundException {
if (userCommand.getCommandDescription().equals("FIGHT!")) {
userCommand.execute(in);
} else if (userCommand.getCommandDescription().equals("RESET!")) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/map/BaseMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import textbox.TextBox;
import ui.Ui;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
Expand Down Expand Up @@ -41,7 +42,7 @@ public void setPlayerY(int playerY) {

public abstract void enableFight();

public void enableFight(Scanner in) {
public void enableFight(Scanner in) throws FileNotFoundException {
assert in != null;
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/map/ShopMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import textbox.TextBox;
import ui.Ui;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

Expand Down Expand Up @@ -58,7 +59,7 @@ public void enableFight(){


@Override
public void enableFight(Scanner in) {
public void enableFight(Scanner in) throws FileNotFoundException {
String answerCommand = "";
Ui ui = new Ui();
queueTextBox();
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import textbox.PlayerStatus;
import textbox.TextBox;
import math.MathQuestion;
import filereader.FileReader;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;


public class Ui {
private static final int DEFAULT_WIDTH_OF_BATTLE_INTERFACE = 50;
Expand Down Expand Up @@ -122,15 +121,19 @@ public void printEnemy(BaseMap map) {
printDividingLine();
}

public void printShopKeeper(ShopKeeper cat){
String filePath = cat.getFilePath();
public void printShopKeeper(ShopKeeper cat) throws FileNotFoundException {
FileReader fileReader = new FileReader(cat.getFilePath());
ArrayList<ArrayList<Character>> mapData = new ArrayList<>();
try {
List<String> lines = Files.readAllLines(Paths.get(filePath));
for (String line : lines) {
System.out.println(line);
mapData = fileReader.readDesign();
} catch (Exception e) {
System.out.println("Unable to read file from local");
}
for (ArrayList<Character> row : mapData) {
for (Character ch : row) {
System.out.print(ch); // Print each character without a newline
}
} catch (IOException e) {
System.out.println("Error reading the file at " + filePath);
System.out.println(); // Print a newline after each row
}
}

Expand Down

0 comments on commit 43f9178

Please sign in to comment.