用java写一个银行存取款系统
时间: 2024-01-01 21:05:51 浏览: 134
要用Java编写一个银行存取款系统,你可以使用面向对象的编程思想来实现。首先,你可以创建一个BankAccount类来代表银行账户。这个类可以包含账户的属性,比如账户号码和余额,以及相关的方法,如存款、取款和查询余额。在存款和取款方法中,你可以根据用户输入的金额来更新账户的余额。
在程序的主函数中,你可以通过创建一个BankAccount对象来管理用户的账户。你可以使用循环和条件语句来实现一个简单的用户界面,让用户选择不同的操作类型,并根据用户的选择执行相应的操作。
以下是一个简单的示例代码,用于实现一个银行存取款系统:
```java
import java.util.Scanner;
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber) {
this.accountNumber = accountNumber;
this.balance = 0.0;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient balance.");
}
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BankAccount account = new BankAccount("123456789");
int choice;
do {
System.out.println("Bank Account Management System");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Check Balance");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the deposit amount: ");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
System.out.println("Deposit successful.");
break;
case 2:
System.out.print("Enter the withdrawal amount: ");
double withdrawalAmount = scanner.nextDouble();
account.withdraw(withdrawalAmount);
System.out.println("Withdrawal successful.");
break;
case 3:
System.out.println("Current balance: " + account.getBalance());
break;
case 0:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
} while (choice != 0);
scanner.close();
}
}
```
阅读全文
相关推荐



















