-
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 #5 from gal11002/John-B
Merging with master
- Loading branch information
Showing
12 changed files
with
554 additions
and
418 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,84 @@ | ||
package controller; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class RPGame { | ||
private ArrayList<String> merchantInventoryList1 = new ArrayList<String>(); // merchant 1's inventory list | ||
private ArrayList<String> merchantInventoryList2 = new ArrayList<String>(); // merchant 2's inventory list | ||
private ArrayList<String> merchantInventoryList3 = new ArrayList<String>(); // merchant 3's inventory list | ||
private ArrayList<String> playerInventoryList = new ArrayList<String>(); // the player's inventory list | ||
|
||
public RPGame() { | ||
//constructor | ||
} | ||
|
||
/** | ||
* This method scans the inventory.txt file located in src\config. It will read lines of the format <stringname> <price> and store them in the inventory list member variables | ||
* | ||
*/ | ||
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; | ||
|
||
while(fileScanner.hasNextLine()) { // loops as long as there is another line to read | ||
token = fileScanner.next(); | ||
if (token.equals("merchant")) | ||
currentMerchant = fileScanner.nextInt(); | ||
else { | ||
item = token + " " + fileScanner.nextInt(); // a string containing the item name and price | ||
if (currentMerchant == 1) | ||
merchantInventoryList1.add(item); | ||
else if (currentMerchant == 2) | ||
merchantInventoryList2.add(item); | ||
else | ||
merchantInventoryList3.add(item); | ||
playerInventoryList.add(item); | ||
} | ||
if (fileScanner.hasNextLine()) // only advances to the next line if there is one to read | ||
fileScanner.nextLine(); | ||
} | ||
|
||
} catch (FileNotFoundException e) { // if inventory.txt is deleted or missing | ||
System.out.println("Inventory file not found"); | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* This method returns the specified merchant inventory list | ||
* | ||
* @param merchant number | ||
* @return the specified merchant's inventory list | ||
*/ | ||
public ArrayList<String> getMerchantInventoryList(int merchantNumber) { | ||
if (merchantNumber == 1) | ||
return merchantInventoryList1; | ||
else if (merchantNumber == 2) | ||
return merchantInventoryList2; | ||
else if (merchantNumber == 3) | ||
return merchantInventoryList3; | ||
else | ||
{ | ||
System.out.println("Invalid merchant number"); | ||
return null; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* This method returns the player's inventory list | ||
* | ||
* @return the player's inventory list | ||
*/ | ||
public ArrayList<String> getPlayerInventoryList() { | ||
return playerInventoryList; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.