java 筆記一些

面象對的umv 視圖示例:數組

用戶  銀行  賬戶  怎麼關聯ide

賬戶代碼:函數

文件命名 Accountui

 

package com.atuigu.exer4;

import org.omg.CORBA.PUBLIC_MEMBER;

public class Account {
    private double balance;
    
    public Account(double init_balance) {
        this.balance = init_balance;
       
    }
    
    public double getBalance() {
        return balance;
    }

    public void deposit(double amt) {
        if(amt>0) {
            balance +=amt;
            System.out.println("存錢成功");
        }        
    }
    
    public void withdraw(double amt) {
        if(balance>=amt) {
            balance-=amt;
            System.out.println("餘額不足");
        }
    }//
    
    
}//主函數結束
View Code

銀行代碼:文件命名:Customerthis

package com.atuigu.exer4;

public class Customer {
    private String firstName;
    private String lastName;
    private Account account;
    public Customer(String f, String l) {
        this.firstName = f;
        this.lastName = l;
        
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public Account getAccount() {
        return account;
    }
    public void setAccount(Account account) {
        this.account = account;
    }
    

}
View Code

用戶代碼:文件命名:Bankspa

package com.atuigu.exer4;

public class Bank {
    private Customer[] customers;
    //定義一個能夠裝不少用戶的數組類;
    private int  numberofCustomer;
    
    public Bank() {
        customers = new Customer[10];
        
    }
    
    public void addCustomer(String f,String l) {
        Customer cust = new Customer(f,l);
        customers[numberofCustomer] = cust;
        numberofCustomer++;
        
    }
    
    public int getNumofCustomers(){
        return numberofCustomer;
        
    }
    
    public Customer getCustomer(int index) {
        if(index>=0 && index < numberofCustomer){
        return customers[index];
        }
        return null;
        
        
    }
    
    
    
        
    }
View Code

主函數代碼示例:文件命名:BankTestcode

package com.atuigu.exer4;

public class BankTest {
    public static void main(String[] args) {
        Bank bank = new Bank();
        bank.addCustomer("jane","Smith");
        bank.addCustomer("ok", "err");
        
        Account a = new Account(2000);
        
        bank.getCustomer(0).setAccount(a);
        
        bank.getCustomer(0).getAccount().withdraw(500);
        double balance = bank.getCustomer(0).getAccount().getBalance();
        System.out.print(""+bank.getCustomer(0).getFirstName());
        System.out.println("奶奶的銀業有我少個"+ bank.getNumofCustomers());
        
        
        
    }

}
View Code
相關文章
相關標籤/搜索