forked from nus-cs2113-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5a8463
commit 643e7b6
Showing
7 changed files
with
118 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters