Skip to main content
Tweeted twitter.com/StackCodeReview/status/1327400858854830082
Became Hot Network Question
edited tags
Link
pacmaninbw
  • 26.1k
  • 13
  • 47
  • 114
Source Link

Tax Calculator (using OOP techniques)

I was wondering if you'd be able to review my basic Tax Calculator that incorporates OOP techniques. I'd greatly appreciate it if you didn't mention about my variable naming convention i.e. Hungarian notation, it is a requirement for my university course. Also, please ignore the fact it says about "Windows Forms Application", this is purely a console app in C++. Furthermore, please just treat my program as it is supposed to be inputted I haven't done error-checking. The main point of this is just to get feedback on how I've used OOP techniques. Thanks very much in advance.

enter image description here

// Tax Calculator.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;

class Person {
private:
    string m_sName;
    string m_sBandIncomeTaxName;
    
    bool m_bPayIncomeTax;
    bool m_bPayStudentLoan;

    double m_dAnnualSalary;
    double m_dTaxRate;
    double m_dMonthlyIncomeTax;
    double m_dWeeklySalary;
    double m_dNationalInsurancePay;
    double m_dNationalInsuranceRate;
    double m_dStudentLoanPay;
    double m_dTakeHomePay;
  
public:

    //Constructor
    Person() :
        m_sName("No name specified"), 
        m_sBandIncomeTaxName("No name specified"), 
        m_bPayStudentLoan(0.0),
        m_bPayIncomeTax(0.0), m_dAnnualSalary(0.0), 
        m_dTaxRate(0.0), 
        m_dMonthlyIncomeTax(0.0),
        m_dWeeklySalary(0.0), 
        m_dNationalInsurancePay(0.0), 
        m_dNationalInsuranceRate(0.0),
        m_dStudentLoanPay(0.0), 
        m_dTakeHomePay(0.0){}

    string GetName() const { return m_sName; }
    double GetNationalInsuranceRate() { return m_dNationalInsuranceRate; }
    double GetNationalInsurancePay() { return m_dNationalInsurancePay; }
    double GetWeeklySalary() { return m_dWeeklySalary; }
    double GetAnnualSalary() const { return m_dAnnualSalary; }
    double GetMonthlyIncomeTax() { return m_dMonthlyIncomeTax; }
    double GetStudentLoanPay() { return m_dStudentLoanPay; }
    double GetMonthlyTakeHomePay() { return m_dTakeHomePay; }
    bool GetStudentLoanBool() { return m_bPayStudentLoan; }
    bool GetIncomeTaxBool() { return m_bPayIncomeTax; }

    void SetNationalInsuranceRate(double dNationalInsuranceRate) { m_dNationalInsuranceRate = dNationalInsuranceRate; };
    void SetName(string sName) { m_sName = sName; }
    void SetAnnualSalary(double dAnnualSalary) { m_dAnnualSalary = dAnnualSalary; }
    void SetPayIncomeTax(bool bPayIncomeTax) { m_bPayIncomeTax = bPayIncomeTax; }
    void SetPayStudentLoan(bool bPayStudentLoan) { m_bPayIncomeTax = bPayStudentLoan; }
    void SetBandIncomeTaxName(string sBandIncomeTaxName) { m_sBandIncomeTaxName = sBandIncomeTaxName; }
    void SetTaxRate(double dTaxRate) { m_dTaxRate = dTaxRate; }

    void CalculateMonthlyIncomeTax();
    void CalculateWeeklyIncome();
    void CalculateNationalInsurance();
    void CalculateAnnualStudentLoan();
    void CalculateMonthlyTakeHomePay();
};

void Person::CalculateMonthlyIncomeTax() { 
    m_dMonthlyIncomeTax = (m_dAnnualSalary * m_dTaxRate) / 12; 
}
void Person::CalculateWeeklyIncome() {
    m_dWeeklySalary = (m_dAnnualSalary / 12) / 4; 
}
void Person::CalculateNationalInsurance() {
    double dRemainderOver = 0;
    if (GetWeeklySalary() > 162 && GetWeeklySalary() < 892) {

        dRemainderOver = GetWeeklySalary() - 162;
        SetNationalInsuranceRate(0.12);
        m_dNationalInsurancePay = dRemainderOver * GetNationalInsuranceRate();
    }
    else
    {
        dRemainderOver = GetWeeklySalary() - 892;
        SetNationalInsuranceRate(0.2);
        m_dNationalInsurancePay = dRemainderOver * GetNationalInsuranceRate();
    }
}
void Person::CalculateAnnualStudentLoan() {
    if (m_dAnnualSalary > 25000) {
        m_dStudentLoanPay = m_dAnnualSalary * 0.9;
    }
    else {
        m_dStudentLoanPay = 0;
    }
}
void Person::CalculateMonthlyTakeHomePay() { 
    m_dTakeHomePay = (m_dAnnualSalary / 12) - (m_dNationalInsurancePay * 4) - (m_dStudentLoanPay / 12) - m_dMonthlyIncomeTax; 
}

void AddPerson(Person& objPerson)
{
    std::string sName = "";
    double dAnnualSalary = 0;
    std::cout << "Enter the name of the person: " ;
    cin >> sName;
    objPerson.SetName(sName);

    std::cout << "How much do you get annually?: " << char(156);
    cin >> dAnnualSalary;
    objPerson.SetAnnualSalary(dAnnualSalary);
}

void DoYouPayAnnualIncomeTax(Person& objPerson)
{
    unsigned char cChoice;
    bool bPayAnnualIncomeTax = false;
    bool bPayStudentLoan = false;

    std::cout << "Do you pay annual income tax?: (y/n) ";
    cin >> cChoice;
    cChoice = tolower(cChoice);
    if (cChoice == 'y')
    {
        objPerson.SetPayIncomeTax(true);
    }
    else if ('n') {
        objPerson.SetPayIncomeTax(false);
    }
    else
    {
        std::cout << "You've entered an invalid choice!\n";
    }
}
void DoYouPayStudentLoan(Person& objPerson)
{
    unsigned char cChoice;
    bool bPayAnnualIncomeTax = false;
    bool bPayStudentLoan = false;

    std::cout << "Do you pay a student loan (y/n): ";
    cin >> cChoice;
    cChoice = tolower(cChoice);
    if (cChoice == 'y')
    {
        objPerson.SetPayStudentLoan(true);
    }
    else if ('n') {
        objPerson.SetPayStudentLoan(false);
    }
    else
    {
        std::cout << "You've entered an invalid choice!\n";
    }
}

void CalcBandIncomeTaxPayer(Person& objPerson) {

    if (objPerson.GetAnnualSalary() < 11850)
    {
        objPerson.SetBandIncomeTaxName("Personal Allowance");
        objPerson.SetTaxRate(0.0);

    }
    else if (objPerson.GetAnnualSalary() < 46350)
    {
        objPerson.SetBandIncomeTaxName("Basic Rate");
        objPerson.SetTaxRate(0.2);
    }
    else if (objPerson.GetAnnualSalary() < 150000)
    {
        objPerson.SetBandIncomeTaxName("Higher Rate");
        objPerson.SetTaxRate(0.4);
    }
    else
    {
        objPerson.SetBandIncomeTaxName("Additional Rate");
        objPerson.SetTaxRate(0.45);
    }

}

void DisplayResults(Person &objPerson) {
    std::cout << "*********************" << std::endl;
    if (objPerson.GetIncomeTaxBool() == true) {
        objPerson.CalculateMonthlyIncomeTax();
        std::cout << "You pay in monthly income tax: " << char(156) << objPerson.GetMonthlyIncomeTax() << std::endl;
    }
    if (objPerson.GetStudentLoanBool()) 
    {
        objPerson.CalculateAnnualStudentLoan();
        std::cout << "Annual Student loan: " << char(156) << objPerson.GetStudentLoanPay() << std::endl;
    }
    //Make the calculations
    std::cout << "Monthly National Insurance: " << char(156) << objPerson.GetNationalInsurancePay() << std::endl;
    std::cout << "Monthly Student Loan: " << char(156) << objPerson.GetStudentLoanPay() << std::endl;
    std::cout << "Take home pay: " << char(156) << objPerson.GetMonthlyTakeHomePay();
    std::cout << "*********************" << std::endl;
}

int main()
{
    std::cout << std::setprecision(2) << std::fixed;
    Person objPerson;
    //Get input from user
    AddPerson(objPerson);
    DoYouPayAnnualIncomeTax(objPerson);
    DoYouPayStudentLoan(objPerson);
    CalcBandIncomeTaxPayer(objPerson);
    objPerson.CalculateWeeklyIncome();
    objPerson.CalculateNationalInsurance();
    objPerson.CalculateMonthlyTakeHomePay();
    DisplayResults(objPerson);
}