15 สิงหาคม 2554

[JAVA]การเขียนโปรแกรมที่ใช้หลักการเชิงออปเจ็ค แบบฝึกหัดที่ 5.3


------------------------------------
             Account
------------------------------------

package Banking;

public class Account {
    protected double balance;
    public Account(double amount){
        balance = amount;
    }
    public void deposit(double amount){
        balance = balance + amount;
    }
    public boolean withdraw(double amount){
        boolean result = true;
        if(amount < balance){
            balance = balance - amount;
        }
        else{
            result = false;
        }
        return result;
    }
    public double getBalance(){
        return balance;
    }
    public void showBalance(){
        System.out.println(balance);
    }
    public Account(){
     
    }
}

------------------------------------
             Customer
------------------------------------

package Banking;

public class Customer {
    private String firstName;
    private String lastName;
    private Account acct[];
    private int AccountNum=0;
    public Customer(String fName,String lName){
        firstName = fName;
        lastName = lName;
        acct = new Account[20];
    }
    public void setFirstName(String fName){
        firstName = fName;
    }
    public String getFirstName(){
        return firstName;
    }
    public void setLastName(String lName){
        lastName = lName;
    }
    public String getLastName(){
        return lastName;
    }
    public void setAccount(Account acct){
        acct = acct;
    }
    public Account getAccount(int index){
        return acct[index];
    }
    public void addAccount(Account acct){
        this.acct[AccountNum]=acct;
        AccountNum++;
    }
    public int getAccountNum(){
        return AccountNum;
    }
    public void printReport()
    {
        for(int i=0;i<getAccountNum();i++)
        System.out.println("Account"+(i+1)+" Balance :"+getAccount(i).getBalance());
    }
}

------------------------------------
           SavingAccount
------------------------------------

package Banking;

public class SavingAccount extends Account {

    public SavingAccount(double amount) {
        balance = amount;
    }
}

------------------------------------
            CheckingAccount
------------------------------------

package Banking;

public class CheckingAccount extends Account {
    private double credit;
    public CheckingAccount(double initBalance,double credit){
        balance = initBalance;
        credit=credit;
    }
    public double getCredit(){
        return credit;
    }
    @Override
    public boolean withdraw(double amount){
        boolean result = true;
        if(amount < balance){
            balance = balance - amount;
        }
        else{
            if(amount > (balance + credit)){
                result = false;
            }
            else{
                balance = 0;
                credit = (balance + credit) - amount;
            }    
        }
        return result;
    }
}

------------------------------------
              Tailer
------------------------------------

import Banking.*;

public class Teller {
    public static void main(String args[]){
     
        Customer cust = new Customer("Somchai","Sommut");
        SavingAccount acct1 = new SavingAccount(3000);
        CheckingAccount acct2 = new CheckingAccount(4000,2000);
     
        cust.addAccount(acct1);
        cust.addAccount(acct2);
     
        acct1.deposit(4200);
        acct1.withdraw(3000);
     
        acct2.deposit(3200);
        acct2.withdraw(1000);
     
        System.out.println(cust.getFirstName()+"  "+cust.getLastName());
        System.out.println("Number of Account : "+cust.getAccountNum());
     
        cust.printReport();
    }  
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น