优化一下,要求自定义顾客汇款次数和金额,每个顾客每次汇款金额相同
时间: 2024-03-21 10:44:41 浏览: 104
iestore网上商店系统v1.1.0
好的,以下是优化后的Java代码:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class BankAccount {
private double totalAmount;
private ArrayList<Customer> customers;
private Random random;
public BankAccount() {
totalAmount = 0.0;
customers = new ArrayList<Customer>();
random = new Random();
}
public void addCustomer(Customer customer) {
customers.add(customer);
}
public void simulate() {
ArrayList<Integer> indices = new ArrayList<Integer>();
for (int i = 0; i < customers.size(); i++) {
indices.add(i);
}
Collections.shuffle(indices, random);
for (int index : indices) {
Customer customer = customers.get(index);
for (int i = 0; i < customer.getNumOfTransactions(); i++) {
double amount = customer.getTransactionAmount();
totalAmount += amount;
}
}
System.out.println("Total amount: " + totalAmount);
}
public static void main(String[] args) {
BankAccount bankAccount = new BankAccount();
int numOfCustomers = 5;
int numOfTransactions = 3;
double transactionAmount = 100.0;
for (int i = 1; i <= numOfCustomers; i++) {
Customer customer = new Customer(i, numOfTransactions, transactionAmount);
bankAccount.addCustomer(customer);
}
bankAccount.simulate();
}
}
class Customer {
private int id;
private int numOfTransactions;
private double transactionAmount;
public Customer(int id, int numOfTransactions, double transactionAmount) {
this.id = id;
this.numOfTransactions = numOfTransactions;
this.transactionAmount = transactionAmount;
}
public int getId() {
return id;
}
public int getNumOfTransactions() {
return numOfTransactions;
}
public double getTransactionAmount() {
return transactionAmount;
}
}
```
现在,您可以自定义顾客的数量,每个顾客的汇款次数和金额。在这个程序中,我们将顾客的每次汇款金额设为100,汇款次数设为3。您可以根据需要修改这些值。
阅读全文