JAVA假设某一银行账户,它可接受顾客的汇款,每做一次汇款,便可计算出汇款的总额。现有多个顾客,每人分多次将钱(每个顾客汇入的时间和钱数不一样)汇入到该银行账户。试编写一个程序,模拟实际作业。
时间: 2023-08-31 07:28:20 浏览: 91
### 回答1:
以下是一个简单的Java程序,实现了对银行账户的模拟操作。程序中定义了一个BankAccount类,包含了账户余额和汇款总额两个属性,以及接受汇款和计算汇款总额的方法。
```java
import java.util.Scanner;
public class BankAccount {
private double balance;
private double total;
public BankAccount(double initialBalance) {
balance = initialBalance;
total = initialBalance;
}
public void deposit(double amount) {
balance += amount;
total += amount;
System.out.println("成功汇入 " + amount + " 元,当前余额为 " + balance + " 元,汇款总额为 " + total + " 元。");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入账户初始余额:");
double initialBalance = scanner.nextDouble();
BankAccount account = new BankAccount(initialBalance);
while (true) {
System.out.print("请输入汇款金额(输入0退出):");
double amount = scanner.nextDouble();
if (amount == 0) {
break;
}
account.deposit(amount);
}
System.out.println("程序已退出。");
}
}
```
在程序中,首先通过Scanner类获取用户输入的账户初始余额,并创建一个BankAccount对象。然后进入循环,每次获取用户输入的汇款金额,调用BankAccount对象的deposit方法进行汇款操作,直到用户输入0退出循环。最后输出程序已退出的提示信息。
### 回答2:
可以使用Java编写一个程序来模拟银行账户的汇款操作。首先,我们需要创建一个银行账户类,其中包括账户名、账号和汇款总额属性。
```java
public class BankAccount {
private String accountName;
private String accountNumber;
private double totalAmount;
public BankAccount(String name, String number) {
accountName = name;
accountNumber = number;
totalAmount = 0.0;
}
public String getAccountName() {
return accountName;
}
public String getAccountNumber() {
return accountNumber;
}
public double getTotalAmount() {
return totalAmount;
}
public void deposit(double amount) {
totalAmount += amount;
}
public void withdraw(double amount) {
if (totalAmount >= amount) {
totalAmount -= amount;
} else {
System.out.println("Insufficient funds.");
}
}
}
```
接下来,我们可以创建一个顾客类,用于记录每个顾客的汇款信息,包括汇款时间和汇款金额。
```java
import java.util.Date;
public class Customer {
private String name;
private double amount;
private Date date;
public Customer(String name, double amount) {
this.name = name;
this.amount = amount;
date = new Date();
}
public String getName() {
return name;
}
public double getAmount() {
return amount;
}
public Date getDate() {
return date;
}
}
```
最后,我们可以编写一个主程序来模拟多个顾客的汇款操作。
```java
public class BankSimulation {
public static void main(String[] args) {
BankAccount account = new BankAccount("John Doe", "1234567890");
Customer customer1 = new Customer("Customer 1", 1000);
Customer customer2 = new Customer("Customer 2", 2000);
Customer customer3 = new Customer("Customer 3", 3000);
account.deposit(customer1.getAmount());
account.deposit(customer2.getAmount());
account.deposit(customer3.getAmount());
System.out.println("Account Name: " + account.getAccountName());
System.out.println("Account Number: " + account.getAccountNumber());
System.out.println("Total Amount: " + account.getTotalAmount());
System.out.println("");
System.out.println("Customer 1: " + customer1.getAmount() + " deposited at " + customer1.getDate());
System.out.println("Customer 2: " + customer2.getAmount() + " deposited at " + customer2.getDate());
System.out.println("Customer 3: " + customer3.getAmount() + " deposited at " + customer3.getDate());
}
}
```
以上代码模拟了一个银行账户接收顾客汇款的场景,每个顾客的汇款金额和时间都不同。程序输出了账户的总金额以及每个顾客的汇款信息。
这个程序可以根据实际需求进行扩展,例如添加提款功能、支持多个账户等。
### 回答3:
下面是一个使用Java编写的程序,用于模拟多个顾客向银行账户汇款的场景:
```java
import java.util.Scanner;
public class BankAccountSimulation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double totalAmount = 0.0; // 汇款的总额
while (true) {
System.out.println("请输入汇款人姓名:");
String name = scanner.nextLine();
System.out.println("请输入汇款金额:");
double amount = scanner.nextDouble();
scanner.nextLine(); // 清除输入的换行符
totalAmount += amount; // 更新汇款的总额
System.out.println(name + "成功汇款:" + amount + "元");
System.out.println("当前汇款总额为:" + totalAmount + "元");
System.out.println("是否继续汇款?(Y/N)");
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("N")) {
break;
}
}
System.out.println("汇款结束。最终汇款总额为:" + totalAmount + "元");
}
}
```
这个程序使用了`Scanner`类来接收用户的输入。首先,它提示用户输入汇款人的姓名和金额,然后将金额加到`totalAmount`变量中。之后,程序会询问用户是否继续汇款,如果用户输入"N",则跳出循环。最后,程序打印出最终汇款的总额。
你可以根据需要进行修改和调整,比如可以添加更多的错误处理和数据验证的功能。希望对你有帮助!
阅读全文