Skip to content

Commit

Permalink
Copied over RPGame.java and inventory.txt from John-B branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Li committed Feb 9, 2015
1 parent e2238d5 commit 9ad38e6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
11 changes: 11 additions & 0 deletions MerchantRPGCSE2102/src/config/inventory.txt
Original file line number Diff line number Diff line change
@@ -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
62 changes: 62 additions & 0 deletions MerchantRPGCSE2102/src/game/RPGame.java
Original file line number Diff line number Diff line change
@@ -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<String> merchantInventory1 = new ArrayList<String>();
private ArrayList<String> merchantInventory2 = new ArrayList<String>();
private ArrayList<String> merchantInventory3 = new ArrayList<String>();

public RPGame() {

}

// Scans src/config/inventory.txt for each merchant's inventory and stores them as the string "<item> <cost>" 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<String> getMerchantInventory(int merchantNumber) {
if (merchantNumber == 1)
return merchantInventory1;
else if (merchantNumber == 2)
return merchantInventory2;
else
return merchantInventory3;
}
}

0 comments on commit 9ad38e6

Please sign in to comment.