search
HomeJavajavaTutorialJava language implements basic operations of bank ATM system

#ATM system

##Function

Simulated bank ATM machine system, with registration and login functions
After logging in, the user can achieve the following functions:
1) Deposit 2 ) Withdrawal 3) Transfer 4) Inquiry 5) Exit

##Design ideas

First of all, to operate an ATM machine, you should have a bank card and an ATM, so we need to design an ATM class and Bankcard Class, ATM has operations such as deposits and withdrawals, and Bankcard is used to record user transactions after operations such as deposits and withdrawals. Secondly, because ATM serves multiple users at the same time, we should identify each card. Here, add a Bank class to record the bank The number of cards and which bank cards each user holds.

##Specific code

Bank class

import java.util.Arrays;

/**
 * @author:zl
 * @Date 22:30 2020/10/9
 */
 
public class Bank {
    private BankCard[] cards;
    private int size;// 有效用户个数
    private static final int INITSIZE  =10;
    public Bank(){
        this(INITSIZE);
    }
    public Bank(int num){
        cards = new BankCard[num];
    }
    public boolean add(BankCard card){//如果注册过,增加失败
        if(contains(card.getID(),card.getPasswd())!=null){
            return false;
        }
        if(size==cards.length){
            cards= Arrays.copyOf(cards,cards.length>>1);//长度不够时进行扩容
        }
        cards[size++] = card;//
        return true;
    }
    public boolean search(int id,int passwd){//查询是否存在这张卡
        boolean k=true;
        for(int i=0;i<size;i++){
            if((cards[i].getID()==id)&&(cards[i].getPasswd()==passwd)){
                k=true;
            }
            else k=false;
        }
        return k;
    }
    public BankCard contains(int id,int passwd){//查询并返回这张卡
        BankCard card=null;
        for(int i=0;i<size;i++){
            if((cards[i].getID()==id)&&(cards[i].getPasswd()==passwd)){
                card=cards[i];
                break;
            }
        }
        return card;
    }

}

Bankcard class

import java.util.Scanner;

public class BankCard {
    private int id;
    private int passwd;
    public int money=0;
    public  BankCard(int id,int passwd){
        this.id=id;
        this.passwd=passwd;
    }
    public int getID(){
        return this.id;
    }
    public int getPasswd(){
        return this.passwd;
    }
    /**
     * 存款
     */
    public void saveMoney(int money){
       this.money+=money;
    }
    /**
     * 取款
     */
    public boolean withDraw(int money){
        boolean flag=false;
       if(this.money>=money){
           this.money-=money;
           flag=true;
       }
            return flag;
    }
    /**
     * 返回余额
     * @return
     */
    public int  getMoney(){
        return this.money;
    }
}

ATM class###
import java.util.Scanner;
import src3.BankCard;

public class ATM {
    private Bank bank;
    private static Scanner scanner;
    public ATM(){
        bank = new Bank();
        scanner = new Scanner(System.in);
    }

    public void start(){
        while (true) {
            System.out.println("1. 登陆 2.注册 3.关机");
            int chioce = scanner.nextInt();
            if (chioce == 3) {
                break;
            }
            System.out.println("请输入账号密码");
            int id = scanner.nextInt();
            int passwd = scanner.nextInt();
            BankCard successCard = null;
            switch (chioce){
                case 1:
                    successCard = login(id,passwd);
                    if(successCard != null){
                        System.out.println("登陆成功");
                        loginSuccess(successCard);
                    }else{
                        System.out.println("登陆失败");
                    }
                    break;
                case 2:
                    if(regiter(id,passwd)){
                        System.out.println("注册成功");
                    }else{
                        System.out.println("注册失败");
                    }
                    break;
            }
        }
    }
    private void loginSuccess(BankCard successCard){
        while (true) {
            System.out.println("1. 存款 2.取款 3.转账 4.余额 5.退卡");
            int chioce = scanner.nextInt();
            if (chioce == 5) {
                break;
            }
            switch (chioce) {
                case 1:
                    System.out.println("请输入存款金额");
                    int money = scanner.nextInt();
                    successCard.saveMoney(money);
                    System.out.println("存款成功");
                    break;
                case 2:
                    System.out.println("请输入取款金额");
                    money = scanner.nextInt();
                    if (successCard.withDraw(money)) {
                        System.out.println("取款成功");
                    } else {
                        System.out.println("取款失败");
                    }
                    break;
                case 3:
                    System.out.println("请输入转账金额");
                    money = scanner.nextInt();
                    System.out.println("请输入转账用户账号,密码");
                    int id = scanner.nextInt();
                    int passwd = scanner.nextInt();
                    BankCard userCard = bank.contains(id, passwd);
                    if (userCard != null) {
                        if (successCard.withDraw(money)) {// 当前卡取款成功
                            userCard.saveMoney(money);
                            System.out.println("转账成功");
                        } else {
                            System.out.println("余额不足");
                        }
                    } else {
                        System.out.println("没有此用户");
                    }
                    break;
                case 4:
                    System.out.println("余额:" + successCard.getMoney());
                    break;
            }
        }
    }

    private boolean regiter(int id,int passwd){
        BankCard card = new BankCard(id,passwd);
        return bank.add(card);
    }
    private BankCard login(int id,int passwd){
        return bank.contains(id,passwd);
    }
}
# #####Test class###
public class TestDemo {
    public static void main(String[] args) {
        ATM atm = new ATM();
        atm.start();
    }
}

The above is the detailed content of Java language implements basic operations of bank ATM system. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Why can't JavaScript directly obtain hardware information on the user's computer?Why can't JavaScript directly obtain hardware information on the user's computer?Apr 19, 2025 pm 08:15 PM

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

Circular dependencies appear in the RuoYi framework. How to troubleshoot and solve the problem of dynamicDataSource Bean?Circular dependencies appear in the RuoYi framework. How to troubleshoot and solve the problem of dynamicDataSource Bean?Apr 19, 2025 pm 08:12 PM

RuoYi framework circular dependency problem troubleshooting and solving the problem of circular dependency when using RuoYi framework for development, we often encounter circular dependency problems, which often leads to the program...

When building a microservice architecture using Spring Cloud Alibaba, do you have to manage each module in a parent-child engineering structure?When building a microservice architecture using Spring Cloud Alibaba, do you have to manage each module in a parent-child engineering structure?Apr 19, 2025 pm 08:09 PM

About SpringCloudAlibaba microservices modular development using SpringCloud...

Treatment of x² in curve integral: Why can the standard answer be ignored (1/3) x³?Treatment of x² in curve integral: Why can the standard answer be ignored (1/3) x³?Apr 19, 2025 pm 08:06 PM

Questions about a curve integral This article will answer a curve integral question. The questioner had a question about the standard answer to a sample question...

What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot?What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot?Apr 19, 2025 pm 08:03 PM

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

Why can't the main class be found after copying and pasting the package in IDEA? Is there any solution?Why can't the main class be found after copying and pasting the package in IDEA? Is there any solution?Apr 19, 2025 pm 07:57 PM

Why can't the main class be found after copying and pasting the package in IDEA? Using IntelliJIDEA...

Java multi-interface call: How to ensure that interface A is executed before interface B is executed?Java multi-interface call: How to ensure that interface A is executed before interface B is executed?Apr 19, 2025 pm 07:54 PM

State synchronization between Java multi-interface calls: How to ensure that interface A is called after it is executed? In Java development, you often encounter multiple calls...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.