Question

In: Computer Science

Programming Project #1: Manage Simple Expenses Objectives: • Use basic C++ constructs: if, switch, repetition (looping)...

Programming Project #1: Manage Simple Expenses
Objectives:
• Use basic C++ constructs: if, switch, repetition (looping) and functions
• Perform simple data type and string object manipulations
• Solve problem, design solution and implement using C++
Description:
Write a menu-driven program named “ManageSimpleExpenses” that provides the following
options:
1. Earn
2. Spend
3. Show logs
4. Exit
Basically, the program allows the user to enter earning or spending amounts and keep track of
the current balance. It also print out the history of all entries.
Requirements:
1. The program must produce the same output as provided.
2. The user must enter the correct “pin number” which is one of the followings (either 5678
or 8765) in order to start the program.
3. The balance starts at 0
4. There is no need to use array or vector, just simply use string object and string
concatenation to keep track of the entry history.
5. There should be no global variable even the balance and the entry log string must be
local variables and being passed in the parameters.
Required error handling:
The program MUST perform the following checks:
1. Case-insensitive when to ask the user to confirm exiting the program (e.g. accepting both
‘Y’ and ‘y’)
2. Check for bad pin code and allow up to 3 trials.
3. Check for negative amount
4. Check for invalid menu answer
5. Check for spending amount that is greater than the current balance

Solutions

Expert Solution


code :


    #include <iostream>
    #include <string>
    using namespace std;

    // function to earn money
    int earn(int deposit, int balance);

    // function to spend money
    int spend(int withdrawl, int balance);

    // function to track balance
    void showLogs(string logs);

    main(){
        
        // storing account balance
        int balance = 0;
        // storing logs
        string logs = "";


        // flag is for running the program continuosly untill user manually exits.
        // program runs for flag=1 and exists if flag=0
        // logged is used to track user correct pin to avoid re-login 
        // when program starts again
        int flag = 1, logged = 0;
        while (flag == 1){

            // doesn't log if user is already logged
            if (logged == 0){
                
                int pin;
                cout << "Welcome!\nEnter pin:  ";
                cin >> pin;
                // pin check
                if(pin != 5678 && pin != 8765){
                    cout << "Wrong PIN\n";
                    continue;
                }
                logged = 1;
                // prompt menu
                cout << "\nMenu\n1. Earn Money\n2. Spend Money\n3. Show Logs\n4. Exit\n";
            }

            cout << "\nEnter your Option: ";
            int option;
            cin >> option;

            switch (option){
                case 1:
                    cout << "Enter amount to deposit: ";
                    // deposit stores amount user needs to earn
                    // bal stores the balance returned after executing the earn function
                    int deposit, bal;
                    cin >> deposit;
                    bal = earn(deposit, balance);
                    // error if bal is -1, so break without updating balance and logs
                    if(bal == -1)
                        break;
                    // if no error, update original balance and logs
                    balance = bal;
                    logs += "[+] Earned money  ";
                    logs +=  to_string(deposit);
                    logs += "\n";
                    cout << "Succesfully earned money!\n";
                    break;
                case 2:
                    cout << "Enter amount to spend: ";
                    // withdrawl stores amount user needs to spend
                    // bal2 stores the balance returned after executing 
                    // the withdram function
                    int withdrawl, bal2;
                    cin >> withdrawl;
                    bal2 = spend(withdrawl, balance);
                    // error if bal is -1, so break without updating balance and logs
                    if(bal2 == -1)
                        break;
                    // if no error, update original balance and Logs
                    balance = bal2;
                    logs += "[-] Spent money  "; 
                    logs += to_string(withdrawl); 
                    logs += "\n";
                    cout << "Succesfully spent money!\n";
                    break;
                case 3:
                    // showing logs stored in a string
                    cout << logs;
                    // showing the current balance
                    cout << "[=] Current Balance " << balance << "\n";
                    break;
                case 4:
                    // exiting the program
                    // flag made 0 to exit out of the loop to terminate the program
                    flag = 0;
                    // also make logged 0 to log out the user 
                    logged = 0;
                    cout << "Exiting Program!\n";
                    // no other code in this loop below is executed on 
                    // exit and control reaches to start of loop.
                    // this is done using continue
                    continue;
                default:
                    // if no option exits, prompt `invalid option`
                    cout << "Invalid Option! Choose Other\n";
                    break;
            }


        }

    }

    int earn(int deposit, int balance){
        // if deposit amount is negetive raise error by returning -1.
        // return value -1 is handled as error in above program
        if(deposit < 0){
            cout << "Cannot deposit negetive amount\n";
            return -1;
        }
        // update balance and return balance
        balance = balance + deposit;
        return balance;
    }

    int spend(int withdrawl, int balance){
        // if withdrawl amount is negetive raise error by returning -1.
        // return value -1 is handled as error in above program
        if(withdrawl < 0){
            cout << "Cannot widthdraw negetive amount\n";
            return -1;
        }
        // if withdrawl is greater then current balance raise error by returning -1.
        if(withdrawl > balance){
            cout << "No sufficient amount to withdraw\n";
            return -1;
        }
        // update balance and return balance
        balance = balance - withdrawl;
        return balance;
    }



Output:

Have a nice day


Related Solutions

Java. Part 1 of 4 - Amusement Park Programming Project Requirements: Use the Java selection constructs...
Java. Part 1 of 4 - Amusement Park Programming Project Requirements: Use the Java selection constructs (if and if else). Use the Java iteration constructs (while, do, for). Use Boolean variables and expressions to control iterations. Proper error handling. Class: Ticket – models admission tickets. Instance Fields: number : long – identify the unique ticket. category : String – store the category of the ticket. holder : String – store the name of the person who purchased the ticket. date...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on this assignment. 1. A structure named student which contains:   a. int ID; student id; b. last name; // as either array of char or a string c. double GPA;    2. An input file containing the information of at least 10 students. 3. An array of struct: read the student information from the input file into the array. 4. A stack: you can use the...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class with public and private members and multiple constructors.  Gain a better understanding of the building and using of classes and objects.  Practice problem solving using OOP. Overview You will implement a date and day of week calculator for the SELECTED calendar year. The calculator repeatedly reads in three numbers from the standard input that are interpreted as month, day of month, days...
Objectives: 1. To get familiar with C# programming language 2. To get familiar with Visual Studio...
Objectives: 1. To get familiar with C# programming language 2. To get familiar with Visual Studio development environment 3. To practice on writing a C# program Task 1: Create documentation for the following program which includes the following: a. Software Requirement Specification (SRS) b. Use Case Task 2: Write a syntactically and semantically correct C# program that models telephones. Your program has to be a C# Console Application. You will not implement classes in this program other than the class...
Programming Projects Project 1—UNIX Shell This project consists of designing a C program to serve as...
Programming Projects Project 1—UNIX Shell This project consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. Your implementation will support input and output redirection, as well as pipes as a form of IPC between a pair of commands. Completing this project will involve using the UNIX fork(), exec(), wait(), dup2(), and pipe() system calls and can be completed on any Linux, UNIX, or macOS...
windows forms Application using Visual Basic use visual basic 2012 Part 1: Programming – Income Tax...
windows forms Application using Visual Basic use visual basic 2012 Part 1: Programming – Income Tax Application 1.1 Problem Statement Due to upcoming end of financial year, you are being called in to write a program which will read in a file and produce reports as outlined. Inputs: The input file called IncomeRecord.txt contains a list of Annual income records of employees in a firm. Each record is on a single line and the fields are separated by spaces. The...
C program simple version of blackjack following this design. 1. The basic rules of game A...
C program simple version of blackjack following this design. 1. The basic rules of game A deck of poker cards are used. For simplicity, we have unlimited number of cards, so we can generate a random card without considering which cards have already dealt. The game here is to play as a player against the computer (the dealer). The aim of the game is to accumulate a higher total of points than the dealer’s, but without going over 21. The...
Please USE C++ The goal for this Project is to create a simple two-dimensional predator-prey simulation....
Please USE C++ The goal for this Project is to create a simple two-dimensional predator-prey simulation. In this simulation the prey are ants and the pred-ators are doodlebugs. These critters live in a world composed of a 20 × 20 grid of cells. Only one critter may occupy a cell at a time. The grid is enclosed, so a critter is not allowed to move off the edges of the world. Time is simulated in time steps. Each critter performs...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following:...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following: (1) the client and the server source files each (2) a brief Readme le that shows the usage of the program. 3. Please appropriately comment your program and name all the identifiers suitable, to enable enhanced readability of the code. Problem: Write an ftp client and an ftp server such that the client sends a request to ftp server for downloading a file. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT