IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Java程序设计实验九(异常处理) bank项目开发 -> 正文阅读

[Java知识库]Java程序设计实验九(异常处理) bank项目开发

实验九:修改实验八完成的项目,使其具有异常处理的功能

一、实验目的

练习在项目中使用Java的异常处理机制。

二、实验内容

将建立一个 OverdraftException 异常类,它由 Account 类的withdraw()方法声明抛出。
UML类图如下图所示:
在这里插入图片描述

【实验步骤】

1. 在 banking.domain 包中建立一个公共类 OverdraftException, 这个类继承Exception 类。
2. 添加一个 double 类型的私有属性 deficit,增加一个共有访问方法getDeficit()。
3. 添加一个有两个参数的共有构造器。deficit 参数初始化 deficit 属性。
4. 修改 Account 类,重写 withdraw 方法使它不返回值(即 void),声明方法抛出。
OverdraftException异常,修改withdraw 方法声明抛出OverdraftException异常,指明“资金不足”以及不足数额(当前余额扣除请求的数额)
5. 修改 CheckingAccount 类,重写 withdraw 方法使它不返回值(即 void),声明方法抛出OverdraftException异常,修改withdraw 方法使其在需要时抛出异常。
分两种情况要处理:
第一是存在没有透支保护的赤字,对这个异常使用 “no overdraft protection”信息。
第二是透支保护数额不足以弥补赤字 : 对 这 个 异 常 可 使用 ”Insufficient funds for overdraft protection” 信息。

运行 TestBanking 类输出以下内容:
Customer [simms,Jane]has a checking balance of 200.0 with a 500.0
overdraft protection
Checking Acct[Jane Simms]: withdraw 150.00
Checking Acct[Jane Simms]: deposit 22.50
Checking Acct[Jane Simms]: withdraw 147.62
Checking Acct[Jane Simms]: withdraw 470.00
Exception: Insufficient funds for overdraft protection Deifcit:470.0

Customer [Simms,Jane]has a checking balance of 0.0
Customer [Bryant,Owen]has a checking balance of 200.0
Checking Acct[Bryant,Owen]: withdraw 100.00
Checking Acct[Bryant,Owen]: deposit25.00
Checking Acct[Bryant,Owen]: withdraw 175.00
Exception: no overdraft protection Deficit:50.0
Customer [Bryant,Owen]has a checking balance of 125.0

四、程序代码

程序结构目录如下图所示:
在这里插入图片描述

4.1 Account.java

package banking.domain;

public class Account {
    protected double balance;
    public Account(double init_balance)
    {
        balance=init_balance;
    }
    public double getBalance()
    {
        return balance;
    }
    public boolean deposit(double amt)
    {
        balance=balance+amt;
        return true;
    }

    public void withdraw (double amt)throws OverdraftException
    {
        if(balance>=amt){
            balance=balance-amt;
        }
        else{
            throw new OverdraftException("金额不足",amt-balance);
        }
    }
    public String getTypeOfAccounts(Account account){
        if(account instanceof SavingsAccount){
            return "Savings Account: ";
        }
        else {
            return "Checking Account: ";
        }
    }
}

4.2 Bank.java

package banking.domain;

public class Bank {
    private Customer[] customers;
    private int numberOfCustomer;
    private static Bank bank = new Bank();
    public Bank(){
        customers=new Customer[10];
        numberOfCustomer=0;
    }
    public void addCustomer(String f,String l){
        customers[numberOfCustomer]=new Customer(f,l);
        numberOfCustomer++;
    }
    public int getNumOfCustomers(){
        return numberOfCustomer;
    }
    public Customer getCustomer(int index){
        return customers[index];
    }
    public static Bank getBank(){
        return bank;
    }
}

4.3 CheckingAccount.java

package banking.domain;

public class CheckingAccount extends Account {
    private double protectedBy;

    public CheckingAccount(double balance) {
        super(balance);
    }
    public CheckingAccount(double balance, double protect) {
        super(balance);
        this.protectedBy=protect;
    }
    public void withdraw(double amt) throws OverdraftException{
        if(protectedBy==0)
        {
            if(balance>=amt){
                balance=balance-amt;
            }
            else{
                throw new OverdraftException("no overdraft protection Deficit:",amt-balance);
            }
        }
        else{
            if(amt <= balance + protectedBy){
                balance = balance - amt;
                if(balance<0){
                    protectedBy+=balance;
                    balance=0;
                }
            }
            else {
                throw new OverdraftException("Insufficient funds for overdraft protection Deifcit:",amt);
            }
        }
    }
}

4.4 Customer.java

package banking.domain;

public class Customer {
    private String firstName;
    private String lastName;
    private Account[] account=new Account[10];
    private SavingsAccount savingsAccount;
    private CheckingAccount checkingAccount;
    private int numberOfAccount;
    public Customer(String f,String l){
        this.firstName=f;
        this.lastName=l;
    }
    public String getFirstName(){
        return this.firstName;
    }
    public String getLastName(){
        return this.lastName;
    }
    public Account getAccount(int number)
    {
        return this.account[number];
    }
    public int getNumOfAccounts(){
        return numberOfAccount;
    }
    public void addAccount(Account newAccount){
        account[numberOfAccount++]=newAccount;
    }
    public SavingsAccount getSavings(){
        return savingsAccount;
    }
    public CheckingAccount getChecking(){
        return checkingAccount;
    }
    public void setSavings(SavingsAccount saving){
        savingsAccount=saving;
    }
    public  void setChecking(CheckingAccount checking){
        checkingAccount=checking;
    }
}

4.5 OverdraftException.java

package banking.domain;

public class OverdraftException extends Exception{
    private double deficit;
    OverdraftException(String message,double deficit){
        super(message);
        this.deficit=deficit;
    }
    public double getDeficit(){
        return this.deficit;
    }
}

4.6 SavingsAccount.java

package banking.domain;

import banking.domain.Account;

public class SavingsAccount extends Account {
    double insterstRate;

    public SavingsAccount(double balance, double insterst_rate) {
        super(balance);
        insterstRate = insterst_rate;
    }
}

4.7 CustomerReport.java

package banking.reports;

import  banking.domain.*;
public class CustomerReport {
    Bank bank = Bank.getBank();
    Customer customer;
    Account account;
    public void generateReport(){
        System.out.println("\t\t\tCUSTOMER REPORT ");
        System.out.println("\t\t\t=================");
        for(int index=0;index< bank.getNumOfCustomers();index++){
            customer= bank.getCustomer(index);
            System.out.println("Customer:"+customer.getLastName()+","+customer.getFirstName());
            for(int i=0;i< customer.getNumOfAccounts();i++) {
                account = customer.getAccount(i);
                System.out.println(account.getTypeOfAccounts(account)+"current balance is $"+account.getBalance());
            }
            System.out.println();
        }
    }
}

4.8 TestBanking.java

package test;

import banking.domain.*;

public class TestBanking {

    public static void main(String[] args) {
        Bank bank = Bank.getBank();
        Customer customer;
        Account account;

        // Create two customers and their accounts
        bank.addCustomer("Jane", "Simms");
        customer = bank.getCustomer(0);
        customer.addAccount(new SavingsAccount(500.00, 0.05));
        customer.addAccount(new CheckingAccount(200.00, 500.00));
        bank.addCustomer("Owen", "Bryant");
        customer = bank.getCustomer(1);
        customer.addAccount(new CheckingAccount(200.00));

        // Test the checking account of Jane Simms (with overdraft protection)
        customer = bank.getCustomer(0);
        account = customer.getAccount(1);
        System.out.println("Customer [" + customer.getLastName()
                + ", " + customer.getFirstName() + "]"
                + " has a checking balance of "
                + account.getBalance()
                + " with a 500.00 overdraft protection.");
        try {
            System.out.println("Checking Acct [Jane Simms] : withdraw 150.00");
            account.withdraw(150.00);
            System.out.println("Checking Acct [Jane Simms] : deposit 22.50");
            account.deposit(22.50);
            System.out.println("Checking Acct [Jane Simms] : withdraw 147.62");
            account.withdraw(147.62);
            System.out.println("Checking Acct [Jane Simms] : withdraw 470.00");
            account.withdraw(470.00);
        } catch (OverdraftException e1) {
            System.out.println("Exception: " + e1.getMessage()
                    + "   Deficit: " + e1.getDeficit());
        } finally {
            System.out.println("Customer [" + customer.getLastName()
                    + ", " + customer.getFirstName() + "]"
                    + " has a checking balance of "
                    + account.getBalance());
        }
        System.out.println();

        // Test the checking account of Owen Bryant (without overdraft protection)
        customer = bank.getCustomer(1);
        account = customer.getAccount(0);
        System.out.println("Customer [" + customer.getLastName()
                + ", " + customer.getFirstName() + "]"
                + " has a checking balance of "
                + account.getBalance());
        try {
            System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00");
            account.withdraw(100.00);
            System.out.println("Checking Acct [Owen Bryant] : deposit 25.00");
            account.deposit(25.00);
            System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00");
            account.withdraw(175.00);
        } catch (OverdraftException e1) {
            System.out.println("Exception: " + e1.getMessage()
                    + "   Deficit: " + e1.getDeficit());
        } finally {
            System.out.println("Customer [" + customer.getLastName()
                    + ", " + customer.getFirstName() + "]"
                    + " has a checking balance of "
                    + account.getBalance());
        }
    }
}
  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-11-12 19:27:07  更:2021-11-12 19:28:02 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 0:38:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码