-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
140 lines (123 loc) · 4.66 KB
/
main.cpp
File metadata and controls
140 lines (123 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* CS-210, Project 2
* R. DuPont, Last Update: 10/04/2025
* --------------------------------------------------
* Overview:
* --------------------------------------------------
* Banking Program for users to check their account growth based on
* inputs for initial value, monthly deposits, interest rate & years.
* Information is presented to the users via a GUI and they have the option
* to run multiple reports.
*/
#include "Banking.h"
#include <iostream>
#include <iomanip>
#include <stdexcept>
using namespace std;
//--------------------------------------------------
// Banking Methods:
//--------------------------------------------------
void printHomeScreen();
int getValidInt();
double getValidDouble();
//--------------------------------------------------
// Global banking object used across functions
//--------------------------------------------------
Banking myInvestment;
//--------------------------------------------------
// main()
// -------------------------------------------------
// Application entry point.
// Prompts user for investment data, displays both
// balance reports, and allows reruns until quit.
//--------------------------------------------------
int main() {
char userInput = ' '; //Holder value for tracking user feedback.
while (userInput != 'q') {
printHomeScreen(); //Calls to print the UI.
// Display reports using current account data
myInvestment.balanceWithoutMonthly(
myInvestment.getInitialInvestment(),
myInvestment.getInterestRate(),
myInvestment.getYears()
);
myInvestment.balanceWithMonthly(
myInvestment.getInitialInvestment(),
myInvestment.getMonthlyDeposit(),
myInvestment.getInterestRate(),
myInvestment.getYears()
);
// Prompt to continue or exit
cout << "\nEnter 'q' to quit, or any other key to run again: ";
cin >> userInput;
}
cout << "Thank you for using our Banking Application. Goodbye!\n";
return 0;
}
//--------------------------------------------------
// printHomeScreen()
// -------------------------------------------------
// Displays the user input form, validates entries,
// and updates the global Banking object.
//--------------------------------------------------
void printHomeScreen() {
try {
cout << "********************************************" << endl;
cout << "**** Welcome to our Banking Application ****" << endl;
cout << "********************************************" << endl;
cout << "Enter your initial investment amount: $";
double initialInvestment = getValidDouble();
if (initialInvestment < 0) throw runtime_error("Invalid entry.");
myInvestment.setInitialInvestment(initialInvestment);
cout << "Provide your monthly deposit: $";
double monthlyDeposit = getValidDouble();
if (monthlyDeposit < 0) throw runtime_error("Invalid entry.");
myInvestment.setMonthlyDeposit(monthlyDeposit);
cout << "Enter the annual interest (%): ";
double annualInterest = getValidDouble();
if (annualInterest < 0) throw runtime_error("Invalid entry.");
myInvestment.setInterestRate(annualInterest);
cout << "Provide the number of years: ";
int years = getValidInt();
if (years <= 0) throw runtime_error("Invalid entry.");
myInvestment.setYears(years);
}
catch (runtime_error& error) {
// Input validation error handling
cout << "\nError: " << error.what() << endl;
cout << "Cannot compute with negative or invalid values.\n" << endl;
printHomeScreen();
}
}
//--------------------------------------------------
// getValidInt()
// -------------------------------------------------
// Ensures that the user enters a valid integer.
// Keeps prompting until valid input is received.
//--------------------------------------------------
int getValidInt() {
int value;
while (true) {
if (cin >> value) break;
cout << "Invalid input! Enter a whole number: ";
cin.clear();
while (cin.get() != '\n'); // discard bad input
}
return value;
}
//--------------------------------------------------
// getValidDouble()
// -------------------------------------------------
// Ensures that the user enters a valid floating-point
// number. Keeps prompting until valid input is entered.
//--------------------------------------------------
double getValidDouble() {
double value;
while (true) {
if (cin >> value) break;
cout << "Invalid input! Enter a valid number: ";
cin.clear();
while (cin.get() != '\n');
}
return value;
}