银行账户管理

Account.java

package com.buaa.account;

publicabstractclass Account {

   privateint id;

   private String userName;

   privatedouble balance;

   publicstaticintCurrentID = 100;

  

   //开户时调用的构造方法

   public Account(String userName, double balance) {

     this.userName = userName;

     this.balance = balance;

     id = CurrentID++;

   }

  

   //读取文件加载数据时用的构造方法

   public Account(int id, String userName, double balance) {

     this.id = id;

     this.userName = userName;

     this.balance = balance;

   }

  

   publicstaticint getCurrentID() {

     returnCurrentID;

   }

   publicstaticvoid setCurrentID(int currentID) {

     CurrentID += currentID;

   }

   publicint getId() {

     return id;

   }

   publicvoid setId(int id) {

     this.id = id;

   }

   public String getUserName() {

     return userName;

   }

   publicvoid setUserName(String userName) {

     this.userName = userName;

   }

   publicdouble getBalance() {

     return balance;

   }

   publicvoid setBalance(double balance) {

     this.balance = balance;

   }

   // 存款

   publicvoid deposit(double money) {

     balance += money;

   }

   // 取款

   publicvoid withdraw(double money) throws BalanceNotEnoughException {

     if (balance < money) {

        thrownew BalanceNotEnoughException("余额不足!");

     } else {

        balance -= money;

     }

   }

  

  

  

   @Override

   public String toString() {

     // TODO Auto-generated method stub

     return id + "\t" + userName + "\t" + balance;

   }

   publicabstractdouble interest();

}

BalanceNotEnoughException.java

package com.buaa.account;

publicclassBalanceNotEnoughExceptionextends Exception {

   public BalanceNotEnoughException(String message) {

     super(message);

   }

}

Bank.java

package com.buaa.account;

import java.util.Collection;

import java.util.LinkedHashMap;

import java.util.Map;

public class Bank {

         //所有的用户信息都存放在此map中

         private Map<Integer, Account> map = new LinkedHashMap<Integer, Account>();

        

         //开户,向map中加入一个Account对象

         public void insertAccount(Account account) {

                   map.put(account.getId(), account);

         }

        

         //以数组的形式返回map中的所有数据

         public Account[] getAllAccount() {

                   Collection<Account> c = map.values();

                   Account[] a = c.toArray(new Account[0]);

                   return a;

         }

        

         public Account getAccount(int id) {

                   return map.get(id);

         }

}

Client.java

package com.buaa.account;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Client {

         /**

          * @param args

          */

         public static void main(String[] args) {

                   // TODO Auto-generated method stub

                   int select = 0 ;

                   String fileName = "Account.dat";

                   Bank bank = new Bank();

                   DataAccount.dataLoad(fileName, bank);

                   Account acc = null;

                   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                   do {

                            try {

                                     System.out.println("请选择: 1-管理员    2-用户    0-退出");

                                     select = Integer.parseInt(br.readLine());

                                     if(select==1) {

                                               System.out.println("管理员模块菜单");

                                               System.out.println("1. 开户");

                                               System.out.println("2. 查询所有账户");

                                               System.out.println("0. 退出");

                                               System.out.print("请输入菜单号:");

                                               select = Integer.parseInt(br.readLine());

                                               switch(select) {

                                                        case 1:

                                                                 System.out.print("请输入开户户名:");

                                                                 String userName = br.readLine();

                                                                 System.out.print("请输入开启金额:");

                                                                 double balance = Double.parseDouble(br.readLine());

                                                                 System.out.print("请选择利息类型(1.固定利率  2.浮动利率)");

                                                                 int type = Integer.parseInt(br.readLine());

                                                                

                                                                 if(type==1) {

                                                                           acc = new FixedAccount(userName, balance);

                                                                 }else if(type==2) {

                                                                           acc = new FloatAccount(userName, balance);

                                                                 }

                                                                 bank.insertAccount(acc);

                                                                 DataAccount.savedData(fileName, bank);

                                                                 System.out.println("开户成功!!!!!");

                                                                 break;

                                                        case 2:

                                                                 System.out.println("账户id\t账户名\t账户余额");

                                                                 Account[] array = bank.getAllAccount();

                                                                 for(Account a : array) {

                                                                           System.out.println(a.getId() + "\t" + a.getUserName() + "\t" + a.getBalance());

                                                                 }

                                                                 break;

                                                        case 0:

                                                        default:

                                                                 break;

                                               }

                                     }else if(select==2) {

                                               System.out.println("用户模块菜单");

                                               System.out.println("1. 存款");

                                               System.out.println("2. 取款");

                                               System.out.println("3. 查看本账户利息");

                                               System.out.println("0. 退出");

                                               System.out.print("请先输入一个账户ID:");

                                               int id = Integer.parseInt(br.readLine());

                                               acc = bank.getAccount(id);

                                               if(acc==null) {

                                                        System.out.println("该账户不存在!!!");

                                               }else {

                                                        System.out.println(acc);

                                                        System.out.print("请输入菜单号:");

                                                        select = Integer.parseInt(br.readLine());

                                                        double money = 0;

                                                        switch(select) {

                                                                 case 1:

                                                                           System.out.print("请输入存钱的数目:");

                                                                           money = Double.parseDouble(br.readLine());

                                                                           acc.deposit(money);

                                                                           DataAccount.savedData(fileName, bank);

                                                                           System.out.println(acc.getUserName() + "成功地存入$" + money);

                                                                           System.out.println(acc);

                                                                          break;

                                                                 case 2:

                                                                           System.out.print("请输入取钱的数目:");

                                                                           money = Double.parseDouble(br.readLine());

                                                                           acc.withdraw(money);

                                                                           DataAccount.savedData(fileName, bank);

                                                                           System.out.println(acc.getUserName() + "成功地取出$" + money);

                                                                           System.out.println(acc);

                                                                           break;

                                                                 case 3:

                                                                           System.out.println(acc.getUserName() + "的利息是" + acc.interest());

                                                                           break;

                                                                 case 0:

                                                                 default:

                                                                           break;

                                                        }

                                               }

                                     }

                            } catch (NumberFormatException e) {

                                     // TODO Auto-generated catch block

                                     e.printStackTrace();

                            } catch (IOException e) {

                                     // TODO Auto-generated catch block

                                     e.printStackTrace();

                            } catch (BalanceNotEnoughException e) {

                                     // TODO Auto-generated catch block

                                     e.printStackTrace();

                            }

                            System.out.println("----------------------------------");

                   }while(select!=0);

                   try {

                            br.close();

                   } catch (IOException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                   }

         }

}

DataAccount.java

package com.buaa.account;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class DataAccount {

         public static void dataLoad(String fileName, Bank bank) {

                   try {

                            BufferedReader br = new BufferedReader(new FileReader(fileName));

                            String s;

                            int count = 0;

                            while ((s = br.readLine()) != null) {

                                     String[] str = s.split(",");

                                     Account acc = null;

                                     if (str[3].equals("1")) {

                                               acc = new FixedAccount(Integer.parseInt(str[0]), str[1],

                                                                 Double.parseDouble(str[2]));

                                     } else if (str[3].equals("2")) {

                                               acc = new FloatAccount(Integer.parseInt(str[0]), str[1],

                                                                 Double.parseDouble(str[2]));

                                     }

                                     bank.insertAccount(acc);

                                     count++;

                            }

                            br.close();

                            Account.setCurrentID(count);

                   } catch (FileNotFoundException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                   } catch (IOException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                   }

         }

         public static void savedData(String fileName, Bank bank) {

                   try {

                            Account[] acc = bank.getAllAccount();

                            BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));

                            String s;

                            for (int i = 0; i < acc.length; i++) {

                                     s = acc[i].getId() + "," + acc[i].getUserName() + ","

                                                        + acc[i].getBalance();

                                     if (acc[i] instanceof FixedAccount) {

                                               s += "," + 1;

                                     } else if (acc[i] instanceof FloatAccount) {

                                               s += "," + 2;

                                     }

                                     bw.write(s);

                                     bw.newLine();

                            }

                            bw.close();

                   } catch (IOException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                   }

         }

}

FixedAccount.java

package com.buaa.account;

publicclass FixedAccount extends Account {

  

   public FixedAccount(String userName, double balance) {

     super(userName, balance);

   }

  

   public FixedAccount(int id, String userName, double balance) {

     super(id, userName, balance);

   }

  

   @Override

   publicdouble interest() {

     // TODO Auto-generated method stub

     returnthis.getBalance() * 0.03;

   }

}

FloatAccount.java

package com.buaa.account;

publicclass FloatAccount extends Account {

  

   public FloatAccount(String userName, double balance) {

     super(userName, balance);

   }

  

   public FloatAccount(int id, String userName, double balance) {

     super(id, userName, balance);

   }

  

   @Override

   publicdouble interest() {

     // TODO Auto-generated method stub

     double balance = this.getBalance();

     if(balance<=5000) {

        return balance * 0.01;

     }elseif(balance <=100000) {

        return balance * 0.02;

     }else {

        return balance * 0.05;

     }

   }

}

相关推荐