用C++和java分别写简易的银行管理系统

用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<iostream>
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<iostream>
#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<iostream>
#include "Account.h"
int main()
{
	Account A(1122, 20000, 0.045);
	A.withDraw(2500);
	A.deposit(3000);
	return 0;
}

运行结果:

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Allen_5210

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值