10_1.h
#ifndef _10_1_H_
#define _10_1_H_
#include <string>
class BankAccount {
public:
BankAccount(const std::string& client,
const std::string& num,
double bal = 0.0);
void show() const;
void deposit (double cash);
void withdraw (double cash);
private:
std::string _name;
std::string _account;
double _balance;
};
#endif
10_1.cpp
#include "10_1.h"
#include <cctype>
#include <iostream>
BankAccount::BankAccount(const std::string& client,
const std::string& num,
double bal) {
_name = client;
_account = num;
_balance = bal;
}
void BankAccount::show() const {
using std::cout;
using std::ios_base;
// set format to #.##
ios_base::fmtflags orig =
cout.setf(ios_base::fixed, ios_base::floatfield);
std::streamsize prec = cout.precision(2);
cout << "Client: " << _name
<< ", Account: " << _account
<< ", Balance: " << _balance << std::endl;
// restore original format
cout.setf(orig, ios_base::floatfield);
cout.precision(prec);
}
void BankAccount::deposit(double cash) {
if(cash <= 0.0) {
std::cout << "Invalid num of " << cash << "!" << std::endl;
return;
}
_balance += cash;
std::cout << "Deposit success!" << std::endl;
}
void BankAccount::withdraw(double cash) {
if(cash > _balance) {
std::cout << cash << " is larger than bank balance!"<< std::endl;
return;
}
_balance -= cash;
std::cout << "Withdraw success!" << std::endl;
}
int main() {
std::string name, account;
double balance, cash;
std::cout << "Please enter client name:" << std::endl;
std::cin >> name;
std::cin.get();
std::cout << "Please enter client account num:" << std::endl;
std::cin >> account;
std::cin.get();
std::cout << "Please enter client balance:" << std::endl;
std::cin >> balance;
if (name.empty() || account.empty() || balance < 0.0) {
std::cout << "Invalid argument!" << std::endl;
return 0;
}
BankAccount client(name, account, balance);
client.show();
std::cout << "Please enter D to deposit money,"
<< "W to withdraw cash, or Q to quit.\n";
char ch;
while (std::cin >> ch && toupper(ch) != 'Q')
{
while (std::cin.get() != '\n')
continue;
if (!isalpha(ch)) {
std::cout << '\a';
continue;
}
switch (ch) {
case 'd':
case 'D':
std::cout << "Enter the cash you want to deposit:";
std::cin >> cash;
client.deposit(cash);
break;
case 'w':
case 'W':
std::cout << "Enter the cash you want to withdraw:";
std::cin >> cash;
client.withdraw(cash);
}
client.show();
std::cout << "Please enter D to deposit money,\n"
<< "W to withdraw cash, or Q to quit.\n";
}
}
10_2.h
#ifndef _10_2_H_
#define _10_2_H_
#include <string>
clas