Skip to content

John b #5

Merged
merged 4 commits into from
Feb 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions MerchantRPGCSE2102/src/controller/RPGame.java
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;
}
}
83 changes: 0 additions & 83 deletions MerchantRPGCSE2102/src/game/Item.java

This file was deleted.

155 changes: 0 additions & 155 deletions MerchantRPGCSE2102/src/game/Merchant.java

This file was deleted.

Loading