The problem asks you to take an integer (debit amount) and a double (credit or initial balance amount) and process the requested debit verifying that 1 it's a multiple of the minimum denomimation amount of $5 and that it's also smaller than the credit/balance. If either is untrue, it is supposed to return the initial deposit amount otherwise it will return the new balance.
I have created 3 objects for this problem:
Transaction
- This object reads in the two initial values given and then is used in ATMATM
- Takes the transaction and applies them to the account and then displays the new balance.Account
- This object keeps track of the current account balance and updates the balance if the ATM passes it a value.
Limitations:
I understand that it can only process a single account, but that is more a limitation set by the problem description than it is me not accounting for multiple accounts. Also no error is returned if the balance cannot be updated, but it is not a requirement. I also understand I made a mountain out of a molehill with this problem as it can be solved by much less code.
In what ways can I improve this code other than the limitations mentioned?
#include <istream>
#include <iostream>
#include <iomanip>
class Account {
public:
Account()
: mBalance(0.0)
{}
void updateBalance(double transaction) {
mBalance += transaction;
}
double getBalance() {
return mBalance;
}
private:
double mBalance;
};
class Transaction {
public:
Transaction()
: mDebit(0)
, mCredit(0.0)
{}
int getDebit() {
return mDebit;
}
double getCredit() {
return mCredit;
}
friend std::istream& operator>>(std::istream& input, Transaction& transaction) {
input >> transaction.mDebit;
input >> transaction.mCredit;
return input;
}
private:
int mDebit;
double mCredit;
};
class ATM {
public:
ATM()
: mAccount()
, mMinDenomination(5)
, kWithdrawal_fee(0.50)
{}
void processTransaction(Transaction& transaction) {
credit(transaction);
debit(transaction);
}
void displayBalance() {
std::cout << mAccount.getBalance() << '\n';
}
private:
Account mAccount;
int mMinDenomination;
const double kWithdrawal_fee;
bool debit(Transaction& transaction) {
if(isWithdrawable(transaction.getDebit())){
mAccount.updateBalance(-1*(transaction.getDebit() + kWithdrawal_fee));
return true;
}
return false;
}
void credit(Transaction& transaction) {
if(transaction.getCredit() > 0) {
mAccount.updateBalance(transaction.getCredit());
}
}
bool isWithdrawable(int transaction) {
if(transaction % mMinDenomination == 0) {
return mAccount.getBalance() >= transaction + kWithdrawal_fee;
}
return false;
}
};
int main() {
std::iostream::sync_with_stdio(false);
std::cout << std::setprecision(2) << std::fixed;
Transaction transaction;
ATM atm;
std::cin >> transaction;
atm.processTransaction(transaction);
atm.displayBalance();
return 0;
}