题目描述(商业ATM机)
Project2: ATM machine
Problem Description:
Design and implement a class named Account that contains:
- An int data field named id for the account.
- A double data field named balance for the account.
- A double data field named annualInterestRate that stores the current interest rate.
- A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0.
- The accessor and mutator functions for id, balance, and annualInterestRate.
- A function named getMonthlyInterestRate() that returns the monthly interest rate.
- A function named withdraw(amount) that withdraws a specified amount from the account.
- 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;
}
}
}