Java线程之银行存取款

银行的存取款可以分为存款和取款:

当去存款的时候会先显示账户信息,然后将存进去的钱和账户原有的钱数相加,返回存款之后账户信息;

当去取款的时候会先显示账户信息,然后将取钱数和账户里面的钱相对比,如果取钱数大于账户里面的钱,那就将账户里面的钱全部取出,如果取钱数小于账户的钱,那就吐出所取的钱数。


本例使用了线程来处理,而且使用了线程同步(synchronized)的知识点


一.首先先建立一个账户类:

class Account{
	private String name;
	private int value;
	public int getMoney(int money){
		if(money >= this.value){
			this.value = 0;
		}else{
			this.value = this.value - money;
		}
		return money;
	}
	public void saveMoney(int money){
		this.value = this.value + money;
	}
	public int searchMoney(){
		return this.value;
	}
	public Account() {
		super();
	}
	public Account(String name, int value) {
		super();
		this.name = name;
		this.value = value;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}

}


二.建立一个存钱的线程,处理存钱的操作:

class SaveAccount implements Runnable{
	private Account a;
	private int money;
	
	public SaveAccount() {
		super();
	}

	public SaveAccount(Account a, int money) {
		super();
		this.a = a;
		this.money = money;
	}

	@Override
	public void run() {
		
		synchronized (a) 
		{
			try {
				int currentMoney = a.searchMoney();
				Thread.sleep(1000);
				a.saveMoney(money);
				System.out.println("尊敬的客户:" + a.getName() + "您好! 您的账户余额" + currentMoney 
						+ "元,您存入" + this.money + "元,当前账户余额为:" + a.searchMoney() + "元");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		
		
		}
	}
	
}

三.建立一个取钱线程,包含取钱的操作:

class GetMoney implements Runnable{
	private Account a;
	private int money;
	
	public GetMoney() {
		super();
	}

	public GetMoney(Account a, int money) {
		super();
		this.a = a;
		this.money = money;
	}

	@Override
	public void run() {
		
		synchronized (a) 
		{
			try {
				int currentMoney = a.searchMoney();
				Thread.sleep(1000);
				a.getMoney(money);
				System.out.println("尊敬的客户:" + a.getName() + "您好! 您的账户余额" + currentMoney 
						+ "元,您取出" + this.money + "元,当前账户余额为:" + a.searchMoney() + "元");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}

四.接下来是主函数调用:

public class ThreadTest6 {
	public static void main(String[] args) {
		Account account = new Account("Zhou",10000000);
		SaveAccount sa = new SaveAccount(account, 10);
		GetMoney gm =new GetMoney(account, 10000);
		Thread t = new Thread(gm);
		Thread t1 = new Thread(sa);
		t.start();
		t1.start();
	}
}

五.代码运行截图:



  • 4
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴为您解答关于Java线程银行存取款代码的问题。以下是示例代码: ``` java public class BankAccount { private int balance; public BankAccount(int initialBalance) { balance = initialBalance; } public synchronized void deposit(int amount) { balance += amount; } public synchronized boolean withdraw(int amount) { if (balance >= amount) { balance -= amount; return true; } return false; } public synchronized int getBalance() { return balance; } } public class BankTransaction implements Runnable { private BankAccount account; private int transactionType; private int transactionAmount; public BankTransaction(BankAccount account, int transactionType, int transactionAmount) { this.account = account; this.transactionType = transactionType; this.transactionAmount = transactionAmount; } @Override public void run() { if (transactionType == 1) { // Deposit account.deposit(transactionAmount); System.out.println("Deposited " + transactionAmount + " into account. New balance is " + account.getBalance()); } else if (transactionType == 2) { // Withdraw boolean result = account.withdraw(transactionAmount); if (result) { System.out.println("Withdrew " + transactionAmount + " from account. New balance is " + account.getBalance()); } else { System.out.println("Could not withdraw " + transactionAmount + " from account. Insufficient funds."); } } else { System.out.println("Invalid transaction type."); } } } public class Main { public static void main(String[] args) { BankAccount account = new BankAccount(1000); Thread thread1 = new Thread(new BankTransaction(account, 1, 500)); // Deposit 500 Thread thread2 = new Thread(new BankTransaction(account, 2, 200)); // Withdraw 200 Thread thread3 = new Thread(new BankTransaction(account, 2, 800)); // Withdraw 800 Thread thread4 = new Thread(new BankTransaction(account, 1, 300)); // Deposit 300 thread1.start(); thread2.start(); thread3.start(); thread4.start(); } } ``` 注意:此代码只是一个示例,实际应用中,可能需要考虑更多的并发性问题,例如死锁、饥饿等问题。如果您在实际项目中使用多线程,请确保仔细测试并仔细考虑并发性问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值