Java手写银行管理系统

User.java

package sdut.ligong.demo;

import java.util.Calendar;
import java.util.Date;

public class User {

    static int count = 1;

    public String id; // 用户Id

    public String userName; // 用户姓名

    public String password; // 用户密码

    private double money; // 用户余额

    public String CreateAccountData; // 创建账户日期



    public static int getCount() {
        return count;
    }

    public static void setCount(int count) {
        User.count = count;
    }

    public String getId() {
        return id;
    }

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

    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 double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public String getCreateAccountData() {
        return CreateAccountData;
    }

    public void setCreateAccountData(String createAccountData) {
        CreateAccountData = createAccountData;
    }

    public User() throws Exception { // 无参构造方法

        // 初始化创建日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        //System.out.println("现在时间是:" + new Date());
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        String week = String
                .valueOf(7 - (calendar.get(Calendar.DAY_OF_WEEK) - 1));
        CreateAccountData = year + "年" + month + "月" + day + "日,星期" + week;
        // 初始化编号
        String num = FileReadAndFileWrite.readCount();
        if(num==null){
            num="0";
        }
        this.id = num;

        // 初始化用户余额
        this.money = 0.0;
        // 初始化用户密码
        password = "123123";
    }

    public User(String userName) throws Exception {
        //初始化用户名 
        this.userName = userName;
        // 初始化创建日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        //System.out.println("现在时间是:" + new Date());
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        String week = String
                .valueOf((calendar.get(Calendar.DAY_OF_WEEK) - 1));
        CreateAccountData = year + "年" + month + "月" + day + "日,星期" + week;
        // 初始化编号
        String num = FileReadAndFileWrite.readCount();
        if(num.equals(null)){
            num="0";
        }
        this.id = num;
        // 初始化用户余额
        this.money = 0.0;
        // 初始化用户密码
        password = "123123";
    }

}

MainActivity.java

package sdut.ligong.demo;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class MainActivity {

    static Scanner input = new Scanner(System.in);

    static boolean mIsFirst = true;

    /**
     * 主界面
     * 
     * @return
     */
    private static void showMain() {
        if(mIsFirst){
            boolean going = verifyIdentidy();
            if(going){
                mIsFirst = false;
                System.out.println("欢迎进入银行管理系统!");
                System.out.println("1.创建账户");
                System.out.println("2.查询账户");
                System.out.println("3.修改密码");
                System.out.println("4.删除账户");
                System.out.println("5.存钱");
                System.out.println("6.取钱");
                System.out.println("7.转账");
                System.out.println("0.退出");
                System.out.println("请选择你要进行的操作");

            }else{
                // 登陆失败
                System.out.println("用户名或密码错误!请5秒后重试");
                for (int i = 0; i < 5; i++) {
                    System.out.println("倒计时" + (5 - i));
                    try { // 延迟一秒
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                mIsFirst = true;
                showMain();

            }

        }else{
            System.out.println("欢迎进入银行管理系统!");
            System.out.println("请选择你要进行的操作");
            System.out.println("1.创建账户");
            System.out.println("2.查询账户");
            System.out.println("3.修改用户信息");
            System.out.println("4.删除账户");
            System.out.println("5.存钱");
            System.out.println("6.取钱");
            System.out.println("7.转账");
            System.out.println("0.退出");


        }




    }



    /**
     * 主程序入口
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        boolean mIsCircle = true;
        do{
            int choose; 
            showMain();
            choose = input.nextInt();
             switch (choose) {
             case 1://创建账户
            CreateAccount();
             break;
             case 2:  //查询账户
             searchOrModifyAccountOrDelete("查询");
             break;
             case 3:  //修改账户
            searchOrModifyAccountOrDelete("修改");
             break;
             case 4:  //删除账户
            searchOrModifyAccountOrDelete("删除");
             break;
             case 5:  //存钱
            saveMoney();
             break;
             case 6: //取钱
            takeMoney();
             break;
             case 7: //转钱
            transferMoney();
             break;
             case 0:
                 mIsCircle = false;
             break;
             default:
                 mIsCircle = false;
             break;
             }
        }while(mIsCircle);


    }


    private static void transferMoney() throws Exception {
        // TODO Auto-generated method stub
        saveAndtakeMoney("转出");
    }

    private static void takeMoney() throws Exception {
        saveAndtakeMoney("取钱");
    }

    private static void saveMoney() throws Exception {
        saveAndtakeMoney("存钱");
    }

    public static void saveAndtakeMoney(String key) throws Exception{
        System.out.println("请选择你要"+key+"的方式");
        System.out.println("1.按照用户编号进行"+key);
        System.out.println("2.按照用户名进行"+key);
        int choose = input.nextInt();
        if(choose==1){
            System.out.println("请输入你要"+key+"的用户编号");
            choose = input.nextInt();
            if(key.equals("取钱")){
                FileReadAndFileWrite.takeNumUserOfMoney(choose);
            }else if(key.equals("存钱")){
                FileReadAndFileWrite.saveNumUserOfMoney(choose);
            }else{
                FileReadAndFileWrite.transferNumUserOfMoney(choose);
            }

        }else if(choose==2){
            System.out.println("请输入你要"+key+"的用户名");
            String userName = input.next();
            if(key.equals("取钱")){
                FileReadAndFileWrite.takeNameUserOfMoney(userName);
            }else if(key.equals("存钱")){
                FileReadAndFileWrite.saveNameUserOfMoney(userName);
            }else{
                FileReadAndFileWrite.transferNameUserOfMoney(userName);
            }

        }
    }

    // 验证身份
    /**
     * 判断管理员身份
     * 
     * @return
     */
    public static boolean verifyIdentidy() {
        System.out.println("请输入管理员账户");
        String userName = input.next();
        if (userName.equals("admin")) {
            System.out.println("请继续输入管理员密码");
            String password = input.next();
            if (password.equals("123456")) {
                return true;
            }
        }
        return false;
    }

    // 创建新账户
    /**
     * @return false 代表创建账户失败
     * @throws Exception
     */
    private static boolean CreateAccount() throws Exception {

        boolean mIsSucceed = false; // 判断创建账户是否成功 true-成功 false-失败

        System.out.println("你输入您要创建的账户名"); // 输入账户信息

        String userName = input.next();

        // 从数据库中调用数据,判断账户是否存在
        File userData = new File("C:\\dataBase\\userData.txt");

        if (!userData.exists()) { // 如果文件不存在,就创建文件

            userData.createNewFile();
            mIsSucceed = FileReadAndFileWrite.saveUserData(userName); // 在数据库中查询用户信息

        } else { // 如果文件存在,在文件中查询账户

            mIsSucceed = FileReadAndFileWrite.saveUserData(userName); // 在数据库中查询用户信息
        }

        if (mIsSucceed) {

            System.out.println("创建用户名成功!");

        } else {

            System.out.println("已存在用户名!");

        }

        return false;
    }



    /**
     * 查找 修改 删除 用户信息
     */
    private static void searchOrModifyAccountOrDelete(String key) throws Exception {
        System.out.println("请选择" + key + "用户信息的方式  1-编号查询 2-用户名查询");
        int choose = input.nextInt();
        switch (choose) {
        case 1:  //按照编号查找
            System.out.println("请输入要" + key + "的编号:");
            int number = input.nextInt();
            if (key.equals("查询")) {
                searchForNumber(number);
            } else if(key.equals("修改")){
                modifyForNumber(number);
            }else if(key.equals("删除")){
                deleteForNumber(number);
            }

            break;
        case 2:
            System.out.println("请输入要" + key + "的用户名");
            String userName = input.next();
            if (key.equals("查询")) {
                searchForUserName(userName);
            } else if(key.equals("修改")){
                modifyForUserName(userName);
            }else if(key.equals("删除")){
                deleteForUserName(userName);
            }
            break;
        default:
            System.out.println("输入错误!");
            break;
        }

    }
/**
 * 按用户名删除用户信息
 * @param userName
 * @throws Exception 
 */
    private static void deleteForUserName(String userName) throws Exception {
        // TODO Auto-generated method stub
        FileReadAndFileWrite.deleteNameUser(userName);
    }
/**
 * 按用户编号删除用户信息
 * @param number
 * @throws Exception 
 */
    private static void deleteForNumber(int number) throws Exception {
        // TODO Auto-generated method stub
        FileReadAndFileWrite.deleteNumUser(number);
    }

    /**
     * 按照用户名修改用户信息
     * 
     * @param userName
     * @throws Exception
     */
    private static void modifyForUserName(String userName) throws Exception {
        FileReadAndFileWrite.modifyNameUser(userName);
    }

    /**
     * 按照用户编号修改用户信息
     * 
     * @param number
     * @throws Exception
     */
    private static void modifyForNumber(int number) throws Exception {
        FileReadAndFileWrite.modifyNumUser(number);
    }

    /**
     * 按用户编号查找用户信息
     * 
     * @param userName
     * @throws Exception
     */
    private static void searchForUserName(String userName) throws Exception {
        FileReadAndFileWrite.findNameUser(userName);
    }

    /**
     * 按照用户名查找用户信息
     * 
     * @param number
     * @throws Exception
     */
    private static void searchForNumber(int number) throws Exception {
        FileReadAndFileWrite.findNumUser(number);
    }

}

FileReadAndFileWrite.java

package sdut.ligong.demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class FileReadAndFileWrite {

    static Scanner input = new Scanner(System.in);

    public static boolean mIsTransfer = false; // 判断是否是转账状态 false-否

    static Double transferMoney = 0.0;  //转账的钱

    /**
     * 将用户名为userName 保存到数据库中 如果用户名在数据库中存在
     */
    public static boolean saveUserData(final String userName) throws Exception {
        // 读取文件
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData.txt")));
        // 写文件
        PrintWriter 

        pw = new PrintWriter("c:\\dataBase\\userData4.txt");

        // 判断文件是否存在
        boolean mIsExists = false; // 默认用户名不存在
        String line = null;
        while ((line = br.readLine()) != null) {
            if (userName.equals(line)) {
                mIsExists = true;
            }
            pw.println(line);
            pw.flush();
        }
        if (mIsExists) {
            mIsExists = !mIsExists;
            return false; // 用户名已存在
        } else { // 将用户名写入文件中
            User u = new User(userName);
            pw.println(u.getId());
            pw.flush();
            pw.println(u.getUserName());
            pw.flush();
            pw.println(u.getPassword());
            pw.flush();
            pw.println(u.getMoney());
            pw.flush();
            pw.println(u.getCreateAccountData());
            pw.flush();
        }
        br.close();
        pw.close();

        // 将userData4中的内容写入到userData中
        BufferedReader br2 = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData4.txt")));
        PrintWriter pw2 = new PrintWriter("c:\\dataBase\\userData.txt");
        line = null;
        while ((line = br2.readLine()) != null) {
            pw2.println(line);
            pw2.flush();
        }
        br2.close();
        pw2.close();
        br = new BufferedReader(new InputStreamReader(new FileInputStream(
                "c:\\dataBase\\userData4.txt")));
        System.out.println("---------------------创建后的用户名----------------");
        int count = 0;
        while ((line = br.readLine()) != null) {
            if (count % 5 == 0) {

                System.out
                        .println("------------------------------------------");
            }
            count = count + 1;

            System.out.println(line);

        }
        System.out.println("---------------------显示结束-----------------------");
        System.out.println();
        System.out.println();
        br.close();
        // copyUserDataToUserData4();
        // copyUserData4ToUserData();
        return true;
    }

    /**
     * 读取用户的编号
     * 
     * @return
     * @throws Exception
     */
    public static String readCount() throws Exception {
        BufferedReader br2 = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData.txt")));
        PrintWriter pw2 = new PrintWriter("c:\\dataBase\\userData4.txt");
        int count = 0, count1 = 0;
        String line = null;
        while ((line = br2.readLine()) != null) {
            if (count % 5 == 0) {
                count1++;
                pw2.println(count1);
                pw2.flush();
            } else {
                pw2.println(line);
                pw2.flush();
            }
            count++;
        }
        int C = count1 + 1;
        pw2.close();
        br2.close();
        copyUserData4ToUserData();
        copyUserDataToUserData4();
        return C + "";

    }

    /**
     * 根据编号 从数据库中找对应编号的用户信息
     * 
     * @param index
     * @throws Exception
     */
    public static void findNumUser(int index) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData.txt")));
        String line = null;
        int count = 0;
        boolean isFind = false;
        while ((line = br.readLine()) != null) {
            if (count % 5 == 0) {
                if (line.equals("" + index)) {
                    isFind = true;
                    System.out.println("账号编号:" + line);
                    for (int i = 0; i < 4; i++) {
                        line = br.readLine();
                        if (i == 0) {
                            System.out.println("用户名为:" + line);
                        } else if (i == 1) {
                            System.out.println("账户密码为:" + line);
                        } else if (i == 2) {
                            System.out.println("账户余额为:" + line + "元");
                        } else if (i == 3) {
                            System.out.println("创建账户日期为:" + line);
                        }

                    }
                    break;
                }
            } else {
                isFind = false;
            }
            count += 1;

        }

        br.close();
        if (!isFind) {
            System.out.println("未找到该用户");
            Thread.sleep(2000);
            System.out.println();
            System.out.println();
        }
    }

    /**
     * 根据姓名查找对应的用户信息
     * 
     * @param index
     * @throws Exception
     */
    public static void findNameUser(String index) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData.txt")));
        String line = null;
        String preline = null;
        boolean isChanged = false;
        int count = 4;
        boolean isFind = false;
        while ((line = br.readLine()) != null) {
            if (count % 5 == 0) {
                if (line.equals(index)) {
                    isFind = true;
                    isChanged = true;
                    break;
                }
            } else {
                // System.out.println("line ====== "+line+"  count==="+count);
            }
            count += 1;
            preline = line;
        }
        br.close();
        if (!isFind) {
            System.out.println("未找到该用户");
            Thread.sleep(2000);
            System.out.println();
            System.out.println();
        }
        if (isChanged) {
            // System.out.println("用户的编号为"+preline);
            findNumUser(Integer.parseInt(preline));
        }
    }

    /**
     * 按照用户名编号修改
     * 
     * @param index
     * @throws Exception
     */
    public static void modifyNumUser(int index) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData4.txt")));
        PrintWriter pw2 = new PrintWriter("c:\\dataBase\\userData.txt");
        String line = null;
        int count = 0;
        boolean isSucceed = false;
        boolean isChanged = true; // 判断是否改写数据库
        while ((line = br.readLine()) != null) {
            if (count % 5 == 0) {
                if (line.equals("" + index)) {
                    System.out.println("是否要修改用户密码 1-是,0-否");
                    int choose = input.nextInt();
                    isSucceed = true;
                    if (choose == 1) {
                        line = br.readLine();
                        String line1 = br.readLine();
                        System.out.println("请输入用户初始密码");
                        String line3 = input.next();
                        if (line3.equals(line1)) {
                            System.out.println("验证成功!");
                            System.out.println("请输入要修改的密码");
                            String password = input.next();
                            System.out.println("用户编号:" + index);
                            pw2.println(index);
                            pw2.flush();
                            System.out.println("用户名:" + line);
                            pw2.println(line);
                            pw2.flush();
                            System.out.println("用户密码:" + password);
                            pw2.println(password);
                            pw2.flush();
                            line = br.readLine();
                            System.out.println("用户余额:" + line);
                            pw2.println(line);
                            pw2.flush();
                            line = br.readLine();
                            System.out.println("账户创建日期:" + line);
                            pw2.println(line);
                            pw2.flush();
                        } else {
                            System.out.println("密码错误!");
                            Thread.sleep(1000);
                            isChanged = false; 
                            break;

                        }

                    } else {
                        System.out.println("退出查询");
                        isChanged = false; // 不改变文件
                        break;
                    }
                } else {
                    //System.out.println("匹配名称line=="+line+"count=="+count);
                    pw2.println(line);
                    pw2.flush();
                }
            } else {
                //System.out.println("5的倍数line=="+line+"count=="+count);
                pw2.println(line);
                pw2.flush();
            }
            count = count + 1;

        }
        if (!isSucceed) {
            System.out.println("未找到用户");
        }
        br.close();
        pw2.close();
        if (!isChanged) {
            copyUserData4ToUserData();
        }
        copyUserDataToUserData4();
        copyUserData4ToUserData();
    }

    /**
     * 按照用户姓名修改密码
     * 
     * @param name
     * @throws Exception
     */
    public static void modifyNameUser(String name) throws Exception {


        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData.txt")));
        String line = null;
        String preline = null;
        int count = 4;
        boolean isFind = false;
        while ((line = br.readLine()) != null) {
            if (count % 5 == 0) {
                if (line.equals(name)) {
                    isFind = true;
                    modifyNumUser(Integer.parseInt(preline));
                }

            } else {

            }
            count += 1;
            preline = line;
        }
        br.close();
        if (!isFind) {
            System.out.println("未找到该用户");
            Thread.sleep(2000);
            System.out.println();
            System.out.println();
        }



    }

    public static void deleteNameUser(String name) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData.txt")));
        String line = null;
        String preline = null;
        int count = 4;
        boolean isFind = false;
        while ((line = br.readLine()) != null) {
            if (count % 5 == 0) {
                if (line.equals(name)) {
                    isFind = true;
                    // System.out.println("用户编号:" + preline);
                    deleteNumUser(Integer.parseInt(preline));
                }

            } else {

            }
            count += 1;
            preline = line;
        }
        br.close();
        if (!isFind) {
            System.out.println("未找到该用户");
            Thread.sleep(2000);
            System.out.println();
            System.out.println();
        }

    }

    /**
     * 按照用户名编号修改
     * 
     * @param index
     * @throws Exception
     */
    public static void deleteNumUser(int index) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData4.txt")));
        PrintWriter pw2 = new PrintWriter("c:\\dataBase\\userData.txt");
        String line = null;
        int count = 0;
        boolean isSucceed = false;
        boolean isChanged = false;
        while ((line = br.readLine()) != null) {
            if (count % 5 == 0) {
                if (line.equals("" + index)) {
                    System.out.println("是否要删除用户 1-是,0-否");
                    int choose = input.nextInt();
                    isSucceed = true;
                    if (choose == 1) {
                        line = br.readLine();
                        String line1 = br.readLine();
                        System.out.println("请输入用户初始密码");
                        String line3 = input.next();
                        if (line3.equals(line1)) {
                            System.out.println("删除成功!");
                            Thread.sleep(2000);
                            pw2.println("");
                            pw2.flush();
                            pw2.println("");
                            pw2.flush();
                            pw2.println("");
                            pw2.flush();
                            line = br.readLine();
                            pw2.println("");
                            pw2.flush();
                            line = br.readLine();
                        } else {
                            System.out.println("密码输入错误!");
                            isChanged = true; // 文件改变了

                        }

                    } else {
                        System.out.println("退出查询");
                    }
                } else {
                    pw2.println(line);
                    pw2.flush();
                }
            } else {
                pw2.println(line);
                pw2.flush();
            }
            count = count + 1;

        }
        if (!isSucceed) {
            System.out.println("未找到用户");
        }
        br.close();
        pw2.close();
        if (isChanged) {
            copyUserData4ToUserData();
        }
        copyUserDataToUserData4();
        copyUserData4ToUserData();
    }

    /**
     * 将文件1复制到文件4
     * 
     * @throws Exception
     */
    public static void copyUserDataToUserData4() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData.txt")));
        PrintWriter pw = new PrintWriter("c:\\dataBase\\userData4.txt");
        String line = null;
        while ((line = br.readLine()) != null) {
            if (!line.equals("")) {
                pw.println(line);
                pw.flush();
            }
        }

        br.close();
        pw.close();
    }

    /**
     * 将文件4复制到文件1
     * 
     * @throws Exception
     */
    public static void copyUserData4ToUserData() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData4.txt")));
        PrintWriter pw = new PrintWriter("c:\\dataBase\\userData.txt");
        String line = null;
        while ((line = br.readLine()) != null) {
            if (!line.equals("")) {
                pw.println(line);
                pw.flush();
            }
        }

        br.close();
        pw.close();
    }

    public static void saveNumUserOfMoney(int index) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData4.txt")));
        PrintWriter pw2 = new PrintWriter("c:\\dataBase\\userData.txt");
        String line = null;
        int count = 0;
        Double fee = 0.0;
        String preline = null;
        boolean isChanged = true;
        boolean isSucceed = true;
        if (!mIsTransfer) {
            System.out.println("请输入要存入的金额");
            fee = input.nextDouble();
            if (fee <= 0) {
                System.out.println("金额必须大于0");
                fee = 0.0;
            }
        } else {
            System.out.println("进入转账状态!");
            fee = transferMoney;
        }

        while ((line = br.readLine()) != null) {
            if (count % 5 == 0) {
                if (line.equals("" + index)) { // 找到该编号
                    String name = br.readLine(); // 获得用户名
                    String password = br.readLine(); // 获得密码
                    System.out.println("请输入账号密码");
                    String pass = input.next();
                    if (!pass.equals(password)) {
                        isChanged = false;
                        isSucceed = false;
                        System.out.println("密码输入错误!");
                        Thread.sleep(1200);
                        break;
                    } else {
                        System.out.println("存入金额:" + fee);
                        Double sum = Double.parseDouble(br.readLine());
                        sum += fee;
                        System.out.println("账户余额:" + sum);
                        pw2.println(index);
                        pw2.flush();
                        pw2.println(name);
                        pw2.flush();
                        pw2.println(password);
                        pw2.flush();
                        pw2.println(sum);
                        pw2.flush();
                        line = br.readLine();
                        System.out.println("创建账户日期:" + line);
                        pw2.println(line);
                        pw2.flush();
                    }
                } else {
                    pw2.println(line);
                    pw2.flush();
                }
            } else {
                pw2.println(line);
                pw2.flush();
            }
            count++;
            preline = line;
        }
        br.close();
        pw2.close();
        if (!isChanged) {
            copyUserData4ToUserData();
        }
        copyUserDataToUserData4();
        copyUserData4ToUserData();
    }

    /**
     * 按照用户名进行存款
     * 
     * @param name
     * @throws Exception
     */
    public static void saveNameUserOfMoney(String name) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData4.txt")));
        String line = null;
        String preline = null;
        boolean isChanged = false;
        int count = 4;
        boolean isFind = false;
        Double fee;
//      if (!mIsTransfer) {
//          System.out.println("请输入要存入的金额");
//          fee = input.nextDouble();
//          if (fee <= 0) {
//              System.out.println("金额必须大于0");
//              fee = 0.0;
//          }
//      } else {
//          fee = transferMoney;
//      }


        while ((line = br.readLine()) != null) {
            if (count % 5 == 0) {
                if (line.equals(name)) {
                    isFind = true;
                    isChanged = true;
//                  System.out.println("name=="+line);
//                  System.out.println("preline=="+preline);
                    saveNumUserOfMoney(Integer.parseInt(preline));
                    break;
                }
            } else {
                // System.out.println("line ====== "+line+"  count==="+count);
            }
            count += 1;
            preline = line;
        }
        br.close();
        if (!isFind) {
            System.out.println("未找到该用户");
            Thread.sleep(2000);
            System.out.println();
            System.out.println();
        }
        if (isChanged) {
            // System.out.println("用户的编号为"+preline);
            findNumUser(Integer.parseInt(preline));
        }

        br.close();
        if (!isChanged) {
            copyUserData4ToUserData();
        }
        copyUserDataToUserData4();
        copyUserData4ToUserData();
    }

    /**
     * 按照用户名进行取款
     * 
     * @param name
     * @throws Exception
     */
    public static void takeNameUserOfMoney(String name) throws Exception {
        boolean isSucceed = false;
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData4.txt")));
        PrintWriter pw2 = new PrintWriter("c:\\dataBase\\userData.txt");
        String line = null;
        String preline = null;
        int count = 3;
        boolean isChanged = true;
        while ((line = br.readLine()) != null) {
            count = count + 1;
            if (count % 5 == 0) {
                if (line.equals(name)) {
                    isSucceed = true;

                    int choose = 1;
                    if (choose == 1) {
                        String line3 = null;
                        line = br.readLine(); // 获取用户密码
                        if(mIsTransfer){

                            line3 = line;
                        }else{
                            System.out.println("请输入用户密码");
                             line3 = input.next();
                        }

                        if (line3.equals(line)) { // 验证用户密码
                            Double fee = null;
                            if(mIsTransfer){
                                 fee = transferMoney;
                            }else{
                                System.out.println("请输入要取出的金额");
                                 fee = input.nextDouble();
                            }

                            System.out.println("用户编号:" + preline);
                            System.out.println("用户名:" + name);
                            pw2.println(name);
                            pw2.flush();
                            System.out.println("用户密码:" + line3);
                            pw2.println(line3);
                            pw2.flush();
                            line = br.readLine();
                            Double sum = Double.parseDouble(line);
                            System.out.println("用户余额:" + sum);
                            if (fee <= sum) {
                                fee = fee;
                            } else {
                                System.out.println("取款失败");
                                fee = 0.0;
                                Thread.sleep(1500);
                                isChanged = false;
                                break;
                            }
                            System.out.println("要取出的金额为:" + fee);
                            sum -= fee;
                            System.out.println("取出之后的金额为:" + sum);
                            pw2.println(sum);
                            pw2.flush();
                            line = br.readLine();
                            System.out.println("账户创建日期:" + line);
                            pw2.println(line);
                            pw2.flush();
                            isSucceed = true;
                        } else {
                            System.out.println("用户密码错误");
                            line = br.readLine();
                            pw2.println(line);
                            pw2.flush();
                            isChanged = false;
                            break;
                        }

                    } else {
                        System.out.println("程序退出");
                        isChanged = false;
                        break;
                    }
                } else {
                    pw2.println(line);
                    pw2.flush();
                }
            } else {
                pw2.println(line);
                pw2.flush();
            }

            preline = line;

        }
        if (!isSucceed) {
            System.out.println("未找到用户");
        }
        br.close();
        pw2.close();
        if (!isChanged) {
            copyUserData4ToUserData();
        }
        copyUserDataToUserData4();
        copyUserData4ToUserData();
    }

    /**
     * 按照用户编号进行取款
     * 
     * @param index
     * @throws Exception
     */
    public static void takeNumUserOfMoney(int index) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData4.txt")));
        PrintWriter pw2 = new PrintWriter("c:\\dataBase\\userData.txt");
        String line = null;
        int count = 0;
        boolean isSucceed = false;
        boolean isChanged = true;
        while ((line = br.readLine()) != null) {
            if (count % 5 == 0) {
                if (line.equals("" + index)) {
                    isSucceed = true;
                    int choose = 1;
                    if (choose == 1) {
                        line = br.readLine();
                        String line1 = br.readLine();
                        String line3 = null;
                        if(mIsTransfer){
                             line3 = line1;
                        }else{
                            System.out.println("请输入用户初始密码");
                             line3 = input.next();
                        }

                        if (line3.equals(line1)) {
                            System.out.println("验证成功!");
                            Double fee = 0.0;
                            if(mIsTransfer){
                                fee = transferMoney;
                            }else{
                                System.out.println("请输入要取出的金额");
                                 fee = input.nextDouble();
                            }

                            System.out.println("用户编号:" + index);
                            pw2.println(index);
                            pw2.flush();
                            System.out.println("用户名:" + line);
                            pw2.println(line);
                            pw2.flush();
                            System.out.println("用户密码:" + line3);
                            pw2.println(line3);
                            pw2.flush();
                            line = br.readLine();
                            Double sum = Double.parseDouble(line);
                            if (fee <= sum) {
                                fee = fee;
                            } else {
                                fee = 0.0;
                                System.out.println("取款失败!");
                                Thread.sleep(1500);
                                isChanged = false;
                                break;
                            }
                            System.out.println("用户余额取款前余额为" + sum + "元");
                            sum -= fee;
                            System.out
                                    .println("取款后" + fee + "元后余额为" + sum + "元");
                            pw2.println(sum);
                            pw2.flush();
                            line = br.readLine();
                            System.out.println("账户创建日期:" + line);
                            pw2.println(line);
                            pw2.flush();
                        } else {
                            System.out.println("密码输入错误!");
                            isChanged = false;
                            break;

                        }

                    } else {
                        System.out.println("退出查询");
                        isChanged = false;
                        break;
                    }
                } else {
                    pw2.println(line);
                    pw2.flush();
                }
            } else {
                pw2.println(line);
                pw2.flush();
            }
            count = count + 1;

        }
        if (!isSucceed) {
            System.out.println("未找到用户");
        }
        br.close();
        pw2.close();
        if(!isChanged){
            copyUserData4ToUserData();
        }
        copyUserDataToUserData4();
        copyUserData4ToUserData();
    }

    public static void transferNameUserOfMoney(String name) throws Exception {
        mIsTransfer = true;
        boolean isSucceed = false;
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData4.txt")));
        PrintWriter pw2 = new PrintWriter("c:\\dataBase\\userData.txt");
        String line = null;
        String preline = null;
        int count = 3;
        boolean isChanged = true;
        while ((line = br.readLine()) != null) {
            count = count + 1;
            if (count % 5 == 0) {
                if (line.equals(name)) {
                    isSucceed = true;

                    int choose = 1;
                    if (choose == 1) {
                        line = br.readLine(); // 获取用户密码
                        System.out.println("请输入用户密码");
                        String line3 = input.next();
                        if (line3.equals(line)) { // 验证用户密码
                            transferNumUserOfMoney(Integer.parseInt(preline));
//                          
                        } else {
                            System.out.println("用户密码错误");
                            line = br.readLine();
                            pw2.println(line);
                            pw2.flush();
                            Thread.sleep(1500);
                            isChanged = false;
                            break;
                        }

                    } else {
                        System.out.println("程序退出");
                        Thread.sleep(1500);
                        isChanged = false;
                        break;
                    }
                } else {
                    pw2.println(line);
                    pw2.flush();
                }
            } else {
                pw2.println(line);
                pw2.flush();
            }
            preline = line;

        }
        if (!isSucceed) {
            System.out.println("未找到用户");
        }
        br.close();
        pw2.close();
        if(!isChanged){
            copyUserData4ToUserData();
        }
        copyUserDataToUserData4();
        copyUserData4ToUserData();
        mIsTransfer = !mIsTransfer;
    }

    public static void transferNumUserOfMoney(int index) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("c:\\dataBase\\userData4.txt")));
        PrintWriter pw2 = new PrintWriter("c:\\dataBase\\userData.txt");
        String line = null;
        int count = 0;
        int cho = 0;
        boolean isSucceed = false;
        boolean isChanged = true;
        while ((line = br.readLine()) != null) {
            if (count % 5 == 0) {
                if (line.equals("" + index)) {
                    isSucceed = true;
                    int choose = 1;
                    if (choose == 1) {
                        line = br.readLine();
                        String line1 = br.readLine();
                        System.out.println("请输入转出账户的初始密码");
                        String line3 = input.next();
                        if (line3.equals(line1)) {
                            System.out.println("验证成功!");
                            System.out.println("请输入要转出的金额");
                            Double fee = input.nextDouble();
                            transferMoney = fee;
                            pw2.println(index);
                            pw2.flush();
                            pw2.println(line);
                            pw2.flush();
                            pw2.println(line3);
                            pw2.flush();
                            line = br.readLine();
                            Double sum = Double.parseDouble(line);
                            if (fee <= sum) {
                                fee = fee;
                            } else {
                                fee = 0.0;
                                System.out.println("取款失败!");
                                Thread.sleep(1000);
                                isChanged = false;
                                break;
                            }
                            System.out.println("用户余额转出前余额为" + sum + "元");
                            sum -= fee;
                            System.out
                                    .println("转出" + fee + "元后余额为" + sum + "元");
                            pw2.println(sum);
                            pw2.flush();
                            line = br.readLine();
                            // System.out.println("账户创建日期:" + line);
                            pw2.println(line);
                            pw2.flush();
                            System.out.println("请选择要转出账户的方式");
                            System.out.println("1.按账户编号转入");
                            System.out.println("2.按账户名号转入");
                             cho = input.nextInt();
                            mIsTransfer = true;
                            isSucceed = true;
                            break;

                        } else {
                            System.out.println("密码输入错误!");
                            Thread.sleep(1000);
                            isChanged = false;
                            break;
                        }

                    } else {
                        System.out.println("退出查询");
                        Thread.sleep(1000);
                        isChanged = false;
                        break;
                    }
                } else {
                    pw2.println(line);
                    pw2.flush();
                }
            } else {
                pw2.println(line);
                pw2.flush();
            }
            count = count + 1;

        }
        if (!isSucceed) {
            System.out.println("未找到用户");
        }
        br.close();
        pw2.close();
        if(!isChanged){
            copyUserData4ToUserData();
        }
        copyUserDataToUserData4();
        copyUserData4ToUserData();
        if (cho == 1) {
            System.out.println("请输入要转入的账户的编号");
            int index1 = input.nextInt();
            takeNumUserOfMoney(index1);
            saveNumUserOfMoney(index1);

        } else {
            System.out.println("请输入要转入的账户的名称");
            String name = input.next();
            takeNameUserOfMoney(name);
            saveNameUserOfMoney(name);

        }
        mIsTransfer = !mIsTransfer;
    }

}
posted @ 2017-09-14 09:20  Philtell  阅读(187)  评论(0编辑  收藏  举报