📄 bankoperation.java
字号:
package Bank;
import java.util.Random;
public class BankOperation{
public static void main(String[] args){
int initialBalance = 500; //The initial account balance 初始化帐户余额
int totalCredits = 0; //Total credits on the account帐户上的存款总额
int totalDebits = 0; //Total debits on the account 帐户上的借款总额
int transactionCount = 20; //Number of debits and credits存款、借款总数
//Create the account, the bank, and the clerks...创建银行、帐户、职员
Bank theBank = new Bank(); //Create a bank 创建银行
Clerk clerk1 = new Clerk(theBank); //Create the first clerk 借款业务的职员
Clerk clerk2 = new Clerk(theBank); //Create the second clerk存款业务的职员
Account account = new Account(1,initialBalance); //Create an account 账户
//Create the threads for the clerks as daemon, and start them off 添加创建职员线程,并启动线程的代码
Thread clerk1Thread = new Thread(clerk1);
Thread clerk2Thread = new Thread(clerk2);
clerk1Thread.setDaemon(true); //set first as daemon 守护线程
clerk2Thread.setDaemon(true); //set second as daemon
clerk1Thread.start(); //Start the first 启动线程
clerk2Thread.start(); //Start the second
//Generate the transactions of each type and pass to the clerks (顾客办理业务)
Random rand = new Random(); //Random number generator
Transaction transaction; //Stores a transaction
int amount = 0; //Stores an amount of money初始化交易数额
for(int i =1;i<= transactionCount; i++){
/*存款交易*/
amount = 50 + rand.nextInt(26); //Generate amount of $50 to $75 动态生成交易总额,50-76
transaction = new Transaction(account, //Account
Transaction.CREDIT, //Credit transaction 存款 transaction
amount); //of amount 交易总额
totalCredits += amount; //Keep total credit tally 重置存款总额
//Wait until the first clerk is free 等待,直到clerk1空闲
while(clerk1.isBusy()){
try{
Thread.sleep(25); //Busy to try later
}catch(InterruptedException e){
System.out.println(e);
}
}
clerk1.doTransaction(transaction); //Now do the credit 处理存款业务
/*取款交易*/
amount = 30 + rand.nextInt(31); //Generate amount of $30 to $60 动态生成交易总额,30-60
transaction = new Transaction(account, //Account transaction
Transaction.DEBIT, //Debit 取款 transaction
amount); //of amount
totalDebits +=amount; //重置借款总额
//Wait until the second clerk is free 等待,直到clerk2空闲
while(clerk2.isBusy()){
try{
Thread.sleep(25); //Busy so try later
}catch(InterruptedException e){
System.out.println(e);
}
}
clerk2.doTransaction(transaction); //Now do the credit 处理借款业务
}
//Wait until both clerks are done 等待,直到clert1和clerk2都空闲,处理业务完毕
while(clerk1.isBusy() || clerk2.isBusy()){
try{
Thread.sleep(25);
}catch(InterruptedException e){
System.out.println(e);
}
}
//一旦所有的事务处理完毕,就应该输出结果。
//但是,程序从循环退出时,职员可能仍然很忙,因此需要等待2个职员空闲后才能输出结果。
//Now output the results 最后,输出结果
System.out.println(
"Original balance 初始余额 : $"+ initialBalance +"\n" +
"Tolal credits 存款总数 : $"+ totalCredits +"\n" +
"Total debits 借款总数 : $"+ totalDebits +"\n" +
"Final balance 帐户余额 : $"+ account.getBalance() +"\n" +
"Should be 应该余额 : $"+(initialBalance + totalCredits - totalDebits)
);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -