2013-04-25 46 views
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() 

} 

多線程Java應用程序
/* 
* 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

有沒有什麼事情可以解決這個問題?如果您告訴我們迄今爲止已嘗試的內容,我們將更願意回答您的問題。 (有用的鏈接提出更好的問題:[問],[FAQ]) – Doorknob 2013-04-25 23:15:36

+0

此外,你的問題有相當多的代碼。爲了更快地獲得更好的幫助,請發佈[SSCCE(鏈接)](http://sscce.org)。 – Doorknob 2013-04-25 23:16:02

+0

我建議你在開始之前先讓銀行賬戶線程安全,然後再移動到用線程敲打它。提示,'整數'不是線程安全的。 JVM不保證即使寫入也是原子的。而且,如果沒有volatile,其他線程不太可能看到寫入。 – 2013-04-25 23:23:01

回答

0

這是一個簡單的例子。

您有一個Balance類,其中包含當前餘額AtomicInteger

你有三種runnables,Income,ExpenditureStatement - 這些都是不言自明的。

該方法的肉是main方法。這使用ScheduledExectorService來安排不同的收入事件以不同的費率發生。評論解釋了什麼是什麼。

最後,我們有一項任務在一年後關閉了整件事 - 這可以很容易地更改或刪除,在這種情況下,它將永遠運行。

您有一些間隔期間重複任務和延遲後運行一次的任務(關機任務)的示例。

此實現沒有錯誤處理 - 任務將無提示失敗;但考慮到簡單性,任務不應該公平。

如果您想要引入非整數值,您可能需要使用BigDecimal,這會更復雜,因爲您需要手動輸入synchronize

static final class Balance { 

    private final AtomicInteger balance; 

    public Balance(int initalBalance) { 
     this.balance = new AtomicInteger(initalBalance); 
    } 

    public void debit(final int amount) { 
     balance.addAndGet(-amount); 
    } 

    public void credit(final int amount) { 
     balance.addAndGet(amount); 
    } 

    public int balance() { 
     return balance.get(); 
    } 

    @Override 
    public String toString() { 
     return "£" + balance.get(); 
    } 
} 

static final class Income implements Runnable { 

    private final Balance balance; 
    private final int income; 

    public Income(Balance balance, int income) { 
     this.balance = balance; 
     this.income = income; 
    } 

    @Override 
    public void run() { 
     balance.credit(income); 
    } 
} 

static final class Expenditure implements Runnable { 

    private final Balance balance; 
    private final int cost; 

    public Expenditure(Balance balance, int cost) { 
     this.balance = balance; 
     this.cost = cost; 
    } 

    @Override 
    public void run() { 
     balance.debit(cost); 
    } 
} 

static final class Statement implements Runnable { 

    private final Balance balance; 

    public Statement(Balance balance) { 
     this.balance = balance; 
    } 

    @Override 
    public void run() { 
     System.out.println("Your balance this month is " + balance.toString()); 
    } 
} 

public static void main(String[] args) throws ParseException { 
    final Balance balance = new Balance(1000); 
    final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); 
    //print balance - monthly 
    executorService.scheduleAtFixedRate(new Statement(balance), 0, 4, TimeUnit.SECONDS); 
    //income - monthly 
    executorService.scheduleAtFixedRate(new Income(balance, 1000), 0, 4, TimeUnit.SECONDS); 
    //rent - monthly 
    executorService.scheduleAtFixedRate(new Expenditure(balance, 500), 4, 4, TimeUnit.SECONDS); 
    //food - weekly 
    executorService.scheduleAtFixedRate(new Expenditure(balance, 50), 1, 1, TimeUnit.SECONDS); 
    //shutdown after a year 
    executorService.schedule(new Runnable() { 
     @Override 
     public void run() { 
      executorService.shutdown(); 
     } 
    }, 52, TimeUnit.SECONDS); 

} 
+0

非常感謝大家的幫助,我現在使用'ScheduledExecutorService'按時間間隔運行應用程序。唯一的問題是應用程序運行6分鐘。它應該只跑52秒?我試過這個:@Boris_the_Spider)。一個52秒 executorService.schedule後 '//關閉(新的Runnable(){ @Override 公共無效的run(){ accountBalance.getBalance(); executorService.shutdown();} } ,52 ,TimeUnit。秒); }' 但它仍然跑了6分鐘?任何幫助將不勝感激 – 2013-04-27 17:09:15

+0

更新您的代碼 - 這可能是一個長期運行的任務。 – 2013-04-28 10:03:07

相關問題