SoFunction
Updated on 2025-03-02

Java implements simple ATM project

This article shares the specific code for implementing simple ATM projects in Java for your reference. The specific content is as follows

The first thing to understand is that this ATM project itself is a lightweight project, just to complete some methods that ATM has, not to truly complete all the functions and requirements of an ATM.

Then in this lightweight ATM project, I will complete the basic functions of adding savings accounts, adding credit accounts, withdrawals, withdrawals and other basic functions.

Suitable for beginners to view, need to master the basic technical capabilities of Java inheritance, polymorphism, encapsulation, etc.

Then, first create the following object classes: Account (account class), Bank (bank class), CreditAccount (credit account), SavingAccount (saving account class);

First of all, you should understand the relationship between these class files and what methods should be used between each class;

Then let's fill in the Account class first

package ;
 
/**
  * Bank account category
  */
public abstract class Account {
 /**
  * Account number
  */
 private String accountId;
 /**
  * Account name
  */
 private String accountName;
 /**
  * Account password
  */
 private String accountPwd;
 /**
  * Account balance
  */
 private double accountBalance;
 /**
  * Account ID number
  */
 private String accountPersonId;
 /**
  * Account email
  */
 private String accountEmail;
 /**
  * Account contact number
  */
 private long accountTelno;
 
 public Account() {
 }
 
 public Account(String accountName, String accountPwd, String accountPersonId, long accountTelno,
 String accountEmail) {
  = accountName;
  = accountPwd;
  = accountPersonId;
  = accountTelno;
  = accountEmail;
 
 }
 
 public String getAccountId() {
 return accountId;
 }
 
 public void setAccountId(String accountId) {
  = accountId;
 }
 
 public String getAccountName() {
 return accountName;
 }
 
 public void setAccountName(String accountName) {
  = accountName;
 }
 
 public String getAccountPwd() {
 return accountPwd;
 }
 
 public void setAccountPwd(String accountPwd) {
  = accountPwd;
 }
 
 public double getAccountBalance() {
 return accountBalance;
 }
 
 public void setAccountBalance(double accountBalance) {
  = accountBalance;
 }
 
 public String getAccountPersonId() {
 return accountPersonId;
 }
 
 public void setAccountPersonId(String accountPersonId) {
  = accountPersonId;
 }
 
 public String getAccountEmail() {
 return accountEmail;
 }
 
 public void setAccountEmail(String accountEmail) {
  = accountEmail;
 }
 
 public long getAccountTelno() {
 return accountTelno;
 }
 
 public void setAccountTelno(long accountTelno) {
  = accountTelno;
 }
 
 /**
  * deposit
  *
  * @param money
  * Deposit amount
  * @return Return account balance
  */
 public double depoist(double money) {// money formal parameters if (money > 0)
  += money;
 return ;
 }
 
 /**
  * Withdrawal
  *
  * @param money
  * Withdrawal amount
  * @return Return account balance
  */
 public abstract double withdraw(double money);
 
 /**
  * transfer
  *
  * @param anotherAccount
  * The transfer of the other party's account
  * @param money
  * Transfer amount
  * @return Return the balance of the current account
  */
 public double tranferAccount(Account anotherAccount, double money) {// Formal parameters  += money;
  -= money;
 
 return ;
 }
 
}

Then fill in the credit account class CreditAccount; we should understand that it inherits the Account class, but it needs to have its own independent attributes, and we can add an attribute with the highest overdraft limit.
This way to implement the code

/**
  * Credit account
  *
  *
  */
public class CreditAccount extends Account {
 //Member variable private double maxOverdraw;//Maximum overdraft amount 
 //Constructor public CreditAccount(String accountName,String accountPwd,String accountPersonId,long accountTelno,String accountEmail,double maxOverdraw){
 super( accountName, accountPwd, accountPersonId, accountTelno, accountEmail);
  = maxOverdraw;
 }
 
 
 //set,get
 public void setMaxOverdraw(double maxOverdraw ){
  = maxOverdraw;
 }
 
 public double getMaxOverdraw(){
 return ;
 }
 
 
 @Override
 public double withdraw(double money) {
 // TODO Auto-generated method stub
 return 0;
 }
 
 
 
}

Similarly, fill in the SavingAccount file

package ;
/**
  * Savings Account
  *
  *
  */
public class SavingAccount extends Account {
 
 public SavingAccount(String accountName,String accountPwd,String accountPersonId,long accountTelno,String accountEmail){
 super( accountName, accountPwd, accountPersonId, accountTelno, accountEmail);
 }
 
 @Override
 public double withdraw(double money) {
 // TODO Auto-generated method stub
 if(money <= getAccountBalance()){
 
 }
 
 else
 ("Insufficient Account Balance");
 return getAccountBalance();
 
 }
 
 
}

The most important thing is to fill in the content of the Bank class. In this class, we need to complete the registration, generate bank accounts, count the sum of the highest overdraft limits of all credit accounts, count the total balances of all accounts, query the account with the highest overdraft limits among all credit accounts, query the account with the highest balance among all savings accounts, and other functions.

We fill in this

package ;
 
import ;
import ;
 
/**
  * Banking
  *
  * @author qianghj
  *
  * Bank Account Opening ----> Bank Account Account account = bank. Account Opening (...)
  *
  */
public class Bank {
 public Account[] accArray = new Account[2000];
 
 public int count = 0;// Indicates the number of bank accounts 
 /**
  * Open a bank account
  *
  * @param accName
  * User name
  * @param accPwd
  * User password
  * @param accPersonId
  * User ID card
  * @param accTelno
  * User mobile number
  * @param accEmail
  * User email
  * @param accountType
  * Account Type 0: Savings Account 1: Credit Account
  * @param maxOverdraw
  * Maximum overdraft amount for a credit account
  * @return Return to a valid bank account
  */
 public Account registAccount(String accName, String accPwd, String accPersonId, long accTelno, String accEmail,
 int accountType, double maxOverdraw) {
 Account account = null;
 if (accountType == 0)
 account = new SavingAccount(accName, accPwd, accPersonId, accTelno, accEmail);
 else
 account = new CreditAccount(accName, accPwd, accPersonId, accTelno, accEmail, maxOverdraw);
 
 (generateNextAccountId());
 accArray[count++] = account;
 return account;
 }
 /**
  * Generate bank account number
  *
  * @return Return to the next account number 1, 2, 3, 4
  */
 public String generateNextAccountId() {
 
 return "62223421" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
 
 }
 
 // Statistics the sum of the maximum overdraft limits of all credit accounts (1050) 2000, 1050 public double statisticsCreditAccountMaxoverdrawSum() {
 double sum = 0;
 for (int i = 0; i < count; i++) {
 // Determine whether the account is CreditAccount type if (accArray[i] instanceof CreditAccount) {
 CreditAccount creditAcc = (CreditAccount) accArray[i];
 sum += ();
 }
 }
 
 return sum;
 }
 
 // Statistics the total balance of all accounts public double aggregateAamount() {
 double sum = 0;
 for (int i = 0; i < count; i++) {
 if (accArray[i] instanceof SavingAccount) {
 SavingAccount savingAccount = (SavingAccount) accArray[i];
 sum += ();
 
 }
 }
 return sum;
 
 }
 
 // Inquiry into the account with the highest overdraft quota among all credit accounts public double maxLimit() {
 
 double tem = 0;
 for (int i = 0; i < count; i++) {
 if (accArray[i] instanceof CreditAccount) {
 CreditAccount creditAccount = (CreditAccount) accArray[i];
 
 if (() > tem) {
 tem = ();
 }
 
 }
 }
 return tem;
 
 }
 
 // Query the account with the highest balance among all savings accounts public double maxBalance() {
 
 double tem = 0;
 for (int i = 0; i < count; i++) {
 if (accArray[i] instanceof SavingAccount) {
 SavingAccount savingAccount = (SavingAccount) accArray[i];
 
 if (() > tem) {
 tem = ();
 }
 
 }
 }
 return tem;
 
 }
 
}

Final test class

package test;
 
import ;
 
import ;
import ;
import ;
 
public class TestAccount {
 
 @Test
 public void testRegist() {
 Bank bank = new Bank();
 
 for (int i = 0; i < 1000; i++) {
 // 0: Savings Account 1: Credit Account Account acc = ("tom" + i, "abc123", "2729382932", 183923302L, "tom" + i + "@",
 i % 2, (i % 2 == 0) ? 0 : 3000);
 if (i % 2 != 0) {
 CreditAccount creditAcc = (CreditAccount) acc;
 ("Name of all credit accounts:" + () + "And overdraft quota:" + ());
 }
 
 }
 
 // 1,000 bank accounts are opened, 500 is the credit account, the maximum overdraft limit is assigned random number, and then test it // double sum = bank.Calculate the sum of the maximum overdraft limit of all credit accounts (); double sum = ();
 ("The sum of the maximum overdraft limit for all credit accounts:" + sum);
 double sum1 = ();
 ("Total balance is" + sum1);
 }
 
}

I don’t write much about the test content, and you can test it yourself if you are interested. In this way, we completed a relatively simple ATM project. Hope it will be helpful to new scholars.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.