欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA基础模拟ATM系统

程序员文章站 2024-02-27 20:48:45
...

ATM系统

题目

JAVA基础模拟ATM系统

封装类

ATM类

package com.cpd.atmTest.entity;

import com.cpd.atmTest.entity.Card;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class Atm {
    private String name; // 名字
    private String num; // 编号
    private boolean type; // 是否继续循环

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public boolean isType() {
        return type;
    }

    public void setType(boolean type) {
        this.type = type;
    }

}

Card类

package com.cpd.atmTest.entity;

public class Card {
    private String account;  //账号
    private String password; //密码
    private double balance;  //余额
    private String bankName; //银行卡名字
    private String name;     //持卡人姓名

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public String getBankName() {
        return bankName;
    }

    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Card(String account, String password, double balance, String bankName, String name) {
        this.account = account;
        this.password = password;
        this.balance = balance;
        this.bankName = bankName;
        this.name = name;
    }

    public Card() {
    }

    @Override
    public String toString() {
        return "Card{" +
                "account='" + account + '\'' +
                ", password='" + password + '\'' +
                ", balance=" + balance +
                ", bankName='" + bankName + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

ATM的接口方法

package com.cpd.atmTest.dao;

import com.cpd.atmTest.entity.Card;

public interface AtmDao {

    //1.余额查询  inquireMoney
    public void inquireMoney();
    //2.取款 takeMoney
    public void takeMoney();
    //3.存款 saveMoney
    public void saveMoney();
    //4.转账 turnMoney
    public void turnMoney();
    //5.打印 print
    public void print();
    //6.退卡 checkCard
    public void checkCard();
    //7.查询转账信息 queryTurnI
    public void queryTurnI();
}

ATM的接口方法实现ATMimpl

package com.cpd.atmTest.impl;

import com.cpd.atmTest.entity.CardInfo;
import com.cpd.atmTest.dao.AtmDao;
import com.cpd.atmTest.entity.Card;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class AtmDaoImpl implements AtmDao{
    // 获取银行卡信息的List,Map,Set集合
    private List<Card> cardList = null;
    private Map<String, Card> cardMap = null;
    private Set<Card> cardSet = null;
    private Card card = null;
    // ATM机构造方法
    public AtmDaoImpl() {
        cardInfo();
    }
    //存储账号所有操作信息
    List<String> AllIn=new ArrayList<>();
    //存储转账信息
    List<String> OutIn=new ArrayList<>();
    //扫描仪对象
    Scanner scanner = new Scanner(System.in);
    // 初始化银行卡信息
    public void cardInfo() {
        CardInfo cardInfo = new CardInfo();
        cardList = cardInfo.getCardInfo();
        cardMap = cardInfo.getCardInfoMap();
        cardSet = cardInfo.getCardInfoSet();
    }
    public void login(){
        if(card!=null){
            //显示菜单
            showMenu();
        }
        System.out.println("请输入银行卡账号:");
        String inaccount=scanner.next();
        boolean cz=true;
        //判断卡号是否存在
        for (String key : cardMap.keySet()) {
            if(inaccount.equals(key)){
                card=cardMap.get(key);
                System.out.println("请输入银行卡密码:");
                String inpassword=scanner.next();
                cz=false;
                //判断密码是否正确
                if(inpassword.equals(card.getPassword())){
                    //将登录信息存入Alllist
                    String str="户主:"+card.getName()+"   账号:"+card.getAccount();
                    AllIn.add(str);
                    OutIn.add(str);
                    //显示菜单
                    showMenu();
                }else {
                    System.out.println("密码错误,请重新输入");
                    login();
                }
            }else{//重新登录
                continue;
            }
        }
        if (cz){
            System.out.println("账号不存在");
            login();
        }
    }

    // 显示主菜单
    public void showMenu() {
        // 4、显示主菜单。
        System.out.println("***********************************");
        System.out.println("*******请输入你想要操作类型*******");
        System.out.println("******1.余额查询  2.取款***********");
        System.out.println("******3.存款        4.转账***********");
        System.out.println("******5.打印        6.退卡***********");
        System.out.println("******7.查询转账信息**************");
        System.out.println("**********************************");
    }
    @Override
    public void inquireMoney() {
        //将当前操作存入Alllist
        String str="查询余额————>当前账户余额查询"+card.getBalance();
        AllIn.add(str);
        System.out.println("当前账户余额查询"+card.getBalance());
    }

    @Override
    public void takeMoney() {
        System.out.println("请输入取款金额:");
        double outMoney=scanner.nextDouble();
        if(outMoney>=0&&outMoney<=card.getBalance()){
            card.setBalance(card.getBalance()-outMoney);
            //将当前操作存入Alllist
            String str="取款————>取款前:"+(card.getBalance()+outMoney)+"  取款:"+outMoney+"  当前账户余额查询:"+card.getBalance();
            AllIn.add(str);
            System.out.println("当前账户余额查询"+card.getBalance());
        }else {
            System.out.println("取款金额错误或余额不足:");
            takeMoney();
        }
    }

    @Override
    public void saveMoney() {
        System.out.println("请输入存款金额:");
        double inMoney=scanner.nextDouble();
        if(inMoney>=0){
            card.setBalance(card.getBalance()+inMoney);
            //将当前操作存入Alllist
            String str="存款————>存款前:"+(card.getBalance()-inMoney)+"  存款:"+inMoney+"  当前账户余额查询:"+card.getBalance();
            AllIn.add(str);
            System.out.println("当前账户余额查询"+card.getBalance());
        }else {
            System.out.println("存款金额输入错误:");
            saveMoney();
        }
    }

    @Override
    public void turnMoney() {
        System.out.println("请输入转账银行卡账号:");
        String turnaccount=scanner.next();
        boolean cz=true;
        if(turnaccount.equals(card.getAccount())){
            System.out.println("不能给自己转账!!!!");
            cz=false;
            turnMoney();
        }else {
            //判断卡号是否存在
            for (String key : cardMap.keySet()) {
                if(turnaccount.equals(key)){
                    Card turncard=cardMap.get(key);
                    System.out.println("请输入转账金额:");
                    double turnMoney=scanner.nextDouble();
                    cz=false;
                    //判断密码是否正确
                    if(turnMoney>=0&&turnMoney<=card.getBalance()){
                        card.setBalance(card.getBalance()-turnMoney);
                        turncard.setBalance(turncard.getBalance()+turnMoney);
                        //将当前操作存入Alllist ,OUtin
                        String str="转账————>转账前:"+(card.getBalance()+turnMoney)+"  转账:"+turnMoney+"  当前账户余额查询:"+card.getBalance()+"  转账户主:"+turncard.getName()+"  转账账户:"+turncard.getAccount();
                        AllIn.add(str);
                        OutIn.add(str);
                        System.out.println("当前账户余额查询"+card.getBalance());
                    }else {
                        System.out.println("转账金额错误或余额不足:");
                        turnMoney();
                    }
                }else{//重新登录
                    continue;
                }
            }
        }
        if (cz){
            System.out.println("账号不存在");
            turnMoney();
        }
    }

    @Override
    public void print() {
        System.out.println("文件存放路径:C:\\m.txt");
        // 打开一个文件
        File file = new File("C:\\m.txt");
        if (file.exists()) {//判断文件是否存在,存在删除
            file.delete();
        }

        try {
            FileWriter fw = new FileWriter(file);;
           for (String str:AllIn){
               fw.write(str+"\n");
               fw.write("--------------------------------------"+"\n");
               fw.flush();
           }

        } catch (IOException e) {

            e.printStackTrace();
        }
    }

    @Override
    public void checkCard() {
        System.out.println("当前账户信息:"+card.toString());
        System.out.println("谢谢您的使用,欢迎下次光临!!!");
    }

    @Override
    public void queryTurnI() {
        // 使用Map循环显示用户信息
        System.out.println("-----------------------");
        for (String str : cardMap.keySet()) {
            Card card = cardMap.get(str);
            System.out.println(card.getName() + "\t" + card.getAccount()+ "\t" + card.getPassword() + "\t" + card.getBalance()+ "\t" + card.getBankName());

        }
        System.out.println("------当前用户转账信息----");
        for (String str : OutIn) {
            System.out.println(str);
        }
        if (OutIn.size()==1){
            System.out.println("当前用户无转账信息!!!!");
        }
        System.out.println("-----------------------");
    }
}

Person测试类

package com.cpd.atmTest.test;

import com.cpd.atmTest.entity.Card;
import com.cpd.atmTest.impl.AtmDaoImpl;

import java.util.Scanner;

public class Person {
    public static void main(String[] args) {
        //欢迎登录
        System.out.println("---欢迎登录ATM系统---");
        AtmDaoImpl atmDao = new AtmDaoImpl();
        Scanner scanner = new Scanner(System.in);
        boolean type=true;
//调用登录方法
        atmDao.login();
        while (type){

            System.out.println("---请选择你想要操作类型*---");
            int choose=scanner.nextInt();
            switch (choose){
                case 1:
                    atmDao.inquireMoney();
                    atmDao.showMenu();
                    break;
                case 2:
                    atmDao.takeMoney();
                    atmDao.showMenu();
                    break;
                case 3:
                    atmDao.saveMoney();
                    atmDao.showMenu();
                    break;
                case 4:
                    atmDao.turnMoney();
                    atmDao.showMenu();
                    break;
                case 5:
                    atmDao.print();
                    atmDao.showMenu();
                    break;
                case 6:
                    atmDao.checkCard();
                    type=false;
                    break;
                case 7:
                    atmDao.queryTurnI();
                    atmDao.showMenu();
                    break;
                default:
                    System.out.println("default");
                    break;
            }
        }
    }
}

代码实现展示

JAVA基础模拟ATM系统

相关标签: ATM系统 java