Пример #1
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'");
      }
    }
  }
Пример #2
0
	public void inputAnalyser ( String input ) {

		if ( mode == MODE.DEFAULT ) {
			outputField.setText ( "" );
			switch ( input ) {
				case "1" :
					mode = MODE.NEW_ACCOUNT;
					outputField.append ( "Enter First Name: \n" );
					break;

				case "2" :
					// redundant but
					mode = MODE.BALANCE;
					if ( bank.getCurrentUserIndex ( ) > - 1 ) {
						outputField.setText (
						        "Your current balance:\n" + bank.getCurrentCustomer ( ).getKonto ( ).getGuthaben ( ) );
					} else {
						outputField.setText ( "No user logged in / created. ERROR currentUserIndex !=0" );
					}
					setToFinalMode ( );
					break;

				case "3" :
					mode = MODE.DEPOSIT;
					if(bank.getCurrentUserIndex ( ) != -1){
					outputField.append ( "Enter value to Deposit: \n" );
					}else{
						outputField.setText ( "No user logged in / created. ERROR currentUserIndex !=0" );
					}
					break;

				case "4" :
					mode = MODE.WITHDRAW;
					if(bank.getCurrentUserIndex ( ) != -1){
					outputField.append ( "Enter value to Withdraw: \n" );
					}else{
						outputField.setText ( "No user logged in / created. ERROR currentUserIndex !=0" );
					}
					break;

				case "5" :
					mode = MODE.PASSTIME;
					if(bank.getCurrentUserIndex ( ) != -1){
					outputField
					        .append (
					                "One year passed...  \n Your bank currently has an interest rate of: "
					                        + bank.getInterestRate ( ) + " \n Old account balance: "
					                        + bank.getCurrentCustomer ( ).getKonto ( )
					                                .getGuthaben ( )
					                        + "\n New account balance:");
					bank.getCurrentCustomer ( ).getKonto ( )
                    .deposit ( bank.getCurrentCustomer ( ).getKonto ( ).getGuthaben ( )
                            / 100 * bank.getInterestRate ( ) );
					outputField.append ( 	bank.getCurrentCustomer ( ).getKonto ( ).getGuthaben ( )
					                        + "\n \n\n Press BACK to get back to the main menu" );
					}else{
						outputField.setText ( "No user logged in / created. ERROR currentUserIndex !=0" );
					}
					break;

				default :
					outputField.append ( "no valid input \n\n" );
					defaultText ( );
					break;
			}
		} else if ( mode == MODE.NEW_ACCOUNT ) {
			if ( tempFirstName == null ) {
				tempFirstName = input;
				outputField.append ( input + "\n Enter last name:\n" );
			} else {
				tempLastName = input;
				outputField.append ( input + "\n\n" );
				bank.addCustomer ( new Kunde ( tempFirstName , tempLastName ) );
				outputField.append (
				        "A new customer was created. The name is:\n" + bank.getCurrentCustomer ( ).getFullName ( )
				                + "\nCustomer ID: \n" + bank.getCurrentCustomer ( ).getKundenNummer ( )
				                + "\nAccount ID: \n" + bank.getCurrentCustomer ( ).getKonto ( ).getKontoNummer ( )
				                + "\nAccount balance: \n" + bank.getCurrentCustomer ( ).getKonto ( ).getGuthaben ( ) );
				tempFirstName = null;
				tempLastName = null;
				setToFinalMode ( );
			}
		} else if ( mode == MODE.DEPOSIT ) {
			outputField.append (