改进后基于数组的Java银行案例

本文探讨了如何使用Java优化基于数组的银行系统案例。通过深入分析用户和银行类,文章展示了如何在IntelliJ IDEA环境下进行有效的代码实现和管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

无目录
用户class

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;
    }

    @Override
    public String toString() {
        return "User{" +
                "cardNo='" + cardNo + '\'' +
                ", identity='" + identity + '\'' +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", phone='" + phone + '\'' +
                ", balance=" + balance +
                '}';
    }

银行class

import java.util.Objects;
import java.util.Scanner;

public class Bank {
//    static boolean flag = false;
    private final User[] users = new User[5];
    User user = new User();
    private static final Scanner glo_sc = new Scanner(System.in);
    public void initial (){
        User user1 = new User("12345","98765","Henry","1234","02198765678",20000.0);
        User user2 = new User("23456","87654","Harry","1234","02187654567",30000.0);
        User user3 = new User("34567","76543","Hudson","1234","02176543456",40000.0);
        User user4 = new User("45678","65432","Leon","1234","02165432345",50000.0);
        User user5 = new User("56789","54321","Levis","1234","02154321234",60000.0);
        users[0] = user1;
        users[1] = user2;
        users[2] = user3;
        users[3] = user4;
        users[4] = user5;

    }

    public static void main(String[] args) {
        Bank bank = new Bank();
        bank.initial();
        while (true) {
            System.out.println("=====TXY=Bank=AYM=System=====");

            System.out.println("Press Y for continue. Press 0 for leave:");
            String command = glo_sc.next();
            if (Objects.equals(command, "y")){
                bank.login();
            }else if (Objects.equals(command, "0")){
                break;
            }else {
                System.out.println("Input incorrect, try again please.");
            }

        }
    }

    private void login(){
        while (true) {
            System.out.println("====Customer==Login====");
            System.out.println("Please enter your CardNo:");
            String num = glo_sc.next();
            System.out.println("Please enter your Password:");
            String pw =  glo_sc.next();
            for (User value : users) {
                if (num.equals(value.getCardNo()) && pw.equals(value.getPassword())) {
                    System.out.println("Welcome back, Our friend dear " + value.getUserName() + "!");
                    user = value;
//                    flag = true;
//                    System.out.println(flag);
                    showMenu();
                    return;
                }
            }
            //                System.out.println(flag);
            System.out.println("Sorry, something must got wrong!");
            while (true) {
                System.out.println("Press Y to continue. Press 0 to back:");
                String command = glo_sc.next();
                if ("y".equals(command)){
                    break;
                }else if ("0".equals(command)){
                    return;
                }else {
                    System.out.println("chose your operation by Y or 0");
                }
            }
        }
    }

    private void showMenu() {
        System.out.println("=====Customer==Service==Center=====");
        label:do {
            System.out.println("=====1.Save===2.Withdraw===3.Trans===4.Query Balance===5.Modify Password===0.Exit======");
            System.out.println("Chose what you gonna do:");
            int choice = glo_sc.nextInt();
            switch (choice){
                case 1:
                    save();
                    break ;
                case 2:
                    withDraw();
                    break ;
                case 3:
                    trans();
                    break ;
                case 4:
                    queryBalance();
                    break ;
                case 5:
                    modifyPassword();
                    break ;
                case 0:
                    System.out.println("Welcome to your next visit!");
                    break label;
                default:
                    System.out.println("Incorrect Input, try again please.");
                    break ;

            }
        }while (true);
    }

    private void withDraw() {
        System.out.println("======Customer==Withdraw==Service=======");
        System.out.println("Enter the amount you gonna withdraw:");
        double wdr = glo_sc.nextDouble();
        if (wdr <= user.getBalance()) {
            if (wdr % 100 == 0.0) {
                double balance = user.getBalance() - wdr;
                user.setBalance(balance);
                System.out.println("Withdraw succeed, your balance update to: " + user.getBalance());
            } else {
                System.out.println("Sorry, the ATM doesn't support amount less than 100.0 ");
            }
        } else {
            System.out.println("Sorry, your balance isn't enough.");
        }

    }

    private void save() {
        System.out.println("======Customer==Deposit==Service=======");
        System.out.println("Enter the amount of you deposit:");
        double deposit = glo_sc.nextDouble();
        if (deposit > 0) {
            deposit = user.getBalance() + deposit;
            user.setBalance(deposit);
            System.out.println("Deposit succeed. Your balance update to " + user.getBalance());
        } else {
            System.out.println("The amount you input is incorrect, deposit failed.");
        }
    }

    private void trans() {
        System.out.println("======Customer==Transfer==Service=======");
        System.out.println("Enter account which you gonna transfer to:");
        String toAcc = glo_sc.next();
        System.out.println("Enter how many you gonna transfer:");
        double amount = glo_sc.nextDouble();

        if (amount <= user.getBalance()){

            for (User value : users) {
                String existAcc = value.getCardNo();
                if (toAcc.equals(existAcc)) {
                    double myBalance = user.getBalance() - amount;
                    user.setBalance(myBalance);
                    System.out.println("Transferring for you~~~~");
                    double hisBalance = value.getBalance();
                    value.setBalance(hisBalance + amount);
                    System.out.println("Transferring succeed. Your balance change to: " + user.getBalance());
                    return;
                }
            }
            System.out.println("The target account is incorrect.");
        }else {
            System.out.println("Your balance isn't enough.");
        }

    }

    private void queryBalance() {
        System.out.println("Your current balance is: "+user.getBalance());
        while (true) {
            System.out.println("Press 0 back to previous:");
            int press = glo_sc.nextInt();
            if (press == 0) {
                return;
            } else {
                System.out.println("Input illegal.");
            }
        }
    }

    private void modifyPassword() {
        System.out.println("Enter your new password:");
        String newPw = glo_sc.next();
        System.out.println("Enter new password one more time:");
        String newPw1 = glo_sc.next();
        if (newPw.equals(newPw1)){
            user.setPassword(newPw);
            System.out.println("Modify succeed. Take care your new password carefully.");
        }else {
            System.out.println("The password you input twice are not the same, operation stop!");
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值