• 简单的银行小案例


    银行案例

    • 实现银行功能

      • 用户输入正确卡号和密码可执行一下操作
      • 菜单如下:1.存款 2.取款 3.转账 4.查询余额 5.修改密码 0.退出
    • 分析:

      • User类(cardNo、identity、username、password、phone、balance)
      • Bank类主要包括以下功能
        • 初始化用户(initial)
        • 用户登录(login)
        • 显示菜单
        • 存款(save)、取款(withDraw)、转账(trans)、查询余额(query)、修改密码(modifyPassword)
          该案例未实现保存数据的功能,再次运行仍为初始数据
    package com.lemon.bank;
    /**
     *用户类
     * @author gyq
     */
    
    public class User {
        //卡号
        private String cardNo;
        //身份证号
        private String identity;
        //用户名
        private String username;
        //密码
        private String password;
        //电话
        private String phone;
        //余额
        private double balance;
    
        //默认构造方法
        public User() {
        }
        //带参构造方法
    
    
        public User(String cardNo, String identity, String username, String password, String phone, double balance) {
            this.cardNo = cardNo;
            this.identity = identity;
            this.username = username;
            this.password = password;
            this.phone = phone;
            this.balance = balance;
        }
    
        public String getCardNo() {
            return cardNo;
        }
    
        public void setCardNo(String cardNo) {
            this.cardNo = cardNo;
        }
    
        public String getIdentity() {
            return identity;
        }
    
        public void setIdentity(String identity) {
            this.identity = identity;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public double getBalance() {
            return balance;
        }
    
        public void setBalance(double balance) {
            this.balance = balance;
        }
    }
    
    package com.lemon.bank;
    
    import sun.security.util.Password;
    
    import java.util.Scanner;
    
    /**
     * 银行类
     * @author gyq
     */
    public class Bank {
        //保存用户的数组
        private User[] users = new User[5];
        //保存个数
        private int size;
        //初始化方法
        public void initial(){
            //创建用户1
            User user1=new User();
            user1.setCardNo("1234567890");
            user1.setIdentity("411123456");
            user1.setUsername("xiaoming");
            user1.setPassword("123456");
            user1.setPhone("110");
            user1.setBalance (100000);
            //创建用户2
            User user2=new User("1234567891","411123451","xiaoli","123123","111",12000);
            //放入数组
            users[0]=user1;
            users[1]=user2;
            size=2;
            //System.out.println ("用户初始化完成!");
        }
        public Bank(){
            initial ();
        }
        //2用户登录
        public void login(){
            Scanner input = new Scanner (System.in);
            System.out.println ("请输入卡号:");
            String cardNo=input.next ();
            System.out.println ("请输入密码:");
            String password=input.next ();
            //遍历数组
            User u=null;
            for (int i = 0; i < size; i++) {
                if(users[i].getCardNo ().equals (cardNo)&&users[i].getPassword ().equals (password)){
                    u=users[i];
                    break;
                }
            }
            if (u!=null){
                //登陆成功!
                System.out.println ("登录成功!");
                //显示菜单
                showMenu(u);
            }else{
                System.out.println ("卡号或密码错误~!");
            }
        }
        //显示菜单
        public void showMenu(User u){
            Scanner input = new Scanner (System.in);
            System.out.println ("==============欢迎进入xxx银行系统=====当前用户:"+u.getCardNo ()+"=================");
    
            do{
                System.out.println ("1.存款   2.取款    3.转账    4.查询余额  5.修改密码  0.退出");
    
                int choice=input.nextInt ();
                switch (choice){
                    case 1://存钱
                        this.save (u);
                        break;
                    case 2://取款
                        this.withDraw (u);
                        break;
                    case 3://转账
                        this.trans (u);
                        break;
                    case 4://查询余额
                        this.query (u);
                        break;
                    case 5://修改密码
                        this.modifyPassword (u);
                        break;
                    case 0://退出
    
                        return;
                    default:
                        break;
                }
            }while (true);
    
        }
        //存钱
        public void save(User u){
            Scanner input = new Scanner (System.in);
            System.out.println ("请输入存款金额");
            double m = input.nextDouble ();
            if (m>0){
                u.setBalance (u.getBalance ()+m);
                System.out.println ("存钱成功:余额:"+u.getBalance ());
            }else{
                System.out.println ("存钱失败,请重新输入!");
            }
    
        }
        //5.取钱
        public void withDraw(User u){
            Scanner input = new Scanner (System.in);
            System.out.println ("请输入取款金额");
            double m = input.nextDouble ();
            if (m>0){
                if (u.getBalance ()>=m) {
                    u.setBalance (u.getBalance () - m);
                    System.out.println ("取钱成功:余额:" + u.getBalance ());
                }else {
                    System.out.println ("余额不足!");
                }
            }else{
                System.out.println ("取钱失败,请重新输入!");
            }
    
        }
        //6.转账
        public void trans(User u){
            Scanner input = new Scanner (System.in);
            System.out.println ("请输入对方卡号:");
            String cardNo = input.next ();
            System.out.println ("请输入转账金额:");
            double m=input.nextDouble ();
            //判断对方卡号是否存在
            User toUser=null;
            for (int i=0;i<size;i++){
                if (users[i].getCardNo ().equals (cardNo)){
                    toUser=users[i];
                    break;
                }
            }
            if (toUser!=null){//存在对方卡号
                if(u.getBalance ()>=m){
                    //转账
                    //扣钱
                    u.setBalance (u.getBalance ()-m);
                    //加钱
                    toUser.setBalance (toUser.getBalance ()+m);
                    System.out.println ("转账成功!当前您的余额为"+u.getBalance ());
                }else{
                    System.out.println ("转账失败!余额不足!");
                }
    
            }else{
                System.out.println ("对方卡号不存在,请重新输入");
            }
        }
        //7查询余额
        public void query(User u){
            System.out.println ("卡号:"+u.getCardNo ()+"用户名:"+u.getUsername ()+"余额:"+u.getBalance ());
        }
        //8修改密码
        public void modifyPassword(User u){
            Scanner input = new Scanner (System.in);
            System.out.println ("请输入新密码(密码长度6位):");
            String newpassword=input.next ();
            if (newpassword.length ()==6){
                u.setPassword (newpassword);
                System.out.println ("修改成功!");
            }else {
                System.out.println ("输入密码不符合要求!");
            }
        }
    }
    
    package com.lemon.bank;
    
    public class TestBank {
        public static void main(String[] args) {
            Bank bank = new Bank ();
            //bank.initial ();
            bank.login ();
        }
    }
    
  • 相关阅读:
    java中的object类
    java中super的使用
    java中final的使用
    java中的继承初始化顺序
    java中的方法重写
    springMVC的流程
    dubbo与zookeeper
    java的几种常见数据结构
    集合框架之List和Set区别
    集合框架
  • 原文地址:https://www.cnblogs.com/lemonlover/p/14053677.html
Copyright © 2020-2023  润新知