DEV Community

Lindelani
Lindelani

Posted on

Billing system

Hi guys

i am a newbie in the coding world. i have challenged myself to come up with a billing system using C++. i have written the code but i cannot get it to work perfectly, i would appreciate if someone can point in the right direction. thanks in advanced.

#include <iostream>
#include <string>
#include <limits>
#include <fstream>
#include <iomanip>
using namespace std;


//a struct function that holds Customers Record
struct CustomerRecord
{
    //declaring variables
std::string CustomerName = " ";
int AccountNumber = 0;
int AreaCode = 0;
int StreetNo = 0;
std::string Suburb = " ";
std::string TownName = " ";
std::string StreetName = " ";


};
// a struct function to get a statement period
struct StatementPeriod
{
    string StartDate = " ";
    string EndDate = " ";

};
// Global variables
CustomerRecord Record;       // Calling CustomerRecord struct in the main
StatementPeriod Period;      // Calling StatementPeriod struct in the main
float PrevAmount = 100;       // Global variable to calculate the amount owed
float CurrentAmount = 10000;  // Global variable to calculate the amount owed
float TotalAmount = PrevAmount + CurrentAmount;  // Global variable to calculate the amount owed

int choice;  // For the update function
int option;  // to choose an option from the menu

// Function to add customer records to the system and store in the file
void AddRecord(CustomerRecord &Record, ofstream &file) {
    // customer Information and Address input from the user
    cout << "Please provide your full name: \t";
    getline(cin, Record.CustomerName);
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cout << "Please enter your account number: \t";
    cin >> Record.AccountNumber;
    cout << "Enter your street Number: \t";
    cin >> Record.StreetNo;
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cout << "Enter your street Name: \t" << endl;
    getline(cin, Record.StreetName);
    cout << "Enter your Suburb Name : \t";
    getline(cin, Record.Suburb);
    cout << "Enter your Town Name: \t";
    getline(cin, Record.TownName);
    cout << "Enter your AreaCode: \t";
    cin >> Record.AreaCode;
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    // Open the file in append mode (removed local declaration)

    if (file.is_open()) {
        // Check if the file is empty, and if so, write the header row
        ifstream checkFile("customer_records.txt");
        if (checkFile.peek() == std::ifstream::traits_type::eof()) {
            // Write the header row with setw for alignment
            file << std::left << setw(15) << "AccountNumber"
                 << setw(20) << "CustomerName"
                 << setw(10) << "StreetNo"
                 << setw(20) << "StreetName"
                 << setw(15) << "Suburb"
                 << setw(15) << "TownName"
                 << setw(10) << "AreaCode"
                 << setw(15) << "StartDate"
                 << setw(15) << "EndDate"
                 << setw(15) << "PrevAmount"
                 << setw(15) << "CurrentAmount"
                 << setw(15) << "TotalAmount\n";
        }
        checkFile.close();

        // Write customer information to the file with setw for alignment
        file << std::left << setw(15) << Record.AccountNumber
             << setw(20) << Record.CustomerName
             << setw(10) << Record.StreetNo
             << setw(20) << Record.StreetName
             << setw(15) << Record.Suburb
             << setw(15) << Record.TownName
             << setw(10) << Record.AreaCode
             << setw(15) << Period.StartDate
             << setw(15) << Period.EndDate
             << setw(15) << PrevAmount
             << setw(15) << CurrentAmount
             << setw(15) << TotalAmount << '\n';

        // Check if writing to the file was successful
        if (!file) {
            cout << "Error writing to the file.\n";
        }
    } else {
        cout << "Error opening the file.\n";
    }
}


//function to update a customer's Record

void UpdateRecord(CustomerRecord &Record, ofstream &file) //passed as a reference to allow for any modification on the original struct
{

 do
 {
 cout << "Select the information you want to update:\n";
 cout << "1. Customer Name\n";
 cout << "2. Account Number\n";
 cout << "3. Area Code\n";
 cout << "4. Street Number\n";
 cout << "5. Street Name\n";
 cout << "6. Suburb\n";
 cout << "7. Town Name\n";
 cout << "8. Exit Update\n";

 cin >> choice;

 //switch option for choosing what you want to update
    switch (choice) {
        case 1:
            cout << "Enter new Customer Name: ";
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            getline(cin, Record.CustomerName);
            break;
        case 2:
            cout << "Enter new Account Number: ";
            cin >> Record.AccountNumber;
            break;
        case 3:
            cout << "Enter new Area Code: ";
            cin >> Record.AreaCode;
            break;
        case 4:
            cout << "Enter new Street Number: ";
            cin >> Record.StreetNo;
            break;
        case 5:
            cout << "Enter new Street Name: ";
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            getline(cin, Record.StreetName);
            break;
        case 6:
            cout << "Enter new Suburb: ";
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            getline(cin, Record.Suburb);
            break;
        case 7:
            cout << "Enter new Town Name: ";
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            getline(cin, Record.TownName);
            break;
        case 8:
            cout << "Exiting Update\n";
            break;
        default:
            cout << "Invalid choice\n";
    }

   } while(choice != 8);

}

//function to delete a customer's Record
//also deletes from the file
void DeleteRecord(CustomerRecord &Record, ofstream &file) {
    // Clear the customer record
    Record.CustomerName = "";
    Record.AccountNumber = 0;
    Record.AreaCode = 0;
    Record.StreetNo = 0;
    Record.Suburb = "";
    Record.TownName = "";
    Record.StreetName = "";

    cout << "Customer record deleted.\n";

    // Close the file
    file.close();
}

// Function to request a statement period
void StatementDate() {
    cout << "Please provide the period from which you would like to see the statement \n";
    cout << "Please provide a start Date (format: day\\month\\year): \n";
    getline(cin, Period.StartDate);
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cout << "Please provide an ending Date (format: day\\month\\year): \n";
    getline(cin, Period.EndDate);

}

// Function to print a customer's record
void PrintRecord(CustomerRecord& Record) {
    if (Record.CustomerName.empty()) {
        cout << "Customer name is empty.\n";
    } else {
        cout << "**************Utility Bill ****************** \n";
        cout << "Account Number: " << Record.AccountNumber << '\n';
        cout << "Customer Name: " << Record.CustomerName << '\n';
        cout << "Address: " << Record.StreetNo << Record.StreetName << Record.Suburb
             << Record.TownName << Record.AreaCode << '\n';
        cout << "Statement Period from " << Period.StartDate << " to " << Period.EndDate << '\n';
        cout << "Bill Summary \n";
        cout << "Previous charges(R): \n" << PrevAmount << '\n';
        cout << "Current Charges(R): \n" << CurrentAmount << '\n';
        cout << "Total Amount Owed (R): \n" << TotalAmount << '\n';
    }
}

int main()
{

 cout<<" \n";
 cout<<"********* Utility Billing System   ******\n"<<endl;

 // Opening the file
    ofstream file("customer_records.txt", ios::app);

    // Check if the file is successfully opened
    if (!file.is_open())
    {
        cout << "Error opening the file.\n";
        return 1; // Return an error code
    }

 //Creating a Menu option
 do
 {
     //displaying the menu
     cout<<" \n";
     std::cout<<"Good day...Please Advice what assistant you would like by choosing a valid option below? \n";
     cout<<" \n";

     std::cout<<"Please enter 1 to add a new customer record \n";
     std::cout<<"Please enter 2 to update your customer record \n";
     std::cout<<"Please enter 3 to delete your customer record\n";
     std::cout<<"Please enter 4 to print  your customer record \n";
     std::cout<<"Please enter 5  for queries \n";
     std::cout<<"Please enter 6  to exit the Billing system \n";


     std::cin>> option;

     cout<<"\n";  //to create space

     //switch statement  with option for different menu options
    switch(option)
    {
        case 1:
            AddRecord(Record, file);
            break;
        case 2:
            UpdateRecord(Record, file);
            break;
        case 3:
            DeleteRecord(Record, file);
            break;
        case 4:
            PrintRecord(Record);
            break;
        case 5:
            cout<<"Email address: lindelanitshikalange0@gmail.com \n";
            cout<<"Contact Details: 079 9640198 \n";
            cout<<" \n";
            break;
        case 6:
            cout<<"Exiting the Program, Have a great day bye!";
            break;
        default:
            cout<<"Please enter a valid option /n";
    }
 } while(option !=6);

 // Close the file after the loop
    file.close();



return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)