客户从银行账户存取钱操作的设计与实现(采用集合存储顾客信息)(可直接运行)

主类:Demo

package 包名;

import java.io.*;
import java.util.ArrayList;                         //ArrayList类是一个特殊的数组--动态数组。来自于System.Collections命名空间;通过添加和删除元素,就可以动态改变数组的长度。
import java.util.HashMap;                           //HashMap 是一个散列表,它存储的内容是键值对(key-value)映射。
import java.util.Map;                              // Map接口通俗的理解其实和Set List函数的            
                                                //  用法一样,唯一的区别就是Map接口就是把每 
                                                // 个元素都使用key—value的形式存储在集合中。 
                                                  // ( 相当于php中数组的键和值)。
import java.util.Scanner;

   public class Demo {
    static java.util.Scanner sc = new java.util.Scanner(System.in);

    public static void main(String[] args) {
    ArrayList<Customer> array = new ArrayList<Customer>();        //创建一 个类型为                                                                                                                     
                                                           // customer的集合对象:array
        while (true) {
                                    //输出俊淇银行的首页面
            System.out.println("-------------------欢迎光临俊淇银行------------------------");
            System.out.println("------              1. 已经有账号                      -----");
            System.out.println("------              2. 注册账号                          -----");
            System.out.println("------              3. 注销账号                          -----");
            System.out.println("------              4. 查看所有用户                      -----");
            System.out.println("------              0. 退出                              -----");
            System.out.println("--------------------------------------------------------------");
            System.out.print("请输入您的选择:");
                    //输入选择
            String start = sc.nextLine();
                        //使用switch  case语句,语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
            switch (start) {
                case "1":

                    caozuo(array);                                  //如果已经有账号,调用
                                                    //           caozuo方法来实现取款存款
                    System.out.println();
                    break;                                          //跳出switch循环
                case "2":
                    System.out.println("请为你创建一个用户名");
                    adduser(array);                                 //调用adduser方法来实                        
                                                                   //  现注册账号的功能
                    System.out.println();
                    break;
                case "3":
                    deleteuser(array);                              //调用deleteuser方法来     
                                                                   //     实现注销账号
                    System.out.println();
                    break;
                case "4":
                    //                                                    调用findalluser     
                                                               //      查看所有用户
                    findalluser(array);
                    System.out.println();
                    break;

                case "0":
                     // System.out.println("------              5. 退出                              -----");
                    System.out.println("                     感谢您的使用                               ");
                    File file = new File("D:\\idea_download\\scott_06\\info.txt");
                    file.delete();
                    return;                                 //return的功能是结束一                                                                                                                        
                                                 //    个方法。 一旦在循环体内执行到一个                        
                                                //        return语句,return语句将     
                                                //       会结束该方法,循环自然也随之结束。

                default:                                     //如果输入的不是case以后的东西
                    // System.out.println("------                  -----");
                    System.out.println("                    你的输入有误!请重写输入                              ");

            }

        }
    }

    public static void adduser(ArrayList<Customer> array) {               //增加用户的方法
        Scanner in = new Scanner(System.in);                            //创建一个Scanner类的对象叫in
        String id="";                                     //创建一个名为id的String类型

        while (true) {                                          //无限循环
            System.out.print("输入账号:");
            id = in.nextLine();
            int x;
            boolean flag = false;                    //创建boolean类型的对象
                                            //使用for循环遍历array集合
            for (x = 0; x < array.size(); x++) {
                Customer s = array.get(x);
                if (id.equals(s.getId())) {        //使用if语句判断账号是否已经存在
                    flag = true;                    //如果相同flag被赋值true
                    break;
                }
            }
            if (flag) {        //判断flag是否为true
                System.out.println("账户被占用,请重新输入");
            } else {
                break;
            }

        }
                                //添加账号密码
        System.out.println("请输入你的密码:");
        String pwd = in.nextLine();           //输入密码
        saveInfo(id, pwd);                  //调用saveinfo方法,将id和pwd实参传入,来实现存储密码和账号到文件,以保证后期登录时正确登录
        System.out.print("输入姓名:");          //输入姓名
        String name = in.nextLine();
        System.out.print("输入你要存的金额:");
        Double balance = in.nextDouble();         //double类型,可以输入小数


        //创建账户对象
        Customer s = new Customer(name, id, balance);
        array.add(s);                       //将s对象,储存到array集合里
        System.out.println("添加账户成功,年利润为0.0123.");
    }


    //查看所有账户信息
    public static void findalluser(ArrayList<Customer> array) {
                                                                                 //判断集合是否为空
        if (array.size() == 0) {
            System.out.println("无用户信息,请重新选择");
            return;
        }
                                                                                        //遍历输出集合
        System.out.println();
        System.out.println("id\t\t\t\t姓名\t\t\t\t余额\t\t\t\t年利润");
        System.out.println("-------------------------------------------------------------------------");
        for (int x = 0; x < array.size(); x++) {
            Customer s = array.get(x);
            System.out.println(s.getId() + "\t\t\t\t" + s.getName() + "\t\t\t\t" + s.getBalance() + "\t\t\t\t" + s.getAnnualInterestRate());
        }
        System.out.println("-------------------------------------------------------------------------");
    }


    public static void deleteuser(ArrayList<Customer> array) {
        Scanner in = new Scanner(System.in);
        System.out.print("输入要删除用户的用户id:");
        String id = in.nextLine();

        int index=-1;                                                 //创建一个索引,来判断是否存在
        for (int x = 0; x < array.size(); x++) {
            Customer s = array.get(x);
            if (id.equals(s.getId())) {                         //如果id在集合里存在
                index = x;                                      //索引变成非负数,在集合的第x位
                break;
            }
        }
        if (index == -1) {                                  //index没变,表示集合无次id
            System.out.print("该用户信息不存在,是否继续当前删除操作(Yes or No):");
            String choice = in.nextLine();
            if (choice.equalsIgnoreCase("Yes")) {
                deleteuser(array);
            } else if (choice.equalsIgnoreCase("No")) {
                return;
            } else {
                System.out.println("输入信息有误,返回主界面");
                return;
            }
        } else {
            array.remove(index);                                        //将集合的第index位删除
            System.out.println("删除用户成功");
        }


    }

    public static void caozuo(ArrayList<Customer> array) {                      //用户存取系统的实现
        Map<String, String> maps = readInfo();                              //创建一个maps对象,来接受readinfo方法传来的对象
        while (true) {
            System.out.println("请输入您的账户id");
            String id = sc.nextLine();

            int index = -1;                 //索引    后期判断id是否存在
//            int password = -1;
            for (int x = 0; x < array.size(); x++) {                        //遍历array集合,查看是否有此用户
                Customer s = array.get(x);
                if (id.equals(s.getId())) {                 //判断id是否相同
                    index = x;
                    break;
                }
            }
                                                                                //若不存在
            if (index == -1) {
                System.out.print("该用户信息不存在,是否继续当前操作(Yes or No):");
                String choice = sc.nextLine();
                if (choice.equalsIgnoreCase("Yes")) {
                    caozuo(array);
                } else if (choice.equalsIgnoreCase("No")) {
                    return;
                } else {
                    System.out.println("输入信息有误,返回主界面");
                    return;
                }
            } else {
                                                   //输入密码判断是否时账户主人
                System.out.println("请输入你的密码");
                String pwd = sc.nextLine();
                int password = -1;                  //索引
                for (Map.Entry<String, String> ste : maps.entrySet()) {         //遍历map集合对象maps   for(类型名  类型 : 需要遍历的数组)
                    if (ste.getValue().equals(id + pwd)) {                  //如果账号密码相同
                        password = 1;
                    }
                }
                if (password == -1) {               //如果不同则不能实现存款取款功能返回到主页面
                    System.out.print("密码错误,请重新输入!");
                    String choice = sc.nextLine();
                    if (choice.equalsIgnoreCase("Yes")) {
                        caozuo(array);
                    } else if (choice.equalsIgnoreCase("No")) {
                        return;
                    } else {
                        System.out.println("输入信息有误,返回主界面");
                        return;
                    }
                }


                if (password == 1) {                    //密码正确接下来操作
                    System.out.println("密码正确!!");
                    System.out.println();
                    Customer s = array.get(index);
                    boolean flag = true;
                    //用户进行存取款页面操作
                    System.out.println("-----选择更新用户信息选项------");
                    System.out.println("--      1. 存款          --");
                    System.out.println("--      2. 取款         --");
                    System.out.println("--      3. 返回主界面    --");
                    System.out.println("--------------------------");
                    while (flag) {
                        System.out.print("请输入您的选择:");
                        Scanner sc = new Scanner(System.in);                    //定义一个Scanner输入流
                        String choice = sc.nextLine();
                        switch (choice) {                                           //switch case方法语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
                            case "1":

                                s.setBalance(Account.Deposit(s.getBalance()));     //调用account类的deposit方法,将返回值赋值到balance
                                System.out.print("继续更新操作(Yes or No):");
                                String a = sc.nextLine();
                                if (a.equalsIgnoreCase("Yes"))                  //判断a是yes or no 来完成后期操作
                                    break;
                                else if (a.equalsIgnoreCase("No")) {
                                    array.set(index, s);
                                    System.out.println("更新操作完成,返回主界面");
                                    return;
                                } else {
                                    System.out.println("输入信息有误,返回主界面");
                                    return;
                                }
                            case "2":
                                s.setBalance(Account.WithDraw(s.getBalance()));              //取款操作   调用account里的withdraw方法,传入参赛是。getbalance
                                System.out.print("继续更新操作(Yes or No):");
                                String b = sc.nextLine();
                                if (b.equalsIgnoreCase("Yes"))
                                    break;
                                else if (b.equalsIgnoreCase("No")) {            //else if 再次继续判断
                                    array.set(index, s);
                                    System.out.println("更新操作完成,返回主界面");
                                    return;
                                } else {
                                    System.out.println("输入信息有误,返回主界面");
                                    return;
                                }
                            case "3":
                                array.set(index, s);
                                System.out.println("操作完成,返回主页。");
                                return;                                                          //跳出这个方法
                            default:
                                System.out.println("输入信息有误。返回主界面");           //输入的数不是有效数字
                                return;
                        }
                    }
                }
            }
        }

    }


    public static boolean saveInfo(String username, String pwd) {                           //存储密码方法,传入账户密码

        try {                               //抛出异常              如果路径中的javaspace目录
            //[1]创建file类指定我们要把数据存储位置
            String result = username + "##" + pwd;                                  //在密码之间加上##方便后期切割
            //[2]创建一个文件输出
            File file = new File("D:\\idea_download\\scott_06\\info.txt");                      //路径
            FileOutputStream fos = new FileOutputStream(file);                 //创建一个fos对象,负责输出内容到文件
            fos.write(result.getBytes());
            fos.close();                            //关闭
            return true;
        } catch (Exception e) {
            e.printStackTrace();            //如果捕捉到异常,输出错误信息
            return false;
        }

    }

    //读取用户的信息
    public static Map<String, String> readInfo() {


        try {
                                                                                            //[1]定义map
            Map<String, String> maps = new HashMap<String, String>();                   //map集合,用hashmap来实现
            File file = new File("D:\\idea_download\\scott_06\\info.txt");              //绝对路径
            FileInputStream fis = null;                      //创建fileinputstream对象,输入流,目的是将file中的内容读出
            fis = new FileInputStream(file);
            BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));           //缓冲流,将文件内容放到缓冲区
            String content = bufr.readLine();//读取数据

            //切割字符串 封装到map集合中
            String[] splis = content.split("##");
            String name = splis[0];
            String pwd = splis[1];
            //把name和pwd放入map中
            maps.put("name", name + pwd);

            fis.close();
            return maps;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


}

然后再创建一个customer类,编写私有属性

package 包名;

public class Customer {

    //this.变量名        表示成员变量
    //直接使用变量名是局部变量




    //私有属性
    private String name;
    private String id;
    private double balance;
    private final double annualInterestRate = 0.0123;   //用final定义常量

    //带参构造方法
    public Customer(String name, String id, double balance) {
        this.name = name;
        this.id = id;
        this.balance = balance;

    }

    //无参构造方法
    public Customer() {
    }

//    成员变量封装 — set 和 get 方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;                                  // this关键字指向的是当前对象的引用        this.属性名称
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {            //带参方法,方便后期存钱操作
        this.balance = balance;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }


}

 编写account类

package unusual;

public class Account {                  //定义账户初始总金额,获取键盘输入。
    static java.util.Scanner sc = new java.util.Scanner(System.in);             //Scanner是一个类,nextInt()是Scanner的成员函数,System.in作为参数传递给Scanner的构造函数,
                                                                                // 使Scanner用键盘作为输入,然后用new在内存中实例化一个Scanner出来,使得其它变量能调用这块内存区。


    public static double Deposit(double a) {          //存款方法

        System.out.println("欢迎进入存款页面,请输入存款的金额");
        while (true) {                                                                          //无限循环,除非有break和return跳出循环
            System.out.println("请输入您要存入的金额:");                                          //获取存款金额
            double money = sc.nextInt();                                                        //输入整数到整型变量

            //存款总数
            a += money;
            System.out.println("您存入了" + money + "元" + "现在共有存款" + a);
            System.out.println("继续请按1,退出请按0");
            //获取是否继续存款1或者0
            int jx = sc.nextInt();
            if (jx == 0) {
                return a;                       //返回a这个变量
            } else {
                continue;                       //按1继续
            }
        }
    }


    public static double WithDraw(double a) {             //取款方法          带参

        System.out.println("欢迎进入取款页面,请输入取款金额");
        while (true) {
            //获取存款金额
            System.out.println("请输入您要取出的金额:");
            double money2 = sc.nextDouble() ;
            //存款总数        (判断取款金额,如果大于自身的存款则返回系统重新操作)
            if (money2 > a) {
                System.out.println("您的取款金额大于存款,请返回系统重新操作");
                break;
            }
            a = a - money2;                                                         //将取出的钱从存款里扣除
            System.out.println("您取出了" + money2 + "元" + "现在共有存款" + a);        //输出现有多少钱给用户
            System.out.println("继续请按1,退出请按0");                                 //用户选择是否继续存款或取款
            //获取是否继续存款1或者0
            int jx = sc.nextInt();                                              //用if判断语句来选择继续还是退出
            if (jx == 0) {
                break;
            } else {
                continue;
            }
        }
        return a;                                               //返回一个a的变量
    }


}


最后附上报告的pdf:
银行报告

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值