From 9ad38e6e766a1445aacb185565de5842a69d93aa Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Mon, 9 Feb 2015 11:33:57 -0500 Subject: [PATCH] Copied over RPGame.java and inventory.txt from John-B branch --- MerchantRPGCSE2102/src/config/inventory.txt | 11 ++++ MerchantRPGCSE2102/src/game/RPGame.java | 62 +++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 MerchantRPGCSE2102/src/config/inventory.txt create mode 100644 MerchantRPGCSE2102/src/game/RPGame.java diff --git a/MerchantRPGCSE2102/src/config/inventory.txt b/MerchantRPGCSE2102/src/config/inventory.txt new file mode 100644 index 0000000..fcf6cb0 --- /dev/null +++ b/MerchantRPGCSE2102/src/config/inventory.txt @@ -0,0 +1,11 @@ +merchant 1 +water 3 +armor 5 +food 10 +merchant 2 +wood 3 +tarp 6 +merchant 3 +glass 3 +tape 13 +rope 5 \ No newline at end of file diff --git a/MerchantRPGCSE2102/src/game/RPGame.java b/MerchantRPGCSE2102/src/game/RPGame.java new file mode 100644 index 0000000..9556a4c --- /dev/null +++ b/MerchantRPGCSE2102/src/game/RPGame.java @@ -0,0 +1,62 @@ +package game; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Scanner; + +public class RPGame { + private ArrayList merchantInventory1 = new ArrayList(); + private ArrayList merchantInventory2 = new ArrayList(); + private ArrayList merchantInventory3 = new ArrayList(); + + public RPGame() { + + } + + // Scans src/config/inventory.txt for each merchant's inventory and stores them as the string " " in the corresponding merchantInventory ArrayList + public void inventoryFromFile() { + Scanner fileScanner = null; + try { + fileScanner = new Scanner(new File("src/config/inventory.txt")); // inventory.txt must be located the config folder + int currentMerchant = 0; // keeps track of which merchant's inventory the scanner is reading + String token = null; + String item = null; + + // Loops through each line of the text file as long as there is another line to read + while(fileScanner.hasNextLine()) { + //must start from the beginning of a line + token = fileScanner.next(); // first word of the line + if (token.equals("merchant")) + currentMerchant = fileScanner.nextInt(); + else { + item = token + " " + fileScanner.nextInt(); // item name and cost appended together in one string + if (currentMerchant == 1) + merchantInventory1.add(item); + else if (currentMerchant == 2) + merchantInventory2.add(item); + else + merchantInventory3.add(item); + } + // advances to next line unless it has reached the end of the file + if (fileScanner.hasNextLine()) + fileScanner.nextLine(); + } + + } catch (FileNotFoundException e) { // if inventory.txt is deleted or missing + System.out.println("Inventory file not found"); + e.printStackTrace(); + } + + } + + // returns the list of inventory items for a specified merchant + public ArrayList getMerchantInventory(int merchantNumber) { + if (merchantNumber == 1) + return merchantInventory1; + else if (merchantNumber == 2) + return merchantInventory2; + else + return merchantInventory3; + } +}