用java写出来:
package day09;
public class Account {
private int id;//账号
protected double balance;//余额
private double yearRate;//年利率
public Account(int id, double balance, double yearRate) {
super();
this.id = id;
this.balance = balance;
this.yearRate = yearRate;
}
//月利率
public double monthRate(double yearRate)
{
return yearRate/12;
}
// 存款方法
public double deposit(double amount)
{
if(amount<=0)
return -1;
balance+=amount;
System.out.println("存款成功!存入金额为:"+amount+"总金额为:"+balance);
return balance;
}
//各种set和get方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getYearRate() {
return yearRate;
}
public void setYearRate(double yearRate) {
this.yearRate = yearRate;
}
// 取款方法
public void withdraw(double amount) {
// TODO Auto-generated method stub
if(amount>balance)
{
System.out.println("余额不足,取款失败!");
}
else {
balance-=amount;
System.out.println("取款成功!取款金额为:"+ amount +"余额为:"+balance);
}
}
}
package day09;
public class AccountTest {
public static void main(String[] args) {
Account acct=new Account(1122,200000,0.0045);
acct.withdraw(30000);
acct.withdraw(2500);
System.out.println("***********************");
acct.deposit(3000);
System.out.println("余额为:"+acct.getBalance()+"月利率为:"+acct.monthRate(acct.getYearRate()));
}
}
运行结果:
用c++实现:
头文件
Account.h
#pragma once
#include
using namespace std;
class Account
{
private:
int id;//身份号
double balance;//余额
double annualInterestRate;//年利率
public :
Account(int id, double balance, double annualInterestRate);
void setId(int id);
void setBalance(double b);
void setAnnualInterestRate(double bal);
int getId();
double getBalance();
double getAnnualInterestRate();
double getMonthlyInterestRate(double annualInterestRate);
//withDraw的函数,从账户中支取指定金额;
void withDraw(int outto);
//)一个名为deposit的函数,向账户中存入指定金额
void deposit(int getto);
};
Account.cpp
#include
#include "Account.h"
using namespace std;
Account A(1122, 20000, 0.045);
Account::Account(int id, double balance, double annualInterestRate) :id(1122), balance(20000), annualInterestRate(0.045) {}
void Account::setId(int id)
{
id = id;
}
void Account::setBalance(double b)
{
balance = b;
}
void Account::setAnnualInterestRate(double bal)
{
annualInterestRate = bal;
}
int Account::getId()
{
return id;
}
double Account::getBalance()
{
return balance;
}
double Account::getAnnualInterestRate()
{
return annualInterestRate;
}
double Account::getMonthlyInterestRate(double annualInterestRate)
{
return annualInterestRate / 12;
}
//withDraw的函数,从账户中支取指定金额;
void Account::withDraw(int outto)
{
if (outto > balance)
{
cout << "余额不足,取款失败!" << endl;
return;
}
else {
balance -= outto;
cout << "取款成功,您当前余额为:" << balance << endl;
cout << "您的月利率为" << A.getMonthlyInterestRate(A.getAnnualInterestRate()) << endl;
}
}
//)一个名为deposit的函数,向账户中存入指定金额
void Account::deposit(int getto)
{
if (getto <= 0)
return;
else {
balance += getto;
cout << "存款成功,您当前余额为:" << balance << endl;
cout << "您的月利率为" << A.getMonthlyInterestRate(A.getAnnualInterestRate()) << endl;
}
}
main.cpp
#include
#include "Account.h"
int main()
{
Account A(1122, 20000, 0.045);
A.withDraw(2500);
A.deposit(3000);
return 0;
}
运行结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)