Skip to content

Commit

Permalink
Can save inventory items now
Browse files Browse the repository at this point in the history
  • Loading branch information
BestDownLoader365 committed Apr 11, 2024
1 parent a5a8463 commit 643e7b6
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/main/java/command/inventory/SellCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SellCommand extends Command {
String userCommand;

public SellCommand(String userCommand) {
commandDescription = "SELL!";
this.userCommand = userCommand;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/command/inventory/UseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class UseCommand extends Command {
String userCommand;

public UseCommand(String userCommand) {
commandDescription = "USE!";
this.userCommand = userCommand;
}

Expand Down
68 changes: 68 additions & 0 deletions src/main/java/filereader/InventoryItemsStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package filereader;

import inventoryitems.Consumable;
import inventoryitems.Item;
import inventoryitems.ShopItem;
import map.PlayerInventory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;

import static filereader.filepath.InventoryItemsPath.INVENTORY_ITEMS_PATH;
import static main.CalculaChroniclesOfTheAlgorithmicKingdom.PLAYER_INVENTORY;

public class InventoryItemsStorage {

public void readFile(PlayerInventory playerInventory) throws FileNotFoundException {
try {
Files.createDirectories(Paths.get("./data"));
} catch (IOException e) {
System.out.println("Fail to create data directory!\n" + e.getMessage());
}

File inventoryItems = new File(INVENTORY_ITEMS_PATH);

if (!inventoryItems.exists() || inventoryItems.length() == 0) {
try {
Files.createFile(Paths.get(INVENTORY_ITEMS_PATH));
} catch (IOException e) {
System.out.println("Fail to create inventory text!\n" + e.getMessage());
}
return;
}

Scanner fileContent = new Scanner(inventoryItems);
while (fileContent.hasNext()) {
String originalData = fileContent.nextLine();
String[] data = originalData.split("\\|");
Consumable item = new Consumable(Integer.parseInt(data[0]), Integer.parseInt(data[1]), data[2],
data[3], Integer.parseInt(data[4]));
item.setQuantity(Integer.parseInt(data[5]));
PLAYER_INVENTORY.loadInventory(item);
}
fileContent.close();
}

public void saveFile(PlayerInventory playerInventory) throws IOException {
ArrayList<Item> generalItems = playerInventory.getGeneralItems();
new FileWriter(INVENTORY_ITEMS_PATH).close();
FileWriter fileWriter = new FileWriter(INVENTORY_ITEMS_PATH);
for (Item item : generalItems) {
String currentItem = "";
currentItem += item.getHealAmt() + "|";
currentItem += item.getDamageAmpAmt() + "|";
currentItem += item.getDescription() + "|";
currentItem += item.getName() + "|";
currentItem += item.getSellPrice() + "|";
currentItem += item.getQuantity();
fileWriter.write(currentItem + System.lineSeparator());
}
fileWriter.close();
}
}
5 changes: 5 additions & 0 deletions src/main/java/filereader/filepath/InventoryItemsPath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package filereader.filepath;

public class InventoryItemsPath {
public static final String INVENTORY_ITEMS_PATH = "./data/inventory.txt";
}
8 changes: 8 additions & 0 deletions src/main/java/inventoryitems/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public int getSellPrice() {
return sellPrice;
}

public int getDamageAmpAmt() {
return -1;
}

public int getHealAmt() {
return -1;
}

public void setSellPrice(int sellPrice) {
this.sellPrice = sellPrice;
}
Expand Down
44 changes: 31 additions & 13 deletions src/main/java/main/CalculaChroniclesOfTheAlgorithmicKingdom.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main;


import filereader.InventoryItemsStorage;
import filereader.MapStorage;
import filereader.PlayerStatusStorage;
import hint.HintHandler;
Expand Down Expand Up @@ -40,6 +41,7 @@ public static void main(String[] args) {
public void startGame() {
Scanner in = new Scanner(System.in);

InventoryItemsStorage inventoryItemsStorage = new InventoryItemsStorage();
MapStorage mapStorage = new MapStorage();
PlayerStatusStorage playerStatusStorage = new PlayerStatusStorage();

Expand All @@ -49,6 +51,12 @@ public void startGame() {
} catch (IOException e) {
System.out.println("Can not read playerStatus !!\n" + e.getMessage());
}

try {
inventoryItemsStorage.readFile(PLAYER_INVENTORY);
} catch (IOException e) {
System.out.println("Can not read inventory file!\n" + e.getMessage());
}
storedMaps.add(PLAYER_INVENTORY);
mapIndex.put(INVENTORY_IDENTITY, storedMaps.size() - 1);
TextBox textBox = new TextBox();
Expand All @@ -59,7 +67,6 @@ public void startGame() {
textBox.initTextBox();



PLAYER_INVENTORY.setPlayerStatus(playerStatus);
PLAYER_INVENTORY.setCurrentTextBox(textBox);

Expand Down Expand Up @@ -93,28 +100,39 @@ public void startGame() {
executeCommand(userCommand, in);

printMessageUnderMap(userCommand, ui, playerStatus, textBox);
try {
mapStorage.saveMap(storedMaps.get(mapIndex.get(FIRST_MAP_IDENTITY)));
} catch (IOException e) {
System.out.println("Can not save the map!\n" + e.getMessage());
}
try {
playerStatusStorage.savePlayerStatus(playerStatus);
} catch (IOException e) {
System.out.println("Can not save Player Status" + e.getMessage());
}

saveAllGameFile(mapStorage, playerStatusStorage, playerStatus, userCommand, inventoryItemsStorage);
} while (!userCommand.getCommandDescription().equals("TIRED"));
}

private void saveAllGameFile(MapStorage mapStorage, PlayerStatusStorage playerStatusStorage,
PlayerStatus playerStatus, Command userCommand,
InventoryItemsStorage inventoryItemsStorage) {
try {
mapStorage.saveMap(storedMaps.get(mapIndex.get(FIRST_MAP_IDENTITY)));
} catch (IOException e) {
System.out.println("Can not save the map!\n" + e.getMessage());
}
try {
playerStatusStorage.savePlayerStatus(playerStatus);
} catch (IOException e) {
System.out.println("Can not save Player Status" + e.getMessage());
}

try {
inventoryItemsStorage.saveFile(PLAYER_INVENTORY);
} catch (IOException e) {
System.out.println("Can not save inventory items!\n" + e.getMessage());
}
}

private static void printMessageUnderMap(Command userCommand, Ui ui, PlayerStatus playerStatus, TextBox textBox) {
if (!userCommand.getCommandDescription().equals("HelpMe!!") &&
!userCommand.getCommandDescription().equals("TIRED")) {
ui.printPlayerStatus(playerStatus);
if (storedMaps.get(currentMap) instanceof BattleInterface ||
storedMaps.get(currentMap) instanceof ShopMap) {
ui.printEnemy(storedMaps.get(currentMap));
} else if (storedMaps.get(currentMap) instanceof PlayerInventory){
} else if (storedMaps.get(currentMap) instanceof PlayerInventory) {
ui.printInventory(playerStatus.getPlayerInventory().getGeneralItems(),
playerStatus.getPlayerInventory().getInventoryNames().get(0),
storedMaps.get(currentMap).getWidth(), storedMaps.get(currentMap).getHeight());
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/map/PlayerInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public PlayerInventory() {
height = 8;
}

public void loadInventory(Consumable item){
generalItems.add(item);
}

public void addItems(Consumable item) {
if (generalItems.isEmpty()) {
item.setQuantity(1);
Expand Down

0 comments on commit 643e7b6

Please sign in to comment.