Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with code in c + + . Please help me to understand the code. Continuing from our Level 3 Quest we are

Please help me with code in c++. Please help me to understand the code. Continuing from our Level 3 Quest we are going to add two pieces of functionality:
The bank occasionally has a prize drawing for a random account holder. You need to write a function that randomly selects an account and displays the winning message.
The ability to save and load the accounts.
Source.cpp
You will add the following functions to your/my level 3 Quest solution:
findAccount: Does the same as Level 3 Quest.
chooseAccount: asks the user for an account ID, calls findAccount with that ID, and continues to ask until a valid ID is entered. The Account is returned (NOT THE ID!)
prizeDrawing: create a seed, engine, and uniform distribution (between 0 and accounts.size()-1), pick a random winner, and display that account holder's first and last name.
loadAccounts: creates a vector of the Account class, and loads it from the passed in file. The file structure is as follows
1<-- lastID
1<-- number of Accounts
1 Joe Smith 1.23<--account information (ID, first, last, beginning balance)
2<-- number of transactions for this account
10518 Walmart 34.25<-- date (month day year), description, and amount for the transaction.
121518 Amazon 54.26<--another transaction.
NOTE::The file can continue to repeat for additional accounts and additional transactions.
saveAccounts: takes a filename and vector of the Account class and saves it to the save file that was loaded from. Be sure to format the save in such a way that loadAccounts can load it!
Account Class
This class takes the place of the account struct and has the following public members/methods:
static int lastID; keeps track of the last ID.
Account(); Should increment lastID, ask the user for the account holder's full name and beginning balance. Use this for new accounts.
Account(int ID, std::string firstName, std::string lastName, float beginningBalance); Simply assigns the values passed in. Use this for loading from a file.
void addTransaction(); copy code from addTransaction function from Level 3 Quest. Adjust for being in a class. used for new transactions. asks for all information.--------------------------------------------- #include
#include
#include
#include
#include
#include "Transaction.h"
#include "Account.h"
int findAccount(int ID, const std::vector & accounts);
Account& chooseAccount(std::vector& accounts);
void prizeDrawing(const std::vector& accounts);
std::vector loadAccounts(const std::string & accountFile);
void saveAccounts(const std::string & accountFile, const std::vector& accounts);
using namespace std;
int main()
{
enum class Choices { addAccount =1, addTransaction, getBalance, print, drawPrize, exit };
string accountFile{ "accounts.txt"};
vector accounts{ loadAccounts(accountFile)};
int choice{0};
while ((Choices)choice != Choices::exit)
{
cout << "Would you like to:
"
<<"1. Add a new Account
"
<<"2. Add a new Transaction to an existing Account
"
<<"3. Get the current balance of an account
"
<<"4. Print Account & Transactions
"
<<"5. Draw Prize
"
<<"6. Exit
";
cin >> choice;
switch ((Choices)choice)
{
case Choices::addAccount:
accounts.push_back(Account());
break;
case Choices::addTransaction:
chooseAccount(accounts).addTransaction();
break;
case Choices::getBalance:
cout << "Your current account balance is: $"<< chooseAccount(accounts).getCurrentBalance()<< endl;
break;
case Choices::print:
chooseAccount(accounts).print();
break;
case Choices::drawPrize:
prizeDrawing(accounts);
break;
default:
break;
}
system("PAUSE");
system("CLS");
}
saveAccounts(accountFile, accounts);
}
int findAccount(int ID, const std::vector& accounts)
{
for (int i{0}; i < accounts.size(); i++)
{
if (accounts[i].getID()== ID)
return i;
}
return -1;
}
Account& chooseAccount(std::vector& accounts)
{
while (true)
{
system("CLS");
std::cout << "Which account do you wish to access (1-"<< Account::lastID <<"): ";
int accountID;
std::cin >> accountID;
if (int accountIndex{ findAccount(accountID, accounts)}; accountIndex !=-1)
return accounts[accountIndex];
else
std::cout << "not a valid ID!
";
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How does the concept of hegemony relate to culture?

Answered: 1 week ago