C++程序设计(PEARSON版)第三版11.7答案

题目描述(商业ATM机)

Project2: ATM machine

Problem Description:

Design and implement a class named Account that contains:

  1. An int data field named id for the account.
  2. A double data field named balance for the account.
  3. A double data field named annualInterestRate that stores the current interest rate.
  4. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0.
  5. The accessor and mutator functions for id, balance, and annualInterestRate.
  6. A function named getMonthlyInterestRate() that returns the monthly interest rate.
  7. A function named withdraw(amount) that withdraws a specified amount from the account.
  8. A function named deposit(amount) that deposits a specified amount to the account.

 

Use the Account class  to simulate an ATM machine. Create ten accounts in an array with id 0, 1, ..., 9, and initial balance $100. The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct one. Once an id is accepted, the main menu is displayed, as shown in the sample run. You can enter a choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing money, and 4 for exiting the main menu. Once you exit, the system will prompt for an id again. So, once the system starts, it will not stop.

 

<Output>

Enter an id: 4

 

Main menu

1: check balance

2: withdraw

3: deposit

4: exit

Enter a choice: 1

The balance is 100.0

Main menu

1: check balance

2: withdraw

3: deposit

4: exit

Enter a choice: 2

Enter an amount to withdraw: 3

 

Main menu

1: check balance

2: withdraw

3: deposit

4: exit

Enter a choice: 1

The balance is 97.0

Main menu

1: check balance

2: withdraw

3: deposit

4: exit

Enter a choice: 3

Enter an amount to deposit: 10

 

Main menu

1: check balance

2: withdraw3: deposit4: exitEnter a choice: 1

The balance is 107.0

Main menu

1: check balance

2: withdraw

3: deposit

4: exit

Enter a choice: 4

 

Enter an id:

下面的代码在VS2017上获得通过

类头文件代码:

#pragma once
class Account
{
public:
	Account();
	~Account();
	int getID();
	double getBanlance();
	double getannualInterestRate();
	void Resetid(int ID);
	void Resetbanlance(double BANLANCE);
	void RestannualInterestRate(double ANNUALINTERESTRATE);
	double getMonthlyInterestRate();
	void withdraw(double amount);
	void deposit(double amount);
private:
	int id;
	double banlance;
	double annualInterestRate;
};

类函数代码:

#include "pch.h"
#include "Account.h"
Account::Account()
{
	id = 0;
	annualInterestRate = 0;
	banlance = 0;
}
int Account::getID()
{
	return id;
}
double Account::getBanlance()
{
	return banlance;
}
double Account::getannualInterestRate()
{
	return annualInterestRate;
}
void Account::Resetid(int ID)
{
	id = ID;
}
void Account::Resetbanlance(double BANLANCE)
{
	banlance = BANLANCE;
}
void Account::RestannualInterestRate(double ANNUALINTERESTRATE)
{
	annualInterestRate = ANNUALINTERESTRATE;
}

double Account::getMonthlyInterestRate()
{
	double monthlyIntetestRate = annualInterestRate / 1200;
	return monthlyIntetestRate;
}
void Account::withdraw(double amount)
{
	banlance -= amount;
}
void Account::deposit(double amount)
{
	banlance += amount;
}
Account::~Account()
{
}

主函数代码:

#include "pch.h"
#include <iostream>
#include<iomanip>
#include "Account.h"
using namespace std;
int main()
{
	int I,n,s;
	Account act[10];
	for (int i = 0; i < 10; i++)
	{
		act[i].Resetbanlance(100.0);
	}
	while (true)
	{
		s = 0;
		cout << "Enter an id:";
		cin >> I;
		if (I >= 0 && I <= 9)
		{
			while (true)
			{
				cout << "Main menu" << endl;
				cout << "1:Check Banlance" << endl;
				cout << "2:withdraw" << endl;
				cout << "3:deposit" << endl;
				cout << "4:exit" << endl;
				cout << "Enter a choice:" ;

				cin >> n;
				if (n == 1)
					cout << "The banlance is " <<fixed<<setprecision(1)<< act[I].getBanlance() << endl;
				else if (n == 2)
				{
					cout << "Enter an amount to withdraw:";
					cin >> s;
					act[I].withdraw(s);
				}
				 else if (n == 3)
				{
					cout << "Enter an amount to deposit:";
					cin >> s;
					act[I].deposit(s);
				}
				else if (n == 4)
					break;
				else
				{
					cout << "Wrong number!,try again!" << endl;
				}

			}
		}
		else
		{
			cout << "Wrong ID,please try again!" << endl;
		//	continue;
		}
	}
}

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C++程序设计 扫描,谭浩强编著,清华大学出社,2004年6月第一。 注意:其他两卷在本网页下面我的其它资源里可以找到 内容简介 C++是近年来国内外广泛使用的现代计算机语言,它既支持面向过程的程序设计,也支持基于对象和面问对象的程序设计。国内许多高校已陆续开设了C++程序设计课程。但是由于C++涉及概念很多,语法比较复杂,内容十分广泛使不少人感到学习难度较大,难以人门。 本书作者深入调查了我国大学的程序设计课程的现状和发展趋势参阅了国内外数十种有关C++的教材,认真分析了学习者在学习过程中遇到的困难,研究了初学者的认识规律。在本书中做到准确定位,合理取舍内容,设计了读者易于学习的教材体系,并且以通俗易懂的语言化解了许多复杂的概念,大大减少了初学者学习C++的困难。 考虑到许多读者在学习C++前未学过其他语言本书把入门起点降低,读者不需要具备C语言的基础。本书包括14章,分为4 篇:基本知识面向过程的程序设计;基于对象的程序设计;面向对象的程序设计。本书内容全面,例题丰富,概念清晰,循序渐进,易于学习。 本书是依据ANSI C++标准进行介绍的,引导读者从开始就按标准C++的规定编程。本书配有两本辅导教材,即《C++程序设计题解与上机指导》 和《C++编程实践指导》。 本书是大学各专业学生学习C++的基础教材,也是适于初学者学习的教材。即使没有教师讲授,读者也能看懂本书的大都分内容

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值