JavaATM机系统

130 阅读1分钟

在这里插入图片描述

  • ATM.java


import java.util.Arrays;
import java.util.Scanner;

public class ATM {
    static Account account = new Account();
    static Bank bank = new Bank();
    public static void main(String[] args) {

        int i,type;
        Login(account.getAccountId(),account.getPassword(),account.getBalance(),account.getType());


    }
    public static void Login(String accountId[],int password[],double balance[],int type[]){
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入账号:");
        String user = scanner.next();
        for (int i=0;i<accountId.length;i++){
            if (accountId[i].equals(user)){
                login_pass(i,password[i],type[i]);
            }
        }
        System.out.print("账号错误,请重新");
        Login(accountId,password,balance,type);



    }

    public static void login_pass(int i,int password,int type) {
        int pass = 0;
        String new_pass = " ";
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("输入密码: ");
            new_pass = scanner.next();
            try{
                pass =Integer.parseInt(new_pass);
            }catch (NumberFormatException e){
                System.out.print("密码请输入数字!");
                System.out.print("请重新");
                login_pass(i,password,type);
            }
            if(pass == password){
                if(type == 1){
                    System.out.println("欢迎你管理员");
                    admin();
                }else{
                    System.out.println("登录成功!");
                    user_ATM(i);
                }

            }
            System.out.print("密码错误!!!");
            System.out.print("请重新");
            login_pass(i,password,type);

        }
    }
    public static void admin(){
        Scanner scanner = new Scanner(System.in);
        int t = 0;
        while(true){
            System.out.println("请输入 1添加用户 2注销用户 3退出系统 4查询用户信息 5退出登录");
            t = scanner.nextInt();
            switch(t){
                case 1:
                    add();

                    break;
                case 2:
                    account.setAccountId(bank.Logout(account.getAccountId()));
                    break;
                case 3:
                    System.exit(-1);
                    break;
                case 4:
                    bank.List(account.getAccountId(),account.getPassword(),account.getBalance(),account.getType());
                    break;
                case 5:
                    Login(account.getAccountId(),account.getPassword(),account.getBalance(),account.getType());
                    break;
                default:
                    System.out.println("输入错误,请重新输入!!");
                    break;
            }
        }

    }
    public static void user_ATM(int i){
        Scanner scanner = new Scanner(System.in);
        int t = 0;
        int New = 0;
        double edit_balance=0;
        while (true){
            bank.Account(i,account.getAccountId(),account.getPassword(),account.getBalance(),account.getType());
            System.out.println("1取款 2查询余额 3存钱 4转账 5重新登录 6修改密码 7退出系统");
            t = scanner.nextInt();
            switch (t){
                case 1:
                    account.user_Balance(i,bank.withdraw());    break;
                case 2:
                    bank.inquiry();     break;
                case 3:
                    account.user_Balance(i,bank.deposit());     break;
                case 4:
                    account.setBalance(bank.transfer(i,account.getAccountId(),account.getBalance()));
                    break;
                case 5:
                    Login(account.getAccountId(),account.getPassword(),account.getBalance(),account.getType());
                                        break;
                case 6:
                    account.user_password(i,bank.edit_pass());    break;
                case 7:
                    System.exit(-1);
                    break;
                default:
                    System.out.println("请重新输入!!");
                    break;
            }
        }
    }
    public static void add() {
        account.setAccountId(bank.add_user(account.getAccountId()));
        account.setPassword(bank.add_pass(account.getPassword()));
        account.setBalance(bank.add_balance(account.getBalance()));
        account.setType(bank.add_type(account.getType()));
        System.out.println("添加成功!!");
    }

}
  • Bank.java
import java.util.Scanner;

public class Bank {
    private static String accountId;
    private static int type;
    private static int password;
    private static double balance;
    public void Account(int i,String accountId[],int password[],double balance[],int type[]){
        this.accountId = accountId[i];
        this.password = password[i];
        this.balance = balance[i];
        this.type = type[i];
    }


    public static String[] Logout(String accountId[]){
        Scanner scanner = new Scanner(System.in);
        String user = " ";
        String error = " ";
        System.out.println("请输入要注销的用户");
        user = scanner.next();
        System.out.println("确定删除吗 yes no");
        error = scanner.next();
        if (error.equals("yes")){
            for (int i = 0; i < accountId.length; i++) {
                if (accountId[i].equals(user)){
                    accountId[i] = "已注销";
                    System.out.println("注销成功!");
                    return accountId;
                }

            }
            System.out.println("未找到该用户!");
            Logout(accountId);
        }else{
            System.out.println("取消成功!!");
        }
        return accountId;
    }




    public static void List(String accountId[],int password[],double balance[],int type[]) {
        String type_new = " ";
        for (int i = 0; i < balance.length; i++) {
            if (type[i] == 0){
                type_new = "普通用户";
            }else{
                type_new = "管理员用户";
            }
            if (!accountId[i].equals("已注销")){
                System.out.println("用户名:"+accountId[i]+' '+"密码:"+password[i]+' '+"账户余额:"+balance[i]+' '+type_new);
            }

        }
    }

    public static String[] add_user(String accountId[]){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入新用户账号!");
        String new_name = scanner.next();
        String[] newarr_user = new String[accountId.length+1];
        for (int j = 0; j <= accountId.length-1; j++) {
            newarr_user[j]  =  accountId[j];
        }
        newarr_user[accountId.length]  =  new_name;
        accountId = newarr_user;
        return accountId;
    }
    public static int[] add_pass(int password[]){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入新用户密码!");
        int new_pass = scanner.nextInt();
        int[] newarr_pass = new int[password.length+1];
        for (int j = 0; j <= password.length-1; j++) {
            newarr_pass[j]  =  password[j];
        }
        newarr_pass[password.length]  =  new_pass;
        password = newarr_pass;
        return password;
    }
    public static double[] add_balance(double balance[]){
        Scanner scanner = new Scanner(System.in);
        double[] newarr_balance = new double[balance.length+1];
        for (int j = 0; j <= balance.length-1; j++) {
            newarr_balance[j] =  balance[j];
        }
        newarr_balance[balance.length] =  0;
        balance = newarr_balance;
        return balance;
    }
    public static int[] add_type(int type[]){
        Scanner scanner = new Scanner(System.in);
        int[] newarr_type = new int[type.length+1];
        for (int j = 0; j <= type.length-1; j++) {
            newarr_type[j] =  type[j];
        }
        newarr_type[type.length] =  0;
        type = newarr_type;
        return type;
    }

    public static int verify(String str){
        int new_int = 0;
        try{
            new_int =Integer.parseInt(str);
        }catch (NumberFormatException e){
            return -1;
        }
        return new_int;
    }
    public static int edit_pass() {
        Scanner scanner = new Scanner(System.in);
        String new_pass = " ";
        int pass = 0;
        System.out.println("输入原密码!");
        new_pass = scanner.next();
        pass = verify(new_pass);
        if(pass != -1){
            if (pass == password){
                while(true){
                    System.out.println("请输入新密码!!");
                    new_pass = scanner.next();
                    pass = verify(new_pass);//验证输入是否为数字
                    if (pass != -1){
                        password = pass;
                        return password;
                    }
                    System.out.print("请输入数字,重新");
                }

            }
        }else{
            System.out.print("请输入数字,重新");
            edit_pass();
            return password;
        }
        System.out.print("密码错误!重新");
        edit_pass();
        return 0;
    }

    public static double  withdraw(){//取款
        Scanner scanner = new Scanner(System.in);
        double edit_balance=0;
        System.out.println("请输入取款数目");
        edit_balance = scanner.nextDouble();
        if (edit_balance > balance){
            System.out.println("您的余额不足,请重新输入");
            withdraw();
        }
        balance =  balance - edit_balance;
        return balance;
    }
    public static double deposit(){//存款
        Scanner scanner = new Scanner(System.in);
        double edit_balance=0;
        System.out.println("请输入存款数额");
        edit_balance = scanner.nextDouble();
        balance = balance + edit_balance;
        return balance;
    }
    public static void inquiry(){//查询
        System.out.println("账户余额:"+balance);
    }
    public static double[] transfer(int i,String new_accountId[],double new_balance[]){
        double re_money[];
        Scanner scanner = new Scanner(System.in);
        double edit_balance=0;
        System.out.println("请输入转账数目");
        edit_balance = scanner.nextDouble();
        if (edit_balance > new_balance[i]){
            System.out.println("您的余额不足,请重新输入");
            transfer(i,new_accountId,new_balance);
            return new_balance;
        }
        new_balance[i] = new_balance[i] - edit_balance;
        while(true){
            System.out.println("请输入转账账号:");
            String user = scanner.next();
            for (int j = 0; j < new_accountId.length; j++) {
                if (new_accountId[j].equals(user)){
                    new_balance[j] += edit_balance;
                    System.out.println("转账成功!!");
                    return new_balance;
                }
            }
        }
    }
}
  • Account.java
import java.util.Scanner;

public class Account {
    private static String accountId[] = {"admin", "w001", "w002", "w003"};
    private static int type[] = {1,0,0,0};
    private static int password[] = {123,456,789,111};
    private static double balance[] = {100.0,200.0,300.0,0};
    public String[] getAccountId() {
        return accountId;
    }

    public void setAccountId(String[] accountId) {
        this.accountId = accountId;
    }
    public int[] getType() {
        return type;
    }

    public void setType(int[] type) {
        this.type = type;
    }

    public int[] getPassword() {
        return password;
    }

    public void setPassword(int[] password) {
        this.password = password;
    }

    public double[] getBalance() {
        return balance;
    }

    public void setBalance(double[] balance) {
        this.balance = balance;
    }
    public void user_accountId(int i,String New_Id){
        accountId[i] = New_Id;
    }
    public void user_password(int i,int New_pass){
        password[i] = New_pass;
    }
    public void user_Balance(int i,double New_Balance){
        balance[i] = New_Balance;
    }
    public void user_type(int i,int New_type){
        type[i] = New_type;
    }
}

在这里插入图片描述