银行账户问题

Suppose there is a class PersonalAccount, which has a private member variable balance (type: integer and value is 0 at first);Besides, this class has three member functions:
1. add, add money into user’s account.
2. subtract, subtract money from user’s account, if the balance is insufficient, do not subtract money and print message “insufficient balance..”
3. getBalance, print the user’s balance like “The Balance is: xx”.

/* from younglee..
 * guys! enjoy your journey of c plus plus, against all odds!
 */
#include<iostream>
using namespace std;

class PersonalAccount {
    private:
        int balance;
    public:
        PersonalAccount();
        void add(int value);
        void subtract(int value);
        void getBalance();
};

int main() {
    // test input here..
    int a, b;
    cin >> a >> b;

    // declare a object and it's balance is 0.
    PersonalAccount pa;

    // add a dollar into user's account..
    pa.add(a);

    // deduct b dollar from user's account..
    pa.subtract(b);

    // show balance..
    pa.getBalance();
}

PersonalAccount::PersonalAccount () {
    balance = 0;
}

// add money into user's account..
void PersonalAccount::add(int value) {
    balance += value;
}

// deduct money from user's account..
void PersonalAccount::subtract(int value) {
    if (balance - value >= 0) {
        balance -= value;
    } else {
        cout << "insufficient balance.." << endl;
    }
}

// get the account's balance..
void PersonalAccount::getBalance() {
    cout << "The Balance is: " << balance << endl;
}
在Java中,为了模拟银行账户的存取操作并保证线程安全,你可以使用`synchronized`关键字或者更现代的`java.util.concurrent.locks`包中的`ReentrantLock`等工具。下面是一个简单的例子: ```java public class BankAccount { private double balance; private Object lock = new Object(); // 同步方法 - 存款 public synchronized void deposit(double amount) { if (amount > 0) { System.out.println(Thread.currentThread().getName() + " 正在存款..."); try { Thread.sleep(1000); // 模拟处理时间 } catch (InterruptedException e) { e.printStackTrace(); } balance += amount; System.out.println("存款完成,当前余额:" + balance); } else { System.out.println("无效的存款金额"); } } // 同步方法 - 取款 public synchronized void withdraw(double amount) { if (amount > 0 && balance >= amount) { System.out.println(Thread.currentThread().getName() + " 正在取款..."); try { Thread.sleep(1000); // 模拟处理时间 } catch (InterruptedException e) { e.printStackTrace(); } balance -= amount; System.out.println("取款完成,当前余额:" + balance); } else { System.out.println("无法取款,余额不足或非法金额"); } } } // 主程序启动线程 public static void main(String[] args) { BankAccount account = new BankAccount(); Thread depositor = new Thread(() -> account.deposit(100)); Thread withdrawer = new Thread(() -> account.withdraw(50)); depositor.start(); // 开始存款线程 withdrawer.start(); // 开始取款线程 try { depositor.join(); withdrawer.join(); } catch (InterruptedException e) { e.printStackTrace(); } } ``` 在这个示例中,`deposit`和`withdraw`方法都被标记为`synchronized`,这意味着在同一时刻只有一个线程可以访问这些方法,防止了数据竞争。通过`lock`对象,我们可以控制对共享资源的操作顺序,确保银行账户的平衡总是准确的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值