经典案例题:ATM取款机详细代码---复制既能跑

本文介绍了Java经典案例题供小白编程爱好者借鉴,因为我也是学习者,这是我的一点编程经验和心得,如有错误,请指出。废话不多说,开始步入正题。(ps:创作不易,有喜欢的小伙伴请点赞支持,谢谢啦!)

这里写目录标题

  • 一、所需知识
  • 二、需求及技术
  • 三、代码
    • 1. 实体类:Account
    • 2. 服务层:AccountService
    • 3. 视图层:AccountService
    • 4. 测试类:Test
    • 5. 结果截图

一、所需知识

此系统要求掌握知识点大致如下:
1、掌握控制台项目的开发流程
2、了解项目所需的技术
3、熟悉项目需实现的功能
3、掌握项目登陆注册的功能实现
4、掌握项目余额的功能实现
5、掌握项目存款转账的功能实现
6、掌握项目分层实现

二、需求及技术

需求: ATM系统是一个基于JAVA SE的控制台版本的综合项目,具有登录、注册、余额、存款、退出以及转账功能。
技术:- JAVA基础 - 面向对象 - 分层

三、代码

分析:分层:实体类层-Jopo -----服务层-service------视图层-view-------测试-test
通俗点说,就是创建三个包对应上面三个层,以下就是下面三个包的具体代码

1. 实体类:Account

/*
账户实体类
 */
public class Account {
     
    private String uname;   //用户名
    private String pwd;     //密码
    private int idcard;     //卡号
    private double blance; //余额

    public Account() {
     
    }

    public Account(String uname, String pwd, int idcard, double blance) {
     
        this.uname = uname;
        this.pwd = pwd;
        this.idcard = idcard;
        this.blance = blance;
    }

    public String getUname() {
     
        return uname;
    }

    public void setUname(String uname) {
     
        this.uname = uname;
    }

    public String getPwd() {
     
        return pwd;
    }

    public void setPwd(String pwd) {
     
        this.pwd = pwd;
    }

    public int getIdcard() {
     
        return idcard;
    }

    public void setIdcard(int idcard) {
     
        this.idcard = idcard;
    }

    public double getBlance() {
     
        return blance;
    }

    public void setBlance(double blance) {
     
        this.blance = blance;
    }

    @Override
    public String toString() {
     
        return "Account{" +
                "uname='" + uname + '\'' +
                ", pwd='" + pwd + '\'' +
                ", idcard=" + idcard +
                ", blance=" + blance +
                '}';
    }

    @Override
    public boolean equals(Object o) {
     
        if (o instanceof Account){
     
            if (((Account) o).getUname().equals(uname)){
     
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public int hashCode() {
     
        return 0;
    }
}

2. 服务层:AccountService

import com.yb.item.jopo.Account;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;

/*
账户服务层、逻辑判断
 */
public class AccountService {
     
    private static Account account = new Account();
    private static double b = 0;

    //数据存储
    private static HashSet<Account> al = new HashSet<Account>();

    //注册
    public static void register(Account accounts){
     
        boolean rs = al.add(accounts);
        if (rs){
     
            System.out.println("注册成功");
            //生成一个随机卡号
            int card = createCard();
            accounts.setIdcard(card);
            account = accounts;
            System.out.println(account);
        }else{
     
            System.out.println("注册失败");
        }
    }

    //登录
    public static boolean login(Account account){
     
        Iterator<Account> iterator = al.iterator();
        while (iterator.hasNext()){
     
            Account next = iterator.next();
            if (account.getIdcard()==next.getIdcard()){
     
                if (account.getPwd().equals(next.getPwd())){
     
                    System.out.println("登录成功");
                    account=next;
                    b = next.getBlance();
                    System.out.println(next);
                    return true;
                }else{
     
                    System.out.println("密码错误...");
                    return false;
                }
            }
            if (account.getIdcard()==next.getIdcard()){
     
                System.out.println("卡号错误...");
                    return false;
            }
        }
        return false;
    }

    //存款
    public static void deposit(double money){
     
        if (money>0){
     
            b = account.getBlance()+money;
            account.setBlance(b);
        }else {
     
            System.out.println("你这给我卡Bug呢!!");
        }
    }

    //查询余额
    public static void CheckBalance(){
     
        System.out.println("您的余额为:"+b);

    }

    //转账
    public static void carryOver(int id,double money){
     
        Iterator<Account> iterator = al.iterator();
        while (iterator.hasNext()){
     
            Account next = iterator.next();
            if (next.getIdcard()==id){
     
                if (money>account.getBlance()){
     
                    System.out.println("转账失败...");
                }else{
     
                    //自己的钱减少
                    b=account.getBlance()-money;
                    account.setBlance(b);
                    //他的钱变多
                    b = next.getBlance()+money;
                    next.setBlance(b);
                }
                System.out.println("您转入的账户:"+next.getUname()+",余额为:"+next.getBlance());
                //将账户重新赋值
                b = next.getBlance();

            }
        }
        System.out.println("您的账户余额为:"+account.getBlance());
    }

    //取款
    public static void getMoney(double money){
     
        if (account.getBlance()>=money&&money>0&&account.getBlance()>=0){
     
            account.setBlance(account.getBlance()-money);
            System.out.println("取款成功,当前余额为:"+account.getBlance());
        }else {
     
            System.out.println("取款失败");
        }
    }



    //自动生成银行卡号
    public static int createCard(){
     
        Random random = new Random();
        //可以追加判断,如果重复生成,则重新生成一个随机卡号
        int num = random.nextInt(100000000)+999999999;
        return num;
    }

}

3. 视图层:AccountService

import com.yb.item.jopo.Account;
import com.yb.item.service.AccountService;

import java.util.Scanner;

public class AccountView {
     

    private static Scanner scanner = new Scanner(System.in);
    private static AccountService as  = new AccountService();
    private static AccountView av  = new AccountView();

        //菜单
         public static void menu(String[] a){
     
             System.out.println("--------------------------------------------------------------");
                for (int i = 0; i <= a.length-1; i++) {
     
                    System.out.print((i+1)+","+a[i]+"  ");
                }
                System.out.println();
             System.out.println("--------------------------------------------------------------");
            }

         //获取对象
         public static void getUser(){
     

         }


        //首页
         public static void index(){
     
             //new String[]{"注册","登录","存款","余额","转账","取款","退出"}

             while (true) {
     
                 menu(new String[]{
     "注册","登录","退出"});
                 System.out.println("请输入您的操作:");
                 int num = scanner.nextInt();
                 switch (num){
     
                     case 1:
                         //注册
                         System.out.println("请输入您的姓名:");
                         String username = scanner.next();
                         System.out.println("请输入您的密码:");
                         String password = scanner.next();
                         as.register(new Account(username,password,0,0));
                         break;
                     case 2:
                         //登录
                         System.out.println("请输入您的卡号:");
                         int id = scanner.nextInt();
                         System.out.println("请输入您的密码:");
                         String pwd = scanner.next();
                         boolean login = as.login(new Account("", pwd, id, 0));

                         while (login) {
     
                             menu(new String[]{
     "存款","余额","转账","取款","退出"});
                             System.out.println("请输入您的操作:");
                             int i = scanner.nextInt();
                             switch (i) {
     
                                 case 1:
                                     //存款
                                     System.out.println("请输入要存的金额:");
                                     double mon = scanner.nextDouble();
                                     as.deposit(mon);
                                     break;
                                 case 2:
                                     //查询余额
                                     as.CheckBalance();
                                     break;
                                 case 3:
                                     //转账
                                     System.out.println("请输入要转入账户:");
                                     int id1 = scanner.nextInt();
                                     System.out.println("请输入要转入的金额:");
                                     double mon1 = scanner.nextDouble();
                                     as.carryOver(id1,mon1);

                                     break;
                                 case 4:
                                     //取款
                                     System.out.println("请输入您要取出的金额:");
                                     double mon2 = scanner.nextDouble();
                                     as.getMoney(mon2);
                                     break;
                                 case 5:
                                     //退出
                                     login=false;
                             }
                         }
                         continue;
                     case 3:
                         System.exit(0);

                 }
             }

         }

}

4. 测试类:Test

public static void main(String[] args) {
     

        av.index();


    }

5. 结果截图

经典案例题:ATM取款机详细代码---复制既能跑_第1张图片

以上就是这个ATM取款系统的的整体流程,有喜欢的朋友可以帮忙点个赞,谢谢!

你可能感兴趣的:(经典案例题,java,数据分析,软件测试,比特币,oauth)