-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from gal11002/Gavin-L
Gavin l
- Loading branch information
Showing
6 changed files
with
203 additions
and
27 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
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 |
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,67 @@ | ||
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 if (merchantNumber == 3) | ||
return merchantInventory3; | ||
else | ||
{ | ||
System.out.println("Invalid merchant number"); | ||
return null; | ||
} | ||
} | ||
} |
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,28 @@ | ||
package tests; | ||
|
||
import junit.framework.TestCase; | ||
import game.RPGame; | ||
|
||
public class TestRPGame extends TestCase | ||
{ | ||
private RPGame game; | ||
|
||
public void setup() | ||
{ | ||
game = new RPGame(); | ||
} | ||
|
||
public void testFile() | ||
{ | ||
setup(); | ||
game.inventoryFromFile(); | ||
assertEquals("water 3", game.getMerchantInventory(1).get(0)); | ||
assertEquals("armor 5", game.getMerchantInventory(1).get(1)); | ||
assertEquals("food 10", game.getMerchantInventory(1).get(2)); | ||
assertEquals("wood 3", game.getMerchantInventory(2).get(0)); | ||
assertEquals("tarp 6", game.getMerchantInventory(2).get(1)); | ||
assertEquals("glass 3", game.getMerchantInventory(3).get(0)); | ||
assertEquals("tape 13", game.getMerchantInventory(3).get(1)); | ||
assertEquals("rope 5", game.getMerchantInventory(3).get(2)); | ||
} | ||
} |