Home > Java > javaTutorial > Steps and code examples to implement ATM simulation system in Java

Steps and code examples to implement ATM simulation system in Java

王林
Release: 2023-04-24 18:19:08
forward
1872 people have browsed it

1. Overview

(1) Topic selection analysis

(2) Development environment

Development environment, choose IDEA, the Java development software, based on JDK1.8 version, develop this ATM simulation program on the local window.

2. Program outline design

(1) Function module design

After analyzing the topic, this ATM simulation program is divided into two major parts: administrator side and user mode module. Among them, the administrator has the functions of querying all accounts, exporting all account information to files, and logging out. The user module has functions such as balance inquiry, ATM transfer, ATM deposit, ATM withdrawal, password modification, transaction record inquiry, export record, card refund, etc.

The total functional module diagram of the system is as follows:

Steps and code examples to implement ATM simulation system in Java

(2) Flowchart

The overall process of the system is: the user passes through the main interface Select administrator login or user mode login, and then enter the account and password set by the system to log in. After successful login, enter the corresponding main function page to perform related operations.

When the administrator logs in to the backend, determine whether the password and account are correct. If they are correct, log in, otherwise it will prompt failure. After logging in to the background, you can operate and view all user functions. Then the system will query all set user information and output it to the console panel. Click the export record function to output all account information to a txt file in the same directory as the current jar program.

When the user logs in, it is judged whether the account password is correct. If it is incorrect, the number of account errors is 1. When the number reaches 3, the account will be locked and cannot be logged in. When the entered account password is correct, it will be determined whether the account is locked. If not, log in, jump to the user's main interface, and perform related operations.

The flow chart of the administrator module is as follows:

Steps and code examples to implement ATM simulation system in Java

The overall flow chart of the user module is as follows:

Steps and code examples to implement ATM simulation system in Java

(3) File structure and class design

This ATM simulation program is developed using idea editing software. The project is divided into three packages: admin (administrator), customer (user), and data (ATM data).

The AdminManage class is written under the admin package, which is responsible for initializing the administrator interface and realizing all the functions of the administrator. The customer class under the Customer package implements the interface of the user module, defines relevant operation buttons, and implements respective monitoring functions. In order to reduce the design of the interface, an output information panel is used in the middle of the user interface, so that the functional results are printed in the panel. Come out and simulate the interface information of the ATM machine.

There are ATMData class, Card class and CustomerAccount class under the Data package. The Card class is the code representation of the user's bank card. It encapsulates the relevant information of the bank card into a basic Java object, which conforms to the object-oriented characteristics of Java. Among them, customerAccount is a subclass of the card class. Based on the card, many user behaviors are added, such as deposits, withdrawals, balance inquiries, etc. The ATMData class is a very important class for this program. It encapsulates all the initial account information of this program, as well as operations such as transfer, obtaining all accounts, and login. Designed in this way, data processing and interface design can be separated to avoid excessive and long user interface code.

The file structure is as shown below, in which the image information required by this program is stored in the resource folder:

Steps and code examples to implement ATM simulation system in Java

3. Detailed program design

The functional modules of this system will not be written in detail here. Several main functions are listed for description:

(1) Withdrawal function

The code for the withdrawal function is designed in the customerAccount class. The input parameter is the amount of withdrawal. First determine whether the amount is a multiple of 100, then determine whether it is greater than 5,000 (the question requires that a single withdrawal cannot be greater than 5,000), and then determine whether the balance of the current account is greater than or equal to the amount obtained. After these conditions are met, the amount of the current account is recalculated and the withdrawal record is added to the transaction record of the current account.

The core code logic is as follows:

if (money % 100 != 0) {
     return false;
}
if (money > 5000) {
  return false;
}
int currentMoney = getMoney();
if (currentMoney < money) {
  return false;
}
//取款
int result = currentMoney - money;
setMoney(result);
//添加交易记录
List<String> operationRecod = getOperationRecod();
operationRecod.add("【" + LocalDateTime.now().format(dateTimeFormatter) + "】 ATM 取款 [" + money + "]元,当前余额[" + result + "]元");
Copy after login

(2) Deposit function

The parameter of this method is the amount deposited. First, determine whether the amount is a multiple of 100. If the verification is passed, Recalculate the current account balance and then add transactions to the current account.

The core code logic is as follows:

 if (money % 100 != 0) {
 	return false;
}
//余额
int countMoney = getMoney() + money;
setMoney(countMoney);
//记录交易记录
List<String> operationRecod = getOperationRecod();
operationRecod.add("【" + LocalDateTime.now().format(dateTimeFormatter) + "】 ATM 存款 [" + money + "]元,当前余额[" + countMoney + "]元");
return true;
Copy after login

(3) User login function

The input parameters of the user login method are account and password. First, determine whether the account exists, and then enter and participate. All accounts set by the system are matched, if not, it fails. If so, determine whether the account is locked. If it is locked, you cannot log in. If it is not locked, determine whether the password is correct. If it is incorrect, the number of errors in the current account will be 1 (if the number of account errors = 3, the account will be locked). If the password is correct, the login will jump to the main interface successfully.

The core code is as follows:

//Whether an account exists

if (allAccount.containsKey(number)) {
  CustomerAccount account = allAccount.get(number);
  //判断账号是否被锁定
  if (account.getErrorCount() >= 3) {
    JOptionPane.showMessageDialog(null, "登录失败!该账号疑似已被锁定", "提示消息", JOptionPane.WARNING_MESSAGE);
    return null;
    //密码是否一致
  } else if (account.getPassword().equals(pwd)) {
    //重置错误次数
    account.setErrorCount(0);
    return account;
  }
  account.setErrorCount(account.getErrorCount() + 1);
  if (account.getErrorCount() >= 3) {
   //锁定账户
    account.setStatus(1);
  }
} else {
   //提示错误消息
  JOptionPane.showMessageDialog(null, "登录失败!卡号或密码错误", "提示消息", JOptionPane.WARNING_MESSAGE);
}
Copy after login

4. Program demonstration

There are many functions, so I won’t record them one by one here. Only tests for several main functions are recorded here.

(1) ATM withdrawal

Test the withdrawal function as shown in the figure below. The initial amount is 10,000 yuan. Withdraw 3000, 6000, and 5000 respectively. The last two are error data. A single withdrawal cannot exceed 5,000 yuan. Later, when the balance is 3,000, 5,000 will be withdrawn. The test withdrawal function cannot be overdrafted.

Test Results:

Steps and code examples to implement ATM simulation system in Java

Steps and code examples to implement ATM simulation system in Java

Steps and code examples to implement ATM simulation system in Java

Steps and code examples to implement ATM simulation system in Java

It can be seen that the function is normal, and the results of the legal and illegal data input tests are in line with expectations.

(2) ATM transfer

Transfer test, the target account must exist, and the balance must be greater than the amount to be transferred, and no overdraft is allowed. The test data is to enter a non-existent account and a transfer amount greater than the current balance.

Enter a non-existent account and the test results are as follows:

Steps and code examples to implement ATM simulation system in Java

#Enter a normal account and amount and the test results are as follows. At this time, log in to the target account and check the transaction records to see that the transfer has been completed Successfully reached the target account.

Steps and code examples to implement ATM simulation system in Java

Steps and code examples to implement ATM simulation system in Java

Enter error data that is greater than the current balance. The test results are as follows. It can be seen that the transfer function test is normal.

Steps and code examples to implement ATM simulation system in Java

(3) Modify password

You must enter the original password to modify the password. Only if it is the same can it be modified. The new password must be greater than 6 characters, and the 6 characters cannot be exactly the same. The password entered for the third time is to confirm the new password and must be the same as the password entered for the second time.

The original passwords are all 123456, and the incorrect data is 111111. The test is as follows:

Steps and code examples to implement ATM simulation system in Java

Steps and code examples to implement ATM simulation system in Java

Enter data less than 6 digits , and the test results of correctly inputting normal data are as follows:

Steps and code examples to implement ATM simulation system in Java

Steps and code examples to implement ATM simulation system in Java

The above is the detailed content of Steps and code examples to implement ATM simulation system in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template