1352 [BA1002] The Account class

Description

Deisgn a class named Account that contains:
An int data field named id for the account (default 0).
A double data field named balance for the account (default 0).
A double data field named annualInterestRate that stores the current interest rate (default 0).
A no-arg constructor that creates a default account.
The accessor and mutator functions for id, balance, and annualInterestRate.
A function named getMonthlyInterestRate() that returns the monthly interest rate. (Hint: monthlyInterestRate = annualInterestRate / 12)
A function name withDraw that withdraws a specified amount from the account.
A function name deposit that deposits a specified amount to the account.

Provided Codes

framework.cpp

#include <iostream>
#include <iomanip>
#include "source.h"
using namespace std;
void print(Account& account)
{
  cout << "Account ID " << account.getId() 
  << fixed << setprecision(2)
  << ", Balance " << account.getBalance() << ", Monthly Interest Rate " << account.getMonthlyInterestRate() << endl;
}
int main()
{


  Account a, b;
  print(a);

  b.setId(1122);
  b.setBalance(20000.0);
  b.setAnnualInterestRate(4.5);
  print(b);

  return 0;
}

Submission

source.h

 class Account{
public:
    Account();
    void setId(int );
    void setBalance(double =0);
    void setAnnualInterestRate(double =0);
    int getId();
    double getBalance(); 
    double getMonthlyInterestRate();
    void withDraw(double );
    void doposit(double );
private:
    int id;
    double balance;
    double annualInterestRate;
};

Account::Account(){
    id=0;
}

void Account::setId(int Id){
    id=Id;
}

void Account::setBalance(double Balance){
    balance=Balance;
}

void Account::setAnnualInterestRate(double Annual){
    annualInterestRate=Annual;
}

int Account::getId(){
    return id;
}

double Account::getBalance(){
    return balance;
}

double Account::getMonthlyInterestRate(){
    return annualInterestRate/12;
}

void Account::withDraw(double payout){
    balance-=payout;
}

void Account::doposit(double income){
    balance+=income;
}

Standard Answer

source.h

// Problem#: 17645
// Submission#: 4642239
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<string>
using namespace std;

class Account {
private:
    int id;
    double balance, annualInterestRate;
public:
    Account(int ID = 0, double BALANCE = 0, double AnnualInterestRate = 0) {
        id = ID;
        balance = BALANCE;
        annualInterestRate = AnnualInterestRate;
    }
    int getId() {
        return id;
    }
    double getBalance() {
        return balance;
    }
    double getAnnualInterestRate() {
        return annualInterestRate;
    }

    void setId(int ID) {
        id = ID;
    }
    void setBalance(double Balance) {
        balance = Balance;
    }
    void setAnnualInterestRate(double AnnualInterestRate) {
        annualInterestRate = AnnualInterestRate;
    }

    double getMonthlyInterestRate() {
        return annualInterestRate/12;
    }
    void withDraw(double amount) {
        balance -= amount;
        if ( balance < 0 ) balance = 0;
    }
    void deposit(double amount) {
        balance += amount;
    }
};                                 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值