请写一段代码,模拟一个资金账户和明细( 操作用户,操作类型(收入,支出),操作金额,操作后金额,操作时间 ),被多个用户同时记账的过程
时间: 2024-04-12 08:30:58 浏览: 10
下面是一个简单的Java代码示例,演示了如何模拟一个资金账户和明细的记账过程,包括多个用户同时进行记账操作:
```java
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
class Account {
private String user;
private double balance;
public Account(String user, double balance) {
this.user = user;
this.balance = balance;
}
public synchronized void deposit(String user, double amount) {
double prevBalance = balance;
balance += amount;
logTransaction(user, "收入", amount, prevBalance, balance);
}
public synchronized void withdraw(String user, double amount) {
if (balance >= amount) {
double prevBalance = balance;
balance -= amount;
logTransaction(user, "支出", amount, prevBalance, balance);
} else {
System.out.println("余额不足,无法支出");
}
}
private void logTransaction(String user, String type, double amount, double prevBalance, double newBalance) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timestamp = sdf.format(new Date());
System.out.println("用户:" + user);
System.out.println("操作类型:" + type);
System.out.println("操作金额:" + amount);
System.out.println("操作前余额:" + prevBalance);
System.out.println("操作后余额:" + newBalance);
System.out.println("操作时间:" + timestamp);
System.out.println("------------------------");
}
}
class User implements Runnable {
private Account account;
private String name;
public User(Account account, String name) {
this.account = account;
this.name = name;
}
@Override
public void run() {
account.deposit(name, 100.0);
account.withdraw(name, 50.0);
}
}
public class AccountTransactionSimulation {
public static void main(String[] args) {
Account account = new Account("John", 0.0);
List<Thread> threads = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
User user = new User(account, "User " + i);
Thread thread = new Thread(user);
threads.add(thread);
thread.start();
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
在示例中,`Account`类表示一个资金账户,包含了存款和取款的方法,并且在进行操作时会记录操作明细。`User`类实现了`Runnable`接口,表示一个用户,可以通过创建`User`对象来模拟不同的用户进行账户操作。`AccountTransactionSimulation`类是程序的入口,创建了一个账户对象和多个用户线程,并且通过多线程的方式模拟了多个用户同时进行记账操作。
运行程序后,你会看到每个用户的操作明细以及账户的余额变化。请注意,由于使用了`synchronized`关键字来保证账户操作的原子性,因此在多线程环境下可以正确地进行记账操作。
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)