经典的银行管理系统初步实现(练习1-4)

练习1:(面向对象基础语法)
写一个账户类(Account),属性:
id:账户号码 长整数
password:账户密码 字符串类型
name:真实姓名 字符串类型
identity:身份证号码 字符串类型
email:客户的电子邮箱 字符串类型
balance:账户余额 double

方法:
deposit: 存款方法,参数是double型的金额
withdraw:取款方法,参数是double型的金额

构造方法:
有参和无参,有参构造方法用于设置必要的属性
主方法测试你写的类

练习2:(封装)
将Account类作成完全封装(将所有的属性用private修饰)

 

 1 package com.firstweek.pratise2;

 2 

 3 public abstract class Account {

 4     private long id;

 5     private String password;

 6     private String name;

 7     private String identity;

 8     private String email;

 9     private double balance;

10     

11     public long getId() {

12         return id;

13     }

14     public void setId(long id) {

15         this.id = id;

16     }

17     public String getPasswordl() {

18         return password;

19     }

20     public void setPassword(String password) {

21         this.password = password;

22     }

23     public String getName() {

24         return name;

25     }

26     public void setName(String name) {

27         this.name = name;

28     }

29     public String getIdentity() {

30         return identity;

31     }

32     public void setIdentity(String identity) {

33         this.identity = identity;

34     }

35     public String getEmail() {

36         return email;

37     }

38     public void setEmail(String email) {

39         this.email = email;

40     }

41     public double getBalance() {

42         return balance;

43     }

44     public void setBalance(double balance) {

45             this.balance = balance;

46     }

47     

48     public void deposit(double amount) {

49             this.balance += amount;

50     }

51     

52     public abstract void withdraw(double amount);

53         

54     public Account() {}

55         

56     public Account(long id,String password,String name,String identity,

57             String email,double balance) {

58             this.id = id;

59             this.password = password;

60             this.name = name;

61             this.identity = identity;

62             this.email = email;

63             this.balance = balance;

64         }

65 }

练习3:(继承,多态)
银行的客户分为两类,储蓄账户(SavingAccount)和信用账户(CreditAccount),区别在于储蓄账户不允许透支,而信用账户可以透支,并允许用户设置自己的透支额度.
注意:CreditAccount需要多一个属性 ceiling 透支额度
为这两种用户编写相关的类

SavingAccount类:

package com.firstweek.pratise3;



import com.firstweek.pratise2.Account;



public class SavingAccount extends Account {

    

    private int acountType;

    

    public int getAcountType() {

        return acountType;

    }



    public void setAcountType(int acountType) {

        this.acountType = acountType;

    }

    

    public SavingAccount() {}

    

    public SavingAccount(long id,String password,String name,String identity,

            String email,double balance) {

        super(id, password, name, identity, email, balance);

    }



    @Override

    public void withdraw(double amount) {

        // TODO Auto-generated method stub

        if (this.getBalance() >= amount) this.setBalance(this.getBalance() - amount);

    }

}

CreditAccount类:

package com.firstweek.pratise3;



import com.firstweek.pratise2.Account;



public class CreditAccount extends Account {

    

    private int accountType;

    private double ceiling = 0;



    public int getAccountType() {

        return accountType;

    }



    public void setAccountType(int accountType) {

        this.accountType = accountType;

    }



    public double getCeiling() {

        return ceiling;

    }



    public void setCeiling(double ceiling) {

            this.ceiling = ceiling;

    }

    

    public CreditAccount() {}

    

    public CreditAccount(long id,String password,String name,String identity,

            String email, int balance) {

        super(id, password, name, identity, email, balance);

    }



    @Override

    public void withdraw(double amount) {

        // TODO Auto-generated method stub

        if (amount - this.getBalance() <= this.getCeiling()) this.setBalance(this.getBalance() - amount);

    }



}

同时要求编写Bank类,属性(可选):
1.当前所有的账户对象的集合,存放在数组中
2.当前账户数量

方法:
1.用户开户,需要的参数:id,密码,密码确认,姓名,身份证号码,邮箱,账户类型,返回新创建的Account对象,提示:用s1.equals(s2) 可以比较s1,s2两个字符串的值是否相等.账户类型是一个整数,为0的时候表示储蓄账户,为1的时候表示信用账户
2.用户登录,参数:id,密码 返回Account对象
3.用户存款,参数:id,存款数额,返回修改过的Account对象
4.用户取款,参数:id,取款数额,返回修改过的Account对象
5.设置透支额度 参数:id,新的额度 ,返回修改过的Account对象.这个方法需要验证账户是否是信用账户

用户会通过调用Bank对象以上的方法来操作自己的账户,请分析各个方法需要的参数

另外,请为Bank类添加几个统计方法
1.统计银行所有账户余额总数
2.统计所有信用账户透支额度总数

 

package com.firstweek.pratiseEx;



import java.util.Collection;

import java.util.HashMap;

import java.util.Iterator;



import com.firstweek.pratise2.Account;

import com.firstweek.pratise3.CreditAccount;

import com.firstweek.pratise3.SavingAccount;



public class Bank {

    

    private HashMap accountArray =new HashMap();

    

    public HashMap getAccountArray() {

        return accountArray;

    }

    public void setAccountArray(HashMap accountArray) {

        this.accountArray = accountArray;

    }

    

    public Account accountCreat(long id,String password,String name,String identity,

            String email,int balance,int accoutType) {

        

        Account account = null;

        

        switch (accoutType) {

            case 0:

                account = new SavingAccount(id, password, name, identity, email, balance);

                break;

            case 1:

                account = new CreditAccount(id, password, name, identity, email, balance);

                break;

        }

        accountArray.put(id, account);

        return account ;

    }

    

    public Account accountLogin(long id,String password) {

        

        Account account = (Account)accountArray.get(id);

        if (!accountArray.containsKey(id) )

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

        if(!password.equals(account.getPasswordl()))

            System.out.println("密码不正确");

        

        return account;  

    }

    

    public Account accountDeposit(long id,int money) {

        Account account = (Account)accountArray.get(id);

        account.deposit(money);

        return account;

    }

    

    public Account accountWithdraw(long id,int money) {

        Account account = (Account)accountArray.get(id);

        account.withdraw(money);

        return account;

    }

    

    public Account setCeilingValue(long id,double ceiling) {

        Account account = (Account)accountArray.get(id);

        if (account instanceof CreditAccount) {

            CreditAccount creditAccount = (CreditAccount)account;

            creditAccount.setCeiling(ceiling);

        }

        return account;

    }

    

    public double getAccountsBalance() {

        double totalBalance = 0;

        Collection collection = accountArray.values();

        Iterator iterator = collection.iterator();

        while(iterator.hasNext()) {

            Account account = (Account)iterator.next();

            totalBalance += account.getBalance();

        }

        return totalBalance;

    }

    

    public double getAccountsCeiling() {

        double totalCeiling = 0;

        Collection collection = accountArray.values();

        Iterator iterator = collection.iterator();

        while(iterator.hasNext()) {

            Account account =(Account)iterator.next();

            if (account instanceof CreditAccount) {

                CreditAccount creditAccount = (CreditAccount)account;

                totalCeiling += creditAccount.getCeiling();

            }

        }

        return totalCeiling;

    }

}

你可能感兴趣的:(管理)