银行账户存取系统

文件那块搞了好久,全在main函数里,挺乱的,最终还是换行符的问题导致写入读出都有问题。
文件会直接在D盘下,叫data5,我没放在存代码的目录里。

存钱的类
package Bank;

class SaveAccount implements Runnable{
    private Account a;
    private int money;

    public SaveAccount() {
        super();
    }

    public SaveAccount(Account a, int money) {
        super();
        this.a = a;
        this.money = money;
    }

    @Override
    public void run() {

        synchronized (a)
        {
            try {
                int currentMoney = a.searchMoney();
                Thread.sleep(1000);
                a.saveMoney(money);
                System.out.println("尊敬的客户:" + a.getName() + "您好! 您的账户余额" + currentMoney
                        + "元,您存入" + this.money + "元,当前账户余额为:" + a.searchMoney() + "元");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }

    }

}
取钱的类
package Bank;

class GetMoney implements Runnable{
    private Account a;
    private int money;

    public GetMoney() {
        super();
    }

    public GetMoney(Account a, int money) {
        super();
        this.a = a;
        this.money = money;
    }

    @Override
    public void run() {

        synchronized (a)
        {
            try {
                int currentMoney = a.searchMoney();
                Thread.sleep(1000);
                a.getMoney(money);
                System.out.println("尊敬的客户:" + a.getName() + "您好! 您的账户余额" + currentMoney
                        + "元,您取出" + this.money + "元,当前账户余额为:" + a.searchMoney() + "元");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
账户类

package Bank;

class Account{
    private String name;
    private int value;
    public int getMoney(int money){
        if(money >= this.value){
            this.value = 0;
        }else{
            this.value = this.value - money;
        }
        return money;
    }
    public void saveMoney(int money){
        this.value = this.value + money;
    }
    public int searchMoney(){
        return this.value;
    }
    public Account() {
        super();
    }
    public Account(String name, int value) {
        super();
        this.name = name;
        this.value = value;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }

}
Main
package Bank;



import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class Main {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //学生信息储存在集合类中
        ArrayList<Account> array = new ArrayList<Account>();
        //启动应用便开始从硬盘加载数据
        try {
            loadData(array);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("向文件存新账户请按1\n查看文件已有账户请按2");
        int k=sc.nextInt();
        if (k==1) {
            System.out.println("请输入2个账户信息来存入文件,格式为姓名和金额");
            String name1 = sc.next();
            int value1 = sc.nextInt();
            Account account = new Account(name1, value1);
            String name2 = sc.next();
            int value2 = sc.nextInt();
            Account account2 = new Account(name2, value2);
            array.add(account);
            array.add(account2);
            try {
                storeData(array);    //将两个账户存入文件
            } catch (IOException e) {
                e.printStackTrace();
            }
            query(array);
        }else if(k==2){
            query(array);
        }
        System.out.println("输入你要从文件中取出来操作的账户姓名");

        String Tname=sc.next();
        for (Account a:array){
            if (Tname.equals(a.getName())){
                System.out.println("请输入存钱的金额,不存输入0");
                int themoney=sc.nextInt();
                SaveAccount sa = new SaveAccount(a, themoney);
                Thread t1 = new Thread(sa,a.getName());
                t1.start();
                System.out.println("请输入取钱金额,不取输入0");
                int themoney2=sc.nextInt();
                GetMoney gm =new GetMoney(a, themoney2);
                Thread t = new Thread(gm,a.getName());
                t.start();
                try {
                    storeData(array);   //修改后存回文件
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("操作完毕");
                System.out.println("是否查看更新后的账户?\n是请按1,退出请按2");
                int check=sc.nextInt();
                if (check==1) query(array);

            }
        }


        try {
            storeData(array);        //完成存钱,取钱后将信息写入文件。
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void query(ArrayList<Account> array){

        if(array.size() == 0){
            System.out.println("暂时未存储任何信息,请重新选择。");
            return;
        }
        System.out.println("姓名\t\t金额");
        for(int i=0;i<array.size();i++){
            Account s = array.get(i);
            System.out.println(s.getName()+"\t"+s.getValue()+"\t");
        }
    }



    private static void loadData(ArrayList<Account> array) throws IOException{
        //初次打开应用会创建文件
        File file= new File("D:\\data5.txt");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("初次启动初始化完成!");
        }
        //从文件中读取数据进集合
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while((line = br.readLine())!=null){
            String[] str;
            str = line.split("&");
            Account s = new Account();
            s.setName(str[0]);
            s.setValue(Integer.parseInt(str[1]));
            array.add(s);
        }
        br.close();
    }
    //储存数据到文件中
    private static void storeData(ArrayList<Account> array) throws IOException{
        File file= new File("D:\\data5.txt");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //直接将数据全部存储到文件中,不用追加方法,也方便增删查改
        //如何信息量比较大就不适应
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        //将数据存储进文件
        for(int i=0;i<array.size();i++){
            Account s = array.get(i);
            bw.write(s.getName());
           // bw.write(s.getName(),0,s.getName().length()-1);
            bw.write('&');//间隔不同数据
            String sgV=s.getValue()+"";
            bw.write(sgV + "\n");
           // bw.write(sgV,0,sgV.length()-1);
          //  bw.newLine();

        }
        bw.flush();
        bw.close();
    }


}
上一篇:对Java题目集7-9的总结性Blog


下一篇:stata分位数分类