码迷,mamicode.com
首页 > 编程语言 > 详细

Java ATM模拟系统报告

时间:2017-10-09 00:33:49      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:java语言   后缀   isp   设计   amr   stream   oid   epo   exception   

设计内容:

本系统采用JAVA语言编写测试完成,基本上在字符界面下模拟了ATM系统的相关实现。

本系统主要模拟银行ATM机系统功能,主要有查阅、取款、存款、添加用户、删除用户等功能。

技术说明:

项目目录下必须有一个已经录入的用户文件,名为ATMUsers (无后缀名,压缩包内已有一份),否则将报错。

当输入用户的卡号和密码时,系统能登录ATM柜员机系统,用户可以按照以下规则进行:

查询余额。

ATM取款:每次取款金额不得大于100,总额不得超过余额。

ATM存款:不能出现负存款。

当输入用户名000000与密码000000时,能登录ATM柜员机系统的管理员账户,管理员可以按照以下规则进行:

列出用户:列出ATM系统上的所有用户。

添加用户:卡号必须为10位,新密码长度必须为6位,只有卡号与密码都符合要求的情况下才可以添加用户。

删除用户:输入卡号以删除卡号对应用户的记录,删除前必须键入Y以确认。

源代码:

import java.util.*;

import java.io.*;


public class ATM {
    static int number;
    static int usernum = 0;
    static boolean isAdmin = false;
    public static Scanner kbd = new Scanner(System.in);
 

    public static void readLength() throws IOException {
        BufferedReader file = null;

        try  {
            file =  new BufferedReader(new InputStreamReader(new FileInputStream("ATMUsers")));
            String line = null;
            int count = 0;
            while ((line = file.readLine()) != null) count++;
            number = count;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (file != null) {
                try {
                    file.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void readUsers(String[][] users) throws IOException {
        BufferedReader file = null;

        try  {
            file =  new BufferedReader(new InputStreamReader(new FileInputStream("ATMUsers")));
            String line = null;
            int count = 0;
            while ((line = file.readLine()) != null) {
                String data = line;
                String[] strArray = data.split(" ");
                users[0][count] = strArray[0];
                users[1][count] = strArray[1];
                users[2][count] = strArray[2];
                count++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (file != null) {
                try {
                    file.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static String checkID(String acctNum, String pwd, String[][] users) {
        String result = "错误!";
        for(int i = 0;i < number;i++) {
            if (acctNum.equals("000000") && pwd.equals("000000")) {
                isAdmin = true;
                return "0";
            }
            if (acctNum.equals(users[0][i]) && pwd.equals(users[1][i])) {
                result = users[2][i];
                usernum = i;
            }
        }
        return result;
    }


    public static int menu() {
        int menuChoice;
 

        do
        {
            System.out.print("\n请选择想要执行的操作:\n"
                    + "**********************"
                    + "\n* 一、余额\t二、存款 *\n"
                    + "*                    *"
                    + "\n* 三、取款\t四、退出 *\n"
                    + "**********************\n");
            menuChoice = kbd.nextInt();
            if (menuChoice < 1 || menuChoice > 4){
                System.out.println("错误!");
            }
        }while (menuChoice < 1 || menuChoice > 4);
        return menuChoice;
    }


    public static int adminMenu() {
        int menuChoice;


        do
        {
            System.out.print("\n请选择想要执行的操作:\n"
                    + "******************************"
                    + "\n* 一、读取用户\t二、添加用户 *\n"
                    + "*                            *"
                    + "\n* 三、删除用户\t四、退出管理 *\n"
                    + "******************************\n");
            menuChoice = kbd.nextInt();
            if (menuChoice < 1 || menuChoice > 4){
                System.out.println("错误!");
            }
        }while (menuChoice < 1 || menuChoice > 4);
        return menuChoice;
    }


    public static void displayBalance(double x) {
        System.out.printf("\n您的余额为 $%.2f\n", x);
    }


    public static double deposit(double x, double y, String[][] users) {
        double depositAmt = y, currentBal = x;
        double newBalance = depositAmt + currentBal;
        System.out.printf("您现在的余额为:$%.2f\n",  newBalance);
        users[2][usernum] = "" + newBalance;
        return newBalance;
    }
 

    public static double withdraw(double x, double y, String[][] users) {
        double withdrawAmt = y, currentBal = x, newBalance;
        newBalance = currentBal - withdrawAmt;
        users[2][usernum] = "" + newBalance;
        System.out.printf("您现在的余额为:$%.2f\n",newBalance);
        return newBalance;
    }


    public static String[][] addUser(String[][] users) throws IOException {
        System.out.print("\n请输入您想要添加的用户卡号:");
        String data1 = kbd.next();
        if(data1.length() != 10) {
            System.out.print("卡号长度不符!");
            return users;
        }
        for(int i = 0;i < number;i++) {
            if(data1 == users[0][i])
                System.out.print("已有同名用户!");
            return users;
        }
        System.out.print("\n请输入密码:");
        String data2 = kbd.next();
        if(data2.length() != 6) {
            System.out.print("密码长度不符!");
        }
        System.out.print("\n请输入初始资金:");
        String data3 = kbd.next();
        String[][] newusers = new String[3][number + 1];
        for(int i = 0; i < number;i++) {
            newusers[0][i] = users[0][i];
            newusers[1][i] = users[1][i];
            newusers[2][i] = users[2][i];
        }
        newusers[0][number] = data1;
        newusers[1][number] = data2;
        newusers[2][number] = data3;
        FileWriter fileWriter = new FileWriter("ATMUsers",true);
        BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
        bufferWriter.newLine();
        bufferWriter.write(data1);
        bufferWriter.write(‘ ‘);
        bufferWriter.write(data2);
        bufferWriter.write(‘ ‘);
        bufferWriter.write(data3);
        bufferWriter.close();
        number++;
        System.out.print("\n添加成功!");
        return newusers;
    }


    public static void deleteUser(String[][] users) throws IOException {
        System.out.print("\n请输入您想要删除的用户卡号:");
        String todelete = kbd.next();
        int i;
        boolean exist = false;
        for(i = 0;i < number;i++) {
            if(todelete.equals(users[0][i])) {
                exist = true;
                break;
            }
        }
        if(exist != true) {
            System.out.print("\n错误:用户不存在!");
            return;
        }
        System.out.print("\n确认要删除该用户吗?(键入Y以确认)");
        String check = kbd.next();
        if(!check.equals("y") && !check.equals("Y")) {
            return;
        }
        int j;
        for(j = i;j < number - 1;j++) {
            users[0][j] = users[0][j + 1];
            users[1][j] = users[1][j + 1];
            users[2][j] = users[2][j + 1];
        }
        number--;
        System.out.print("\n删除成功!");
    }


    public static void remakeFile(String[][] users) throws IOException {
        File file =  new File("ATMUsers");
        file.delete();
        File newfile =  new File("ATMUsers");
        newfile.createNewFile();
        FileWriter fileWriter = new FileWriter(newfile.getName(),true);
        BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
        for(int i = 0;i < number;i++) {
            if(i != 0) bufferWriter.newLine();
            bufferWriter.write(users[0][i]);
            bufferWriter.write(‘ ‘);
            bufferWriter.write(users[1][i]);
            bufferWriter.write(‘ ‘);
            bufferWriter.write(users[2][i]);
        }
        bufferWriter.close();
    }


    public static void main(String[] args) throws IOException {
        String accNum, pass, origBal = "错误!";
        int count = 0, menuOption = 0;
        double depositAmt = 0, withdrawAmt = 0, currentBal = 0;

 
        readLength();
        String[][] users = new String[3][number];
        readUsers(users);
        do{
            System.out.print("请输入账户名:");
            accNum = kbd.next();
            System.out.print("请输入密码:");
            pass = kbd.next();
            origBal = checkID(accNum, pass,users);
            count++;
            if (count >= 3 && origBal.equals("错误!")){
                System.out.print("尝试次数已达上限!");
                System.exit(0);
            }
            if (!(origBal.equals("错误!"))){
                if(isAdmin == false) System.out.println("\n您的余额为 $" + Float.parseFloat(users[2][usernum]));
                if(isAdmin == true) System.out.println("\n您正以管理员身份登录。");
            }
            else
                System.out.println(origBal);
        }while(origBal.equals("错误!"));
        currentBal=Double.parseDouble(origBal);
        while (menuOption != 4 && isAdmin == true) {
            menuOption = adminMenu();
            switch (menuOption) {
                case 1:
                    for (int i = 0; i < number; i++){
                        System.out.println("用户" + (i + 1) + ": " + users[0][i]);
                    }
                    continue;
                case 2:
                    users = addUser(users);
                    continue;
                case 3:
                    deleteUser(users);
                    continue;
                case 4:
                    System.out.print("\n正在退出……");
                    remakeFile(users);
                    System.exit(0);
                    break;
            }
        }
            while (menuOption != 4 && isAdmin == false) {
            menuOption= menu();
            switch (menuOption)
            {
                case 1:
                    displayBalance(currentBal);
                    continue;
                case 2:
                    System.out.print("\n请输入您想要存储的数额:$ ");
                    depositAmt = kbd.nextDouble();
                    if(depositAmt < 0.0) {
                        System.out.print("\n不得进行负存款!");
                        continue;
                    }
                    currentBal=deposit(depositAmt, currentBal,users);
                    continue;
                case 3:
                    System.out.print("\n请输入您想要提取的数额:$ ");
                    withdrawAmt = kbd.nextDouble();
                    while(withdrawAmt > 100.0 || withdrawAmt > currentBal) {
                        System.out.print("错误:一次取款太多!" + "\n请输入较小的数额:$");
                        withdrawAmt = kbd.nextDouble();
                    }
                    currentBal = withdraw(currentBal, withdrawAmt,users);
                    continue;
                case 4:
                    System.out.print("\n正在退出……");
                    remakeFile(users);
                    System.exit(0);
                    break;
            }
        }
    }
}

 

Java ATM模拟系统报告

标签:java语言   后缀   isp   设计   amr   stream   oid   epo   exception   

原文地址:http://www.cnblogs.com/PabloZeal/p/7638997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!