Java 文件字符过滤流练习--银行卡信息系统(console)

/*
*代码作为Java里字符过滤流的练习使用
*字符流FileWrite、FileReader
*字符过滤流PrintWriter、BufferedReader
*/
  1. 设计银行卡类Card
    Card类属性类
    卡号:cardid
    密码:password
    用户姓名:name
    身份证号:uid
    电话:phone
    余额: balance
    状态:state (正常,锁定:密码错误超过3次)
    2.设计交易类Trade
    交易编号:tradeid
    交易卡号:cardid
    交易金额:money
    交易类型:tradetype 支出/收入
    交易时间: tradetime
  2. 银行卡管理类CardManager
    实现的基本功能: 对开户,登录,取款,存款,查看交易记录等进行管理
    1.建Card实体类
public  class Card {
    private int cardid =(int) ((int)10000001+Math.random()*89999998) ;
    private String password;
    private String name;
    private String uid;
    private String phone;
    private double balance = 0;
    private String state = "正常";  //正常  超过3次被锁定
    private int flag = 0;//错误一次加1

    public Card(){

    }

    public Card(int cardid, String password, String name, String uid, String phone, double balance, String state) {
        super();
        this.cardid = cardid;
        this.password = password;
        this.name = name;
        this.uid = uid;
        this.phone = phone;
        this.balance = balance;
        this.state = state;
    }
    public String toString(){
        return "卡号:"+(int)cardid+" 姓名:"+name+" 密码:"+password+" 身份证号:"+uid+" 电话号码:"+phone+" 余额:"+balance+" 状态:"+state;
    }
    //网文件里输出信息
    public String toStringFile(){
        return (int)cardid+","+name+","+password+","+uid+","+phone+","+balance+","+state;
    }

    public int getCardid() {
        return cardid;
    }
    public void setCardid(int cardid) {
        this.cardid = cardid;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getUid() {
        return uid;
    }
    public void setUid(String uid) {
        this.uid = uid;
    }
    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;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }
}

2.建Trade实体类、并继承Card类

public class Trade extends Card{
    private String tradeid;
    private double money;
    private String tradetype;  //指出/收入
    private String tradetime;

    public String getTradeid() {
        return tradeid;
    }
    public void setTradeid(String tradeid) {
        this.tradeid = tradeid;
    }

    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
    public String getTradetype() {
        return tradetype;
    }
    public void setTradetype(String tradetype) {
        this.tradetype = tradetype;
    }
    public String getTradetime() {
        return tradetime;
    }
    public void setTradetime(String tradetime) {
        this.tradetime = tradetime;
    }
    public String toString(int a){
        return "交易编号:"+tradeid+" 交易卡号:"+(int)super.getCardid()+" 交易金额:"+money+" 交易类型:"+tradetype+" 交易时间:"+tradetime;
    }
}

3.建操作类CardManger

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class CardManager {
    static Scanner sc = new Scanner(System.in);
    static ArrayList<Card> list1 = new ArrayList<Card>();
    static ArrayList<Trade> list2 = new ArrayList<Trade>();
    static File file1 = new File("d://cardinf.txt");
    static File file2 = new File("d://tradeinf.txt");
    static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static SimpleDateFormat da = new SimpleDateFormat("yyMMddHHmmss");
    static int cmaxindex = 0;
    static int tmaxindex = 0;
    public static void main(String [] args){

        ready();//程序开始时,将文件里的数据读取到Arrayslis集合里

        while(true){
            int choose = index();//开始选择页面
            switch(choose){
            case 1:open();break;//开户
            case 2:login();break;//登陆
            case 3:System.exit(0);//退出
            }
        }
    }
    private static int index() {
        // TODO Auto-generated method stub
        System.out.println("————————————————欢迎登陆个人网上银行————————————————");
        System.out.println("1.开户 2.登陆 3.退出");
        int ch = sc.nextInt();

        return ch;
    }
    private static void ready() {
        // TODO Auto-generated method stub
        try {
            //字符基础流
            FileReader fr = new FileReader(file1);
            //字符过滤流
            BufferedReader br = new BufferedReader(fr);
            String data[] = new String[6];
            String str = "";
            while((str = br.readLine()) != null){
                data = str.split(",");
                 Card cc = new Card(Integer.parseInt(data[0]),data[1],data[2],data[3],data[4],Double.valueOf(data[5]),data[6]);
                list1.add(cc);
                cmaxindex++;
            }
            br.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    private static void open() {
        // TODO Auto-generated method stub
        Card card = new Card();
        System.out.println("————————————————欢迎登陆个人网上银行————————————————");
        System.out.println("您的卡号为:"+card.getCardid());
        System.out.println("请输入您的其它信息:");
        System.out.print("密码:");
        card.setPassword(sc.next());
        System.out.print("姓名:");
        card.setName(sc.next());
        System.out.print("身份证:");
        card.setUid(sc.next());
        System.out.print("电话号码:");
        card.setPhone(sc.next());

        list1.add(card);
        try {
            FileWriter fw = new FileWriter(file1);
            PrintWriter pw = new PrintWriter(fw);

            for(Card cc: list1){
                pw.println(cc.toStringFile());
            }

            pw.close();
            System.out.println("开户成功");
            cmaxindex++;
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    private static void login() {
        // TODO Auto-generated method stub
        System.out.println("————————————————欢迎登陆个人网上银行————————————————");
        System.out.print("卡号:");
        int cardn = sc.nextInt();
        System.out.print("密码:");
        String pass = sc.next();
        for(int i = 0;i < cmaxindex;i++){
            if(list1.get(i).getState().equals("正常")){
                if(list1.get(i).getCardid() == cardn){
                    if(list1.get(i).getPassword().equals(pass)){
                        System.out.println("登陆成功");
                        pers(i);
                    }else{
                        list1.get(i).setFlag(list1.get(i).getFlag()+1);
                        if(list1.get(i).getFlag() == 3){
                            list1.get(i).setState("锁定");
                        }
                        System.out.println("您输入的账号或密码不正确");
                    }
                }
            }else{
                System.out.println("您的卡被锁了");
            }
        }
    }

    private static void pers(int num) {
        // TODO Auto-generated method stub
        System.out.println("————————————————欢迎登陆个人网上银行————————————————");
        while(true){
            System.out.println("1.取款 2.存款 3.查看交易记录 4.退出");
            int n = sc.nextInt();

            switch(n){
            case 1:tMoney(num);break;//取款
            case 2:dep(num);break;//存款
            case 3:notes(num);break;//查看交易记录
            case 4:System.exit(0);//退出
            }
        }
    }

    private static void tMoney(int num) {
        // TODO Auto-generated method stub
        System.out.println("请输入要取款的金额:");
        double money = sc.nextDouble();
        if(list1.get(num).getBalance() <= 0){
            System.out.println("您的账户余额不足");
        }else{
            if(list1.get(num).getBalance() >= money){
                list1.get(num).setBalance(list1.get(num).getBalance()-money);
                write();
                addt("支出",money);
                System.out.println("取款成功");
            }else{
                System.out.println("您的账户余额不足");
            }
        }
    }

    private static void addt(String string,double money) {
        // TODO Auto-generated method stub
        Date day=new Date();
        Trade trade = new Trade();
        trade.setTradeid(da.format(day));
        trade.setMoney(money);
        trade.setTradetype(string);
        trade.setTradetime(df.format(day));
        try {
            FileWriter fw = new FileWriter(file2,true);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(trade.toString(1));
            pw.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    private static void dep(int num) {
        // TODO Auto-generated method stub
        System.out.println("请输入要存款的金额:");
        double money = sc.nextDouble();
        list1.get(num).setBalance(list1.get(num).getBalance()+money);
        write();
        addt("收入",money);
        System.out.println("存款成功");
    }

    private static void notes(int num) {
        // TODO Auto-generated method stub
        try {
            FileReader fr = new FileReader(file2);
            BufferedReader br = new BufferedReader(fr);
            String str = "";
            while((str = br.readLine()) != null){
                System.out.println(str);
            }
            br.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    private static void write() {
        // TODO Auto-generated method stub
        try {
            FileWriter fw = new FileWriter(file1);
            PrintWriter pw = new PrintWriter(fw);
            for(Card cc: list1){
                pw.println(cc.toStringFile());
            }
            fw.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值