Menu

(Solved) : Gain Experience Developing Programs Using Stl Containers Inheritance Aim Gain Experience D Q29545340 . . .

Gain experience developing programs using STL containers andinheritance.

Aim:
Gain experience developing programs using STL containers andinheritance.
On completion you should know how to:
• Write C++ code using STL container class objects.
• Use inheritance to facilitate programming with objects.
• Gain experience writing and debugging O-O programsincrementally.
Prerequisites:
This assignment involves writing a program to handle the billing ofelectricity, gas and phone
accounts of a meter reading and billing company. Meter readinginformation on electricity, gas and
phone usage is contained in the file “usage.txt”. Each recordbegins with the service type (i.e. elect,
gas or phone) followed with customer details:

BillingRecord Suppliers name Customers name Customers address Account balance Days since last readin . . and other usage information which depend on the service type: Electricitv Previous reading Current reading Rate 1 Rate 1 threshold Rate 2 Supply charge Gas Previous reading Current reading Heating value Rate Supply charge Phone Number of local calls Local call rate Long distance call time Long distance call rate Line rental

This information is used to calculate the customer’s next bill.The processing of the billing records
is complicated because some suppliers offer their customersdiscounts if they subscribe to more
than one service from them. To solve this problem you are to useobject oriented programming
with inheritance to represent the different types of billingrecords. Polymorphism will also be used
to allow all records to be processed efficiently in the onearray.

Billing System Design
The system is comprised of a BillSystem class object that own avector of BillRecord objects in
implemented in appropriately named files. The BillRecord class is abase class that has 3 derived
classes for representing the 3 types of bills (elect. gas &phone). The BillSystem has public
functions for reading the data file into the BillRecord vector,processing the billing information
and displaying the records on the screen. Some of the functions ofthe BillRecord class are
overridden to handle the different types of data in the derivedclasses.
Step 1
For step 1, get the BillRecord base class working. Examine theincomplete code in the provided
files and complete the function definitions of class BillRecord andBillSystem so that the customer
details (only) are read into each BillRecord of the vector BRecsfrom “usage.txt”. Then test that
main() displays the first 10 customer details on the screen, asshown below. Note: don’t forget to
delete the pointers in vector BRecs in the BillSystemdestructor.
# Service Supplier Customer Address AccBal Days
1 Phone Origin George Carter 24 Dingo St Exeter SA 27.49 28
2 Gas EA Paul Scott 21 Beach Rd Barham NSW 92.98 30
“ “ “ “ “ “ “
10 Gas EA Carol Harris 67 Broke St Milton NSW 20.45 29
Step 2
Declare the derived BillRecord classes: ElectBillRecord,GasBillRecord and PhoneBillrecord in
BillSystem.h. These classes inherit the base class BillRecord. Makesure you provide private data
members for storing the relevant usage information shown on page 1.Change the BillRecord class’s
private data member: AccountBalance and DaysSinceLastReading toprotected to make them
available to the derived classes. Also, make functions:ReadUsageInfo() and DisplayUsageInfo()
into virtual functions so that they can be overridden by the baseclasses. See here:
https://www.geeksforgeeks.org/virtual-function-cpp/
Now provide the derived classes with their versions of functions:ReadUsageInfo() and
DisplayUsageInfo() to read and display their usage informationappropriately.
Modify ReadFile() in BillSystem.cpp so that it reads the servicetype and then allocates a
ElectBillRecord, GasBillRecord or PhoneBillrecord accordingly usingthe new operator. Then call
their ReadCustDetails() and ReadUsageInfo() functions to have thedata in the file read
appropriately. You might have to work on the code in eachReadUsageInfo() function to get each
derived class reading its file data correctly.
When you complete this step run the main() again to display 5records showing the Customer and
usage info on the screen something like this:

# Service Supplier Customer Address AccBal Days
1 Phone Origin George Carter 53 Dingo St Exeter SA 27.49 28
(LCalls: 283, 0.14 DCalls: 245, 0.32 Rental: 0.44)
2 Gas EA Paul Scott 84 Beach Rd Barham NSW 92.98 30
(Readings: 14148, 14224 HV:38.40 Rate: 0.039 SuppC: 0.56)
3 Elect Alinta Brian Crowe 61 Beach Rd Hanwood WA 18.06 31
(Readings: 113144, 113568 R1: 0.28 R1Th: 535 R2: 0.30 SuppC:0.86)
“ “ “ “ “ “

Step 3
To win more customers Dodo and Alinta provide 15% and 20% discount,respectively, to all
customers who subscribe to all three services from them. To dealwith this add a protected data
member to the BillRecord class named Discount and a public memberfunction named
SetDiscount(float d) for setting it. Also set Discount to one inthe constructor. (Note: for 15%
discount set Discount to 0.85. For 20% discount set Discount to0.8.)
There are various methods for finding which customers should havethe discount applied. You can
devise your own method if you wish but try to keep it efficient.The processing for this should be
done in a public member function in the BillSystem class namedCalDiscounts()..
One suggestion for finding the discounted records is to use amultiset with a user defined
comparison object for ordering the BillRecord pointers. Thecomparison object should compare the
BillRecords first by name and second by address. When all Dodo orAlinta records are inserted
into this multiset, customers with the same name and address willbe grouped and can be accessed
with multiset functions: count(), lower_bound() and upper_bound().(E.g. erase all BillRecord
pointers with a count less than 3.) Once the discount pointers arefound they can be dereferenced to
set the discount appropriately. Test your code by uncommenting the“step-4” code in main() and
print on the screen the number of discount customers Dodo andAlinta have.
Step 4
Add two more public member functions to the BillSystem class namedCalBills() and PrintReport().
Also add a public pure virtual function: UpdateBalance() in theBillRecord class and override this
in the derived classes. UpdateBalance() should calculate theBillAmount and add this to the
AccountBalance. The BillAmount is calculated as follows:
Elect : C = CrntReading – PrevReading;
P = SuppyCharge * DaysSinceLastReading;
If (C <= R1Thld) BillAmt = (C * Rate1 + P) * Discount –AccBalance;
Else BillAmt = (R1Thld * Rate1 + (C – R1Thld) * Rate2 + P) *Discount – AccBalance;
Gas : C = CrntReading – PrevReading;
P = SuppyCharge * DaysSinceLastReading;
BillAmt = (C * HeatingValue * Rate) * Discount – AccBalance;
Phone : L = LocalCallRate * NumLocalCalls
D = LDistCallRate * LDistCallTime
P = LineRental * DaysSinceLastReading;
BillAmt = (L + D + P) * Discount – AccBalance;
Note: the AccountBalance should be set to zero after the BillAmountis calculated.
PrintReport() should print the first 5 records similar to Step-1with the “AccBal” and “Days”
replaced with “BillAmt”. Also, print the names and addresses of allDodo and Alinta customers
who have discounts.

/**********************************************************************
* main.cpp
**********************************************************************/
#include <iostream>
#include “BillSystem.h”
using namespace std;

char Menu();

int main(){

   BillSystem BS;

   cout << “Begin tests for BillSystemnn”;

   if(!BS.ReadFile(“usage.txt”)){
       cout << “File notfound!nn”;
       exit(1);
   }
   int n = BS.GetNumRecs();

   cout << “Num records read: ” << n<< endl << endl;

   for(int i=0; i<n && i<10; i++){
       BS.DisplayRec(i);
       cout << endl;
   }
  
   //BS.CalDiscounts(); // uncoment when step 3complete
  
   //BS.CalBills();     // uncomentwhen step 4 complete
   //BS.PrintReport(); // uncoment when step 4complete

   cout << endl << “End tests forBillSystemn”;

   return 0;
}

/**********************************************************************
* BillRecord.cpp
**********************************************************************/
#include <iostream>
#include <fstream>
#include <iomanip>
#include “BillRecord.h”
using namespace std;

// ========== class BillRecord function definitions==========

// Default constructor
BillRecord::BillRecord()
{

}

// Reads customer details part of record from file
bool BillRecord::ReadCustDetails(ifstream &fin)
{
   //put code here for reading the customer details partof file record only into the private data members
   return true;
}

// Displays customer details part of record on screen
void BillRecord::DisplayCustDetails()
{
   //display customer details (only) from private datamembers
}

// Virtual fn for reading usage info part of record from file inderived classes
bool BillRecord::ReadUsageInfo(ifstream &fin)
{
   //the code here should jusy test BillType and read(eat) the usage info from file and discard it
   //later we will override this fn to read usage infointo the approbriate derived classes private data members
   return true;
}

// virtual fn for displays usage info part of record in derivedclasses
void BillRecord::DisplayUsageInfo()
{
   // does nothing – later we will override this fn todisplay the appropriate billing info in the derived classes
}

// ========== Derived Class function definitions ==========

// write the function definitions of the derived classes here

/**********************************************************************
* BillSystem.cpp
**********************************************************************/
#include <iostream>
#include <fstream>
#include “BillSystem.h”
using namespace std;

// ========== class BillSystem Public Function definitions==========

// Destructor
BillSystem::~BillSystem()
{
   //Iterate BRecs vector and delete each ptr
}

// Reads data in “usage.txt” into BRecs vector
bool BillSystem::ReadFile(char *fname)
{

   // open fname
   // if not found display: “File not found.” and return0
   // while not eof
       // allocate new memory to a tempBillRecord
       //if(!temp.ReadCustDetails(fin))break;
       //if(!temp.ReadUsageInfo(fin))break;
       // add temp to back of BRecsvector
   // close file
   return true;
}

// Returns the number of records in BRecs
int BillSystem::GetNumRecs()
{
   return BRecs.size();
}

// Displays ith record on screen
void BillSystem::DisplayRec(int i)
{
   BRecs[i]->DisplayCustDetails();
   BRecs[i]->DisplayUsageInfo();
   cout<<endl;
}

// ========== class BillSystem Private Function definitions==========

/**********************************************************************
* BillSystem.h
**********************************************************************/
#ifndef BILLSYS_H
#define BILLSYS_H

#include <iostream>
#include <vector>
#include “BillRecord.h”
using namespace std;

class BillSystem {
public:
    ~BillSystem();
   bool ReadFile(char *fname);
   int GetNumRecs();
   void DisplayRec(int i); // prints ith record onscreen
  
private:
   vector<BillRecord*> BRecs; // vector of pointersto class BillRecord
};

#endif

BillingRecord Supplier’s name Customer’s name Customer’s address Account balance Days since last readin . . and other usage information which depend on the service type: Electricitv Previous reading Current reading Rate 1 Rate 1 threshold Rate 2 Supply charge Gas Previous reading Current reading Heating value Rate Supply charge Phone Number of local calls Local call rate Long distance call time Long distance call rate Line rental Show transcribed image text

Expert Answer


Answer to Gain Experience Developing Programs Using Stl Containers Inheritance Aim Gain Experience D Q29545340 . . .

OR