Esempio n. 1
0
  /** Returns all bank markets */
  public List<Market> getBankMarkets() {
    List<Market> markets = new ArrayList<Market>();

    for (Bank bank : bankByMarketMap.values()) {
      markets.add(bank.getMarketId());
    }
    return markets;
  }
  private boolean execute(CashCorrectionAction cashAction) {

    boolean result = false;

    CashHolder ch = cashAction.getCashHolder();
    int amount = cashAction.getAmount();

    String errMsg = null;

    while (true) {
      if (amount == 0) {
        errMsg = LocalText.getText("CorrectCashZero");
        break;
      }
      if ((amount + ch.getCash()) < 0) {
        errMsg =
            LocalText.getText(
                "NotEnoughMoney", ch.getName(), Bank.format(ch.getCash()), Bank.format(-amount));
        break;
      }
      break;
    }

    if (errMsg != null) {
      DisplayBuffer.add(LocalText.getText("CorrectCashError", ch.getName(), errMsg));
      result = true;
    } else {
      // no error occured
      gameManager.getMoveStack().start(false);

      Bank bank = gameManager.getBank();

      String msg;
      if (amount < 0) {
        // negative amounts: remove cash from cashholder
        new CashMove(ch, bank, -amount);

        msg = LocalText.getText("CorrectCashSubstractMoney", ch.getName(), Bank.format(-amount));
      } else {
        // positive amounts: add cash to cashholder
        new CashMove(bank, ch, amount);
        msg = LocalText.getText("CorrectCashAddMoney", ch.getName(), Bank.format(amount));
      }
      ReportBuffer.add(msg);
      gameManager.addToNextPlayerMessages(msg, true);
      result = true;
    }

    return result;
  }
Esempio n. 3
0
  public static void main(String[] args) throws java.io.IOException {
    if (args.length < 1) {
      System.err.println("Usage java TestHarness <input file> <R1> <R2> ...");
      System.exit(-1);
    }

    // get the name of the input file
    String inputFile = args[0];

    // now get the resources
    int numOfResources = args.length - 1;

    // the initial number of resources
    int[] initialResources = new int[numOfResources];

    // the resources involved in the transaction
    int[] resources = new int[numOfResources];
    for (int i = 0; i < numOfResources; i++)
      initialResources[i] = Integer.parseInt(args[i + 1].trim());

    // create the bank
    Bank theBank = new BankImpl(initialResources);
    int[] maxDemand = new int[numOfResources];

    // read initial values for maximum array
    String line;
    try {
      BufferedReader inFile = new BufferedReader(new FileReader(inputFile));

      int threadNum = 0;
      int resourceNum = 0;

      for (int i = 0; i < Customer.COUNT; i++) {
        line = inFile.readLine();
        StringTokenizer tokens = new StringTokenizer(line, ",");

        while (tokens.hasMoreTokens()) {
          int amt = Integer.parseInt(tokens.nextToken().trim());
          maxDemand[resourceNum++] = amt;
        }
        theBank.addCustomer(threadNum, maxDemand);
        ++threadNum;
        resourceNum = 0;
      }
    } catch (FileNotFoundException fnfe) {
      throw new Error("Unable to find file " + inputFile);
    } catch (IOException ioe) {
      throw new Error("Error processing " + inputFile);
    }

    // now loop reading requests

    BufferedReader cl = new BufferedReader(new InputStreamReader(System.in));
    int[] requests = new int[numOfResources];
    String requestLine;

    while ((requestLine = cl.readLine()) != null) {
      if (requestLine.equals("")) continue;

      if (requestLine.equals("*"))
        // output the state
        theBank.getState();
      else {
        // we know that we are reading N items on the command line
        // [RQ || RL] <customer number> <resource #1> <#2> <#3>
        StringTokenizer tokens = new StringTokenizer(requestLine);

        // get transaction type - request (RQ) or release (RL)
        String trans = tokens.nextToken().trim();

        // get the customer number making the tranaction
        int custNum = Integer.parseInt(tokens.nextToken().trim());

        // get the resources involved in the transaction
        for (int i = 0; i < numOfResources; i++) {
          resources[i] = Integer.parseInt(tokens.nextToken().trim());
          System.out.println("*" + resources[i] + "*");
        }

        // now check the transaction type
        if (trans.equals("RQ")) { // request
          if (theBank.requestResources(custNum, resources)) System.out.println("Approved");
          else System.out.println("Denied");
        } else if (trans.equals("RL")) // release
        theBank.releaseResources(custNum, resources);
        else // illegal request
        System.err.println("Must be either 'RQ' or 'RL'");
      }
    }
  }
Esempio n. 4
0
 public BankMap(List<Bank> allBanksForClient) {
   for (Bank bank : allBanksForClient) {
     bankByMarketMap.put(bank.getMarketId().getOrganizationId(), bank);
   }
 }