1
/**
 *
 * 
 * A small class to handle deposits and withdrawls from a bank account
 * 
 */
package spendingsimulation;

//import java.util.Random;

public class BankAccount 
{
    public int accountBalance;
    public int incomeAmt;
    public BankAccount()
    {
        accountBalance = 0 ;
        //incomeAmt = 0;
    }

    public void deposit(int addAmount, String name)
    {
        // the 'name' argument holds the name of the source of this credit 

        accountBalance+=addAmount ;
        System.out.println(name + " added " + addAmount) ;
        System.out.println("Account balance is now standing at " + accountBalance);
    }

    public void withdraw(int takeAmount, String name)
    {
        // the 'name' argument holds the name of the bill being paid

        accountBalance-=takeAmount ;
        System.out.println(name + " took " + takeAmount) ;
        System.out.println("Account balance is now standing at " + accountBalance);
    }

    public int getBalance()
    {
        return accountBalance ;
    }

    //@Override
    /*public void run()
    {
        try
        {
            deposit(incomeAmt, incomeS1);
            getBalance();
            Thread.sleep(100) ;
        } // End of try block
        catch(InterruptedException ex)
        {
            System.out.println(typeOfUtility + " Terminated early") ;
        } // End of Catch clause

        System.out.println(typeOfUtility + " has finished") ;
    }  // End of method run()
    */


} // End of class BankAccount

package spendingsimulation;

import java.lang.Thread ;

public class SpendMain {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Viewing expenditure");

        // Incoming payments
        Thread incomeS1 = new Thread(new Incomming("Wage Recieved", 2000, 1000) );
        Thread incomeS2 = new Thread(new Incomming("Interest Recieved", 10, 4000) );

        // Outgoing costs
        Thread outgoingS1 = new Thread(new Consumables("Oil Bill", 250, 3000)) ;
        Thread outgoingS2 = new Thread(new Consumables("Food Bill", 600, 1000)) ;
        Thread outgoingS3 = new Thread(new Consumables("Electricity Bill", 50, 1000)) ;
        Thread outgoingS4 = new Thread(new Consumables("Entertrainment Bill", 400, 1000)) ;
        Thread outgoingS5 = new Thread(new Consumables("Shopping Bill", 200, 1000)) ;

        System.out.println("Expenditure commencing") ;


        // Get the threads going
        //Incomming
        incomeS1.start();
        incomeS2.start();

        // Outgoing
        outgoingS1.start();
        outgoingS2.start();
        outgoingS3.start();
        outgoingS4.start();
        outgoingS5.start();


        System.out.println("Expenditure is now underway.") ; 


    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spendingsimulation;

import java.util.Random;

/**
 *
 * @author B00533474
 */
public class Incomming implements Runnable {

    //private int workingTime ;
   private String typeOfUtility ;
   // private Random randGen ;
    //private int incomingAmt;

    public Incomming(String name, int addAmount, int time){
        typeOfUtility = name ;

      //  randGen = new Random();
        //workingTime = randGen.nextInt(10000) ; // Up to 10 seconds

      //  outgoingAmt = amt;
    }

    //@Override
    public void run()
    {
        try
        {
            System.out.println(typeOfUtility + " has come into the account");
            System.out.println(" The balance on the account is now: ");
            //this.getBalance();
            Thread.sleep(1000) ;
        } // End of try block
        catch(InterruptedException ex)
        {
            System.out.println(typeOfUtility + " Terminated early") ;
        } // End of Catch clause

        System.out.println(typeOfUtility + " has finished") ;
    }  // End of method run()

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spendingsimulation;

import java.util.Random;
import java.util.Timer;

/**
 *
 * @author B00533474
 */
public class Consumables implements Runnable {

    private int workingTime ;
    private String typeOfUtility ;
    private Random randGen ;
    private int incomeAmt;


    Consumables(String name, int amt, int time) {
        typeOfUtility = name ;
        randGen = new Random();
        workingTime = randGen.nextInt(10000) ; // Up to 10 seconds
        incomeAmt = amt;
    }

    @Override
    public void run() {
        Timer myTimer = new Timer();

    }

}

私の問題は、 run() メソッドがいくつあるべきか、どのクラスにあるべきか、そして Bank Account クラスに正しくアクセスして使用する方法がわからないことです。また、タイマー クラスの使用方法も不明です。1 秒を 1 週間に相当させたいと考えています。どんな助けでも大歓迎です。

4

1 に答える 1