尚硅谷--Java基础实战_Bank项目01—08(全部试验项目)

这篇博客详细介绍了从Bank项目01到08的全过程,涵盖`Account.java`、`Customer.java`、`Bank.java`等核心类的实现,逐步引入CheckingAccount和SavingsAccount,以及异常处理和客户报告功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java基础实战_Bank项目01

目录结构

在这里插入图片描述
不要加01,这里是为了区分

【Account.java】类

package banking;

public class Account {
   

    private double balance;    //银行帐户的当前(或即时)余额

    //公有构造器 ,这个参数为 balance 属性赋值
    public Account(double init_balance) {
   
        this.balance = init_balance;
    }

    //用于获取经常余额
    public double getBalance() {
   
        return balance;
    }

    //向当前余额增加金额
    public void deposit(double amt){
   
        balance+=amt;
    }

    //从当前余额中减去金额
    public void withdraw(double amt){
   
        balance-=amt;
    }
}

【TestBanking】类

package banking;
import banking.*;

public class TestBanking {
   

  public static void main(String[] args) {
   
    Account  account;

    // Create an account that can has a 500.00 balance.
    System.out.println("Creating an account with a 500.00 balance.");
    //code
    account = new Account(500.00);

    System.out.println("Withdraw 150.00");
   	//code
    account.withdraw(150.00);

    System.out.println("Deposit 22.50");
	//code
    account.deposit(22.50);

    System.out.println("Withdraw 47.62");
   	//code
    account.withdraw(47.62);

    // Print out the final account balance
    System.out.println("The account has a balance of " + account.getBalance());
  }
}

Java基础实战_Bank项目02

目录结构

在这里插入图片描述
自己做的话不需要加02,这里是为了区分

【Account.java】类

package banking;

public class Account {
   

    private double balance;    //银行帐户的当前(或即时)余额

    //公有构造器 ,这个参数为 balance 属性赋值
    public Account(double init_balance) {
   
        this.balance = init_balance;
    }

    //用于获取经常余额
    public double getBalance() {
   
        return balance;
    }

    //向当前余额增加金额
    public void deposit(double amt){
   
        balance+=amt;
    }

    //从当前余额中减去金额
    public void withdraw(double amt){
   
        balance-=amt;
    }
}

【Customer.java】类

package banking;

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 String getLastName() {
   
        return lastName;
    }

    public Account getAccount() {
   
        return account;
    }

    public void setAccount(Account acct) {
   
        this.account = acct;
    }
}

【TestBanking.java】类

package banking;/*
 * This class creates the program to test the banking classes.
 * It creates a new Bank, sets the Customer (with an initial balance),
 * and performs a series of transactions with the Account object.
 */

import banking.*;

public class TestBanking {
   

  public static void main(String[] args) {
   
    Customer customer;
    Account  account;

    // Create an account that can has a 500.00 balance.
    System.out.println("Creating the customer Jane Smith.");
    //code
    customer = new Customer("Jane","Smith");
    System.out.println("Creating her account with a 500.00 balance.");
    //code
    account = new Account(500.00);
    customer.setAccount(account);

    System.out.println("Withdraw 150.00");
	//code
    customer.getAccount().withdraw(150.00);

    System.out.println("Deposit 22.50");
  	//code
    customer.getAccount().deposit(22.50);

    System.out.println("Withdraw 47.62");
   	//code
    customer.getAccount().withdraw(47.62);

    // Print out the final account balance
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName()
		       + "] has a balance of " + account.getBalance());
  }
}

Java基础实战_Bank项目03

目录结构

在这里插入图片描述
自己做的话不需要加03,这里是为了区分

【Account.java】类

package banking;

public class Account {
   

    private double balance;    //银行帐户的当前(或即时)余额

    //公有构造器 ,这个参数为 balance 属性赋值
    public Account(double init_balance) {
   
        this.balance = init_balance;
    }

    //用于获取经常余额
    public double getBalance() {
   
        return balance;
    }

    /**
     * 向当前余额增加金额
     * @param amt   增加金额
     * @return  返回 true(意味所有存款是成功的)
     */
    public boolean deposit(double amt){
   
        balance+=amt;
        return true;
    }

    /**
     * 从当前余额中减去金额
     * @param amt   提款数目
     * @return  如果 amt小于 balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。
     */
    public boolean withdraw(double amt){
   
        if (amt < balance){
   
            balance-=amt;
            return true;
        }else{
   
            return false;
        }

    }
}

【Customer.java】类

package banking;

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 String getLastName() {
   
        return lastName;
    }

    public Account getAccount() {
   
        return account;
    }

    public void setAccount(Account acct) {
   
        this.account = acct;
    }
}

【TestBanking.java】类

package banking;/*
 * This class creates the program to test the banking classes.
 * It creates a new Bank, sets the Customer (with an initial balance),
 * and performs a series of transactions with the Account object.
 */

import banking.*;

public class TestBanking {
   

  public static void main(String[] args) {
   
    Customer customer;
    Account  account;

    // Create an account that can has a 500.00 balance.
    System.out.println("Creating the customer Jane Smith.");
	//code
    customer = new Customer("Jane","Smith");
    System.out.println("Creating her account with a 500.00 balance.");
	//code
    account = new Account(500.00);
    customer.setAccount(account);

    // Perform some account transactions
    System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
    System.out.println("Deposit 22.50: " + account.deposit(22.50));
    System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
    System.out.println("Withdraw 400.00: " + account.withdraw(400.00));

    // Print out the final account balance
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName()
		       + "] has a balance of " + account.getBalance());
  }
}

Java基础实战_Bank项目04

目录结构

在这里插入图片描述

【Account.java】类

package banking;


public class Account {
   

    private double balance;    //银行帐户的当前(或即时)余额

    //公有构造器 ,这个参数为 balance 属性赋值
    public Account(double init_balance) {
   
        this.balance = init_balance;
    }

    //用于获取经常余额
    public double getBalance() {
   
        return balance;
    }

    /**
     * 向当前余额增加金额
     * @param amt   增加金额
     * @return  返回 true(意味所有存款是成功的)
     */
    public boolean deposit(double amt){
   
        balance+=amt;
        return true;
    }

    /**
     * 从当前余额中减去金额
     * @param amt   提款数目
     * @return  如果 amt小于 balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。
     */
    public boolean withdraw(double amt){
   
        if (amt < balance){
   
            balance-=amt;
            return true;
        }else{
   
            return false;
        }

    }
}

【Customer.java】类

package banking;

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 String getLastName() {
   
        return lastName;
    }

    public Account getAccount() {
   
        return account;
    }

    public void setAccount(Account acct) {
   
        this.account = acct;
    }
}

【Bank.java】类

package banking;

/**
 * 银行类
 */
public class Bank {
   

    private Customer[] customers;   //Customer对象的数组
    private int numberOfCustomer;   //整数,跟踪下一个 customers 数组索引

    /**
     * 公有构造器,以合适的最大尺寸(至少大于 5)初始化 customers 数组。
     */
    public Bank() {
   
        customers = new Customer[10];
    }

    /**
     *  该方法必须依照参数(姓,名)构造一个新的 Customer 对象然后把它放到 customer 数组中。还必须把 numberOfCustomers 属性的值加 1。
     * @param f 姓
     * @param l 名
     */
    public void addCustomer(String f,String l){
   
        customers[numberOfCustomer++]=new Customer(f,l);
    }

    /**
     * 通过下标索引获取 customer
     * @param index 下标索引
     * @return  customer
     */
    public Customer getCustomer(int index) {
   
        return customers[index]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值