Java实现一个模拟银行

Java新手,欢迎各位大佬、前辈指点。

 

实现的功能:

  1. 创建一个新的账户(可以选择是支票账户或者储蓄账户)
  2. 通过用户名和密码登录到某个账户
    • 存款
    • 取款
    • 转账
    • 查询余额
    • 打印用户列表
    • 注销指定账户
    • 对支票账户进行扣税
    • 对储蓄账户发放利息

涵盖知识点:

  • 面向对象的封装性、继承性、多态性
  • 对象数组的使用
  • 控制台输入
  • 随机数的使用
  • 调用另一个包中static的方法
  • 判断两个字符串是否相等(equal)
  • 字符串的拼接
  • 判断字符串中某个字符是否与指定字符相等
  • 对象的强制类型转换(父类对象调用子类方法时)
  • 销毁对象数组中的某个对象

父类BankAccount

 1 public class BankAccount
 2 {
 3     final private String id;
 4     private double balance;
 5     final private String password;
 6 
 7     BankAccount(String id,String password,double balance)
 8     {
 9         this.id=id;
10         this.password=password;
11         this.balance=balance;
12     }
13 
14     public boolean proof(double amount)//为true时余额充足
15     {
16         return (balance-amount>0);
17     }
18     public String getId()
19     {
20         return id;
21     }
22     public String getPassword()
23     {
24         return password;
25     }
26     public double getBalance()
27     {
28         return balance;
29     }
30     public void deposit(double amount)//存款
31     {
32         balance+=amount;
33     }
34     public void withdraw(double amount)//取款
35     {
36         if(proof(amount))
37         {
38             balance-=amount;
39         }
40         else
41         {
42             System.out.println("余额不足!!!");
43         }
44     }
45    public void transferAccount(BankAccount user,double amount)//转账
46    {
47        if(proof(amount))
48        {
49            withdraw(amount);
50            user.deposit(amount);
51        }
52        else
53        {
54            System.out.println("你的余额不足!");
55            System.out.println("操作失败!!!");
56        }
57    }
58 }

子类SavingAccount(储蓄账户)

 1 public class SavingAccount extends BankAccount//储蓄账户类
 2 {
 3     private double interest;
 4     public SavingAccount(String id,String password,double balance)
 5     {
 6         super(id,password,balance);
 7     }
 8     public void harvestVest()//发利息,利息为存款的5%
 9     {
10         interest=getBalance()*0.05;
11         super.deposit(interest);
12     }
13     public void deposit(double amount)//存款
14     {
15         super.deposit(amount);
16     }
17     public void withdraw(double amount)//取款
18     {
19         super.withdraw(amount);
20     }
21 }
22 //储蓄账户:发利息、账户密码登录、存取款、查余额、转账

子类CheckingAccount(支票账户)

 1 public class CheckingAccount extends BankAccount//支票账户类
 2 {
 3     private double tax;
 4     public CheckingAccount (String id,String password,double balance)
 5     {
 6         super(id,password,balance);
 7     }
 8     public void deduction()//扣税,税为存款的10%
 9     {
10         tax=getBalance()*0.1;
11         super.withdraw(tax);
12     }
13     public void deposit(double amount)//存款
14     {
15         super.deposit(amount);
16     }
17     public void withdraw(double amount)//取款
18     {
19         super.withdraw(amount);
20     }
21 }
22 //支票账户:扣税、账户密码登录、存取款、查余额、转账

需要在主方法中调用的方法的包

  1 class Method
  2 {
  3     public static String createUser(char[] a, int b)//创造账号的方法
  4     {
  5         StringBuffer s;
  6         if (b == 1)
  7         {
  8             s = new StringBuffer("Saving");
  9         }
 10         else { s = new StringBuffer("Checking"); }
 11         String user;
 12         for (int i = 0; i < 3; i++)
 13         {
 14             int number = (int) (Math.random() * 62);
 15             s.append(a[number]);
 16         }
 17         user = s.toString();
 18         return user;
 19     }
 20     public static void outPut1(BankAccount[] USER,int count)//打印用户库
 21     {
 22         if(count==0)
 23         {
 24             System.out.println("你还没创建任何用户");
 25             return;
 26         }
 27         for(int i=0;i<count;i++)
 28         {
 29             System.out.println("第"+(i+1)+"个用户账户名为:" +USER[i].getId()+ "  密码为:"+USER[i].getPassword());
 30         }
 31     }
 32     public static void outPut2(BankAccount[] USER,int count)//打印用户余额
 33     {
 34         if(count==0)
 35         {
 36             System.out.println("你还没创建任何用户");
 37             return;
 38         }
 39         for(int i=0;i<count;i++)
 40         {
 41             System.out.println("第"+(i+1)+"个用户账户名为:" +USER[i].getId()+ "  余额为:"+USER[i].getBalance());
 42         }
 43     }
 44     public static void assignment(char[] a)//给数组赋值
 45     {
 46         for (int i = 0; i < 26; i++)
 47         {
 48             a[i] = (char) (i + 65);
 49         }
 50         for (int i = 26; i < 52; i++)
 51         {
 52             a[i] = (char) (i - 26 + 97);
 53         }
 54         for (int i = 52; i < 62; i++)
 55         {
 56             a[i] = (char) (i - 52 + 48);
 57         }
 58     }
 59     public static int Login(String id,String password,BankAccount[] USER,int count)//登录时的验证
 60     {
 61         for(int i=0;i<count;i++)
 62         {
 63             if(id.equals(USER[i].getId()))
 64             {
 65                 if(password.equals(USER[i].getPassword()))
 66                 {
 67                     return i;
 68                 }
 69                 else
 70                 {
 71                     return -1;
 72                 }
 73             }
 74         }
 75         return -1;
 76     }
 77     public static int Login(String id,BankAccount[] USER,int count)//用于返回下标,判断用户名是否存在
 78     {
 79         for(int i=0;i<count;i++)
 80         {
 81             if(id.equals(USER[i].getId()))
 82             {
 83                 return i;
 84             }
 85         }
 86         return -1;
 87     }
 88     public static void taxDeduction(BankAccount[] USER,int count)//扣税
 89     {
 90         for(int i=0;i<count;i++)
 91         {
 92             String name=USER[i].getId();
 93             if(name.charAt(0)=='C')
 94             {
 95                 ((CheckingAccount)USER[i]).deduction();//强制类型转换
 96             }
 97         }
 98     }
 99     public static void interestPayment(BankAccount[] USER,int count)//发利息
100     {
101         for(int i=0;i<count;i++)
102         {
103             String name=USER[i].getId();
104             if(name.charAt(0)=='S')
105             {
106                 ((SavingAccount)USER[i]).harvestVest();//强制类型转换
107             }
108         }
109     }
110     public static void logOff(String id,String password,BankAccount[] USER,int count)//注销
111     {
112         int x=Login(id,password,USER,count);
113         if(x!=-1)
114         {
115             USER[x]=null;
116             for(int j=x;j<count;j++)
117             {
118                 USER[j]=USER[j+1];
119             }
120             System.out.println("注销成功!");
121         }
122         else
123         {
124             System.out.println("用户名不存在或密码错误!");
125         }
126     }
127 }

 

主方法

  1 import java.util.Scanner;
  2 public class Bank
  3 {
  4     public static void main(String[] args)
  5     {
  6         BankAccount[] USER=new BankAccount[100];    //建立BankAccount类的数组
  7         int count = 0;                              //记录用户名的个数
  8         String id;                                  //输入的用户名
  9         String password;                            //输入的密码
 10         double amount;                              //进行余额改变的数字,存、取、转;
 11         char[] a = new char[62];                    //存放用户名符号的数组
 12         Scanner sc = new Scanner(System.in);
 13         boolean bool3=true;                         //控制程序保持运行
 14         int select1;                                //select1为选择创建||登录
 15         Method.assignment(a);                       //给数组a赋值
 16         while (bool3)
 17         {
 18             System.out.println("欢迎使用本银行系统");
 19             System.out.println("请输入你要进行的操作编号");
 20             System.out.println("1、创建一个新帐户");
 21             System.out.println("2、登录到一个已经存在的账户");
 22             System.out.println("3、打印用户列表");
 23             System.out.println("4、注销账户");
 24             System.out.println("5、对支票账户扣税");
 25             System.out.println("6、给储蓄账户发放利息");
 26             System.out.println("其他、退出操作");
 27             System.out.print("请输入:");
 28             select1 = sc.nextInt();
 29             switch (select1)
 30             {
 31                 case 1:
 32                     System.out.println("如果你想要创建储蓄账户,请输入1,如果你想要创建支票账户,请输入2:");
 33                     int select2 = sc.nextInt();      //select2为建立储蓄||支票 账户
 34                     id=Method.createUser(a, select2);
 35                     System.out.println("你的用户名为:" + id);
 36                     System.out.print("请设置密码:");
 37                     password = sc.next();
 38                     if(select2==1)
 39                     {
 40                         BankAccount user=new SavingAccount(id,password,0);
 41                         USER[count++]=user;
 42                     }
 43                     else if(select2==2)
 44                     {
 45                         BankAccount user=new CheckingAccount(id,password,0);
 46                         USER[count++]=user;
 47                     }
 48                     else
 49                     {
 50                         System.out.println("非法输入!!!");
 51                     }
 52                     break;
 53                 case 2:
 54                     System.out.println("请输入账户:");
 55                     id = sc.next();
 56                     System.out.println("请输入密码:");
 57                     password = sc.next();
 58                     int judge=Method.Login(id,password,USER,count);
 59                     if(judge!=-1)
 60                     {
 61                         boolean bool2=true;
 62                         while (bool2)
 63                         {
 64                             System.out.println("请输入想要进行的操作编号");
 65                             System.out.println("1、存款");
 66                             System.out.println("2、取款");
 67                             System.out.println("3、查余额");
 68                             System.out.println("4、转账");
 69                             System.out.println("5、退卡");
 70                             int select3 = sc.nextInt();    //select3为用户进行的一系列操作
 71                             switch (select3)
 72                             {
 73                                 case 1:
 74                                     System.out.println("请输入存款金额:");
 75                                     amount = sc.nextDouble();
 76                                     USER[judge].deposit(amount);
 77                                     System.out.println("当前余额为: " + USER[judge].getBalance());
 78                                     break;
 79                                 case 2:
 80                                     System.out.println("请输入取款金额:");
 81                                     amount = sc.nextDouble();
 82                                     USER[judge].withdraw(amount);
 83                                     System.out.println("当前余额为: " + USER[judge].getBalance());
 84                                     break;
 85                                 case 3:
 86                                     System.out.println("当前余额为: " + USER[judge].getBalance());
 87                                     break;
 88                                 case 4:
 89                                     String payee;
 90                                     System.out.println("请输入收款人:");
 91                                     payee = sc.next();
 92                                     System.out.println("请输入转账金额:");
 93                                     amount = sc.nextDouble();
 94                                     int z=Method.Login(payee,USER,count);
 95                                     if(z!=-1)
 96                                     {
 97                                         USER[judge].transferAccount(USER[z],amount);
 98                                         System.out.println("当前余额为: " + USER[judge].getBalance());
 99                                     }
100                                     else
101                                     {
102                                         System.out.println("收款人不存在!");
103                                     }
104                                     break;
105                                 default:
106                                     bool2=false;
107                             }
108                             System.out.println("----------------------------------------------------------");
109                         }
110                     }
111                     else
112                     {
113                         System.out.println("登录失败!");
114                     }
115                     break;
116                 case 3:
117                     Method.outPut1(USER,count);
118                     break;
119                 case 4:
120                     System.out.println("请输入要注销的用户名:");
121                     id=sc.next();
122                     System.out.println("请输入密码以验证身份:");
123                     password=sc.next();
124                     Method.logOff(id,password,USER,count--);
125                     break;
126                 case 5:
127                     Method.taxDeduction(USER,count);
128                     System.out.println("扣完税后的各账户余额为:");
129                     Method.outPut2(USER,count);
130                     break;
131                 case 6:
132                     Method.interestPayment(USER,count);
133                     System.out.println("加完利息后的各账户余额为:");
134                     Method.outPut2(USER,count);
135                     break;
136                 default:
137                     bool3=false;
138             }
139             System.out.println("=========================================================");
140         }
141     }
142 }

 

posted @ 2020-11-20 00:20  喃南瓜  阅读(189)  评论(0)    收藏  举报