使用java编写ATM系统程序并给出代码 1.用户登录注册 2.用户存款 3.用户查询余额(查询账户余额和操作记录) 4.用户取款 5.用户转账功能
时间: 2024-03-08 09:46:54 浏览: 160
java语言编写的ATM系统
5星 · 资源好评率100%
好的,下面是一个简单的Java编写ATM系统程序的示例代码。
```java
import java.util.ArrayList;
import java.util.Scanner;
class User {
private String name;
private String account;
private String password;
private double balance;
private ArrayList<String> records;
public User(String name, String account, String password, double balance) {
this.name = name;
this.account = account;
this.password = password;
this.balance = balance;
this.records = new ArrayList<>();
}
public String getName() {
return name;
}
public String getAccount() {
return account;
}
public String getPassword() {
return password;
}
public double getBalance() {
return balance;
}
public ArrayList<String> getRecords() {
return records;
}
public void deposit(double amount) {
balance += amount;
records.add("Deposit: " + amount);
}
public void withdraw(double amount) {
balance -= amount;
records.add("Withdraw: " + amount);
}
public boolean transfer(User receiver, double amount) {
if (balance >= amount) {
balance -= amount;
receiver.deposit(amount);
records.add("Transfer: " + amount + " to " + receiver.getAccount());
return true;
} else {
return false;
}
}
}
class LoginRegister {
private ArrayList<User> users;
public LoginRegister() {
this.users = new ArrayList<>();
}
public void register() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your account: ");
String account = scanner.nextLine();
System.out.print("Enter your password: ");
String password = scanner.nextLine();
System.out.print("Enter your initial balance: ");
double balance = scanner.nextDouble();
User user = new User(name, account, password, balance);
users.add(user);
System.out.println("Registration successful!");
}
public User login() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your account: ");
String account = scanner.nextLine();
System.out.print("Enter your password: ");
String password = scanner.nextLine();
for (User user : users) {
if (user.getAccount().equals(account) && user.getPassword().equals(password)) {
System.out.println("Login successful!");
return user;
}
}
System.out.println("Invalid account or password!");
return null;
}
}
class Query {
public static void queryBalance(User user) {
System.out.println("Name: " + user.getName());
System.out.println("Account: " + user.getAccount());
System.out.println("Balance: " + user.getBalance());
}
public static void queryRecords(User user) {
System.out.println("Name: " + user.getName());
System.out.println("Account: " + user.getAccount());
System.out.println("Records: ");
for (String record : user.getRecords()) {
System.out.println(record);
}
}
}
class Deposit {
public static void deposit(User user) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the deposit amount: ");
double amount = scanner.nextDouble();
user.deposit(amount);
System.out.println("Deposit successful!");
}
}
class Withdrawal {
public static void withdraw(User user) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the withdrawal amount: ");
double amount = scanner.nextDouble();
if (user.getBalance() >= amount) {
user.withdraw(amount);
System.out.println("Withdrawal successful!");
} else {
System.out.println("Insufficient balance!");
}
}
}
class Transfer {
public static void transfer(User user) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the recipient's account: ");
String account = scanner.nextLine();
User receiver = null;
for (User u : Main.users) {
if (u.getAccount().equals(account)) {
receiver = u;
break;
}
}
if (receiver == null) {
System.out.println("Invalid account!");
return;
}
System.out.print("Enter the transfer amount: ");
double amount = scanner.nextDouble();
if (user.transfer(receiver, amount)) {
System.out.println("Transfer successful!");
} else {
System.out.println("Insufficient balance!");
}
}
}
public class Main {
public static ArrayList<User> users = new ArrayList<>();
public static LoginRegister loginRegister = new LoginRegister();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Welcome to ATM system!");
System.out.println("1. Register");
System.out.println("2. Login");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
loginRegister.register();
break;
case 2:
User user = loginRegister.login();
if (user != null) {
while (true) {
System.out.println("Welcome, " + user.getName() + "!");
System.out.println("1. Query balance");
System.out.println("2. Query records");
System.out.println("3. Deposit");
System.out.println("4. Withdrawal");
System.out.println("5. Transfer");
System.out.println("6. Logout");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
Query.queryBalance(user);
break;
case 2:
Query.queryRecords(user);
break;
case 3:
Deposit.deposit(user);
break;
case 4:
Withdrawal.withdraw(user);
break;
case 5:
Transfer.transfer(user);
break;
case 6:
System.out.println("Logout successful!");
break;
default:
System.out.println("Invalid choice!");
break;
}
if (choice == 6) {
break;
}
}
}
break;
case 3:
System.out.println("Goodbye!");
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
break;
}
}
}
}
```
这个示例程序包含了用户登录注册、用户存款、用户查询余额、用户取款、用户转账等功能,并且使用了面向对象的思路设计程序。当然,这只是一个简单的示例程序,具体的实现方式还需要根据你的需求和具体情况来进行调整和完善。
阅读全文