SlideShare a Scribd company logo
1 of 5
Download to read offline
In C++
For this following code please separate it into 5 different source codes. The code is already given
to you.
main.cpp main
Grocery.hpp class definition
Grocery.cpp class definition for any member functions
Phistory.hpp class definition
Phistory.cpp class definition for any member functions
#include
#include
#include
using namespace std;
// Define the PriceHistory class
class PriceHistory {
private:
string date; // Date in YYYY-MM-DD format
float price;
public:
// Default constructor
PriceHistory() {
date = "";
price = 0.0;
}
// Parameterized constructor
PriceHistory(string d, float p) {
date = d;
price = p;
}
// Getter for date
string getDate() const {
return date;
}
// Getter for price
float getPrice() const {
return price;
}
};
// Define the Grocery class
class Grocery {
private:
string upc;
string description;
float cost;
float sellingPrice;
int inventory;
char status;
PriceHistory priceHistory[25];
int numHistories;
public:
static int numGroceries;
// Default constructor
Grocery() {
upc = "";
description = "";
cost = 0.0;
sellingPrice = 0.0;
inventory = 0;
status = ' ';
numHistories = 0;
}
// Parameterized constructor
Grocery(string u, string d, float c, float s, int i, char st) {
upc = u;
description = d;
cost = c;
sellingPrice = s;
inventory = i;
status = st;
numHistories = 0;
}
// Getter for upc
string getUPC() const {
return upc;
}
// Getter for description
string getDescription() const {
return description;
}
// Getter for cost
float getCost() const {
return cost;
}
// Getter for selling price
float getSellingPrice() const {
return sellingPrice;
}
// Getter for inventory
int getInventory() const {
return inventory;
}
// Getter for status
char getStatus() const {
return status;
}
// Getter for numHistories
int getNumHistories() const {
return numHistories;
}
// Add a new price history
void addPriceHistory(string date, float price) {
if (numHistories < 25) {
priceHistory[numHistories++] = PriceHistory(date, price);
}
}
// Display the grocery item
void display() const {
cout << upc << "t" << description << "t" << cost << "t" << sellingPrice << "t" <<
inventory << "t" << status << endl;
}
};
int Grocery::numGroceries = 0;
int main() {
const int MAX_GROCERIES = 100;
Grocery groceries[MAX_GROCERIES];
// Open the input file
ifstream infile("a4data.txt");
if (!infile.is_open()) {
cout << "Error opening input file." << endl;
return 1;
}
// Read the input file
string line;
while (getline(infile, line)) {
string upc = line.substr(0, 6);
string description = line.substr(6, 20);
float cost = stof(line.substr(26, 6));
float sellingPrice = stof(line.substr(32, 6));
int inventory = stoi(line.substr(38, 3));
char status = line[41];
// Add the grocery item to the array
if (Grocery::numGroceries < MAX_GROCERIES) {
groceries[Grocery::numGroceries++] = Grocery(upc, description, cost, sellingPrice,
inventory, status);
}
}
// Print the grocery items
cout <<
"=================================================================" <<
endl;
cout << "UPCtDescriptionttCosttPricetInventorytStatus" << endl;
cout <<
"=================================================================" <<
endl;
for (int i = 0; i < Grocery::numGroceries; i++) {
groceries[i].display();
}
return 0;
}

More Related Content

Similar to In C++For this following code please separate it into 5 different .pdf

C#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfC#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdf
ajantha11
 
#include iostream #includeData.h #includePerson.h#in.pdf
#include iostream #includeData.h #includePerson.h#in.pdf#include iostream #includeData.h #includePerson.h#in.pdf
#include iostream #includeData.h #includePerson.h#in.pdf
annucommunication1
 
#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx
#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx
#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx
gertrudebellgrove
 
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
fasttracksunglass
 
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdfstruct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
anonaeon
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
Darwin Durand
 
Please help me understand why when I run my code it does not make me.pdf
Please help me understand why when I run my code it does not make me.pdfPlease help me understand why when I run my code it does not make me.pdf
Please help me understand why when I run my code it does not make me.pdf
rmwaterlife
 
重構—改善既有程式的設計(chapter 8)part 2
重構—改善既有程式的設計(chapter 8)part 2重構—改善既有程式的設計(chapter 8)part 2
重構—改善既有程式的設計(chapter 8)part 2
Chris Huang
 

Similar to In C++For this following code please separate it into 5 different .pdf (17)

C#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfC#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdf
 
#include iostream #includeData.h #includePerson.h#in.pdf
#include iostream #includeData.h #includePerson.h#in.pdf#include iostream #includeData.h #includePerson.h#in.pdf
#include iostream #includeData.h #includePerson.h#in.pdf
 
Penjualan swalayan
Penjualan swalayanPenjualan swalayan
Penjualan swalayan
 
Virtual inheritance
Virtual inheritanceVirtual inheritance
Virtual inheritance
 
Functional C++
Functional C++Functional C++
Functional C++
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196
 
Relay Modern: architecture and workflow
Relay Modern: architecture and workflowRelay Modern: architecture and workflow
Relay Modern: architecture and workflow
 
#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx
#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx
#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx
 
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
 
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdfstruct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
 
Please help me understand why when I run my code it does not make me.pdf
Please help me understand why when I run my code it does not make me.pdfPlease help me understand why when I run my code it does not make me.pdf
Please help me understand why when I run my code it does not make me.pdf
 
重構—改善既有程式的設計(chapter 8)part 2
重構—改善既有程式的設計(chapter 8)part 2重構—改善既有程式的設計(chapter 8)part 2
重構—改善既有程式的設計(chapter 8)part 2
 
Writing Go(od) Tests (FOSDEM 2020)
Writing Go(od) Tests (FOSDEM 2020)Writing Go(od) Tests (FOSDEM 2020)
Writing Go(od) Tests (FOSDEM 2020)
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Core c sharp and .net quick reference
Core c sharp and .net quick referenceCore c sharp and .net quick reference
Core c sharp and .net quick reference
 
Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick reference
 

More from siva009113

More from siva009113 (6)

In Chapter 2, a discussion of how social media influences the politi.pdf
In Chapter 2, a discussion of how social media influences the politi.pdfIn Chapter 2, a discussion of how social media influences the politi.pdf
In Chapter 2, a discussion of how social media influences the politi.pdf
 
In March 2019, Zambia, a small open economy, lifted its export ban o.pdf
In March 2019, Zambia, a small open economy, lifted its export ban o.pdfIn March 2019, Zambia, a small open economy, lifted its export ban o.pdf
In March 2019, Zambia, a small open economy, lifted its export ban o.pdf
 
in JavsScript we have folders and files. Which folder would these tw.pdf
in JavsScript we have folders and files. Which folder would these tw.pdfin JavsScript we have folders and files. Which folder would these tw.pdf
in JavsScript we have folders and files. Which folder would these tw.pdf
 
IN JAVA PLEASEmake sure of the variables are correct and the s.pdf
IN JAVA PLEASEmake sure of the variables are correct and the s.pdfIN JAVA PLEASEmake sure of the variables are correct and the s.pdf
IN JAVA PLEASEmake sure of the variables are correct and the s.pdf
 
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdfIN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
 
In an agricultural experiment there are 25 varieties of maize, label.pdf
In an agricultural experiment there are 25 varieties of maize, label.pdfIn an agricultural experiment there are 25 varieties of maize, label.pdf
In an agricultural experiment there are 25 varieties of maize, label.pdf
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Recently uploaded (20)

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

In C++For this following code please separate it into 5 different .pdf

  • 1. In C++ For this following code please separate it into 5 different source codes. The code is already given to you. main.cpp main Grocery.hpp class definition Grocery.cpp class definition for any member functions Phistory.hpp class definition Phistory.cpp class definition for any member functions #include #include #include using namespace std; // Define the PriceHistory class class PriceHistory { private: string date; // Date in YYYY-MM-DD format float price; public: // Default constructor PriceHistory() { date = ""; price = 0.0; } // Parameterized constructor PriceHistory(string d, float p) { date = d; price = p; } // Getter for date string getDate() const { return date; } // Getter for price float getPrice() const { return price;
  • 2. } }; // Define the Grocery class class Grocery { private: string upc; string description; float cost; float sellingPrice; int inventory; char status; PriceHistory priceHistory[25]; int numHistories; public: static int numGroceries; // Default constructor Grocery() { upc = ""; description = ""; cost = 0.0; sellingPrice = 0.0; inventory = 0; status = ' '; numHistories = 0; } // Parameterized constructor Grocery(string u, string d, float c, float s, int i, char st) { upc = u; description = d; cost = c; sellingPrice = s; inventory = i; status = st; numHistories = 0; } // Getter for upc
  • 3. string getUPC() const { return upc; } // Getter for description string getDescription() const { return description; } // Getter for cost float getCost() const { return cost; } // Getter for selling price float getSellingPrice() const { return sellingPrice; } // Getter for inventory int getInventory() const { return inventory; } // Getter for status char getStatus() const { return status; } // Getter for numHistories int getNumHistories() const { return numHistories; } // Add a new price history void addPriceHistory(string date, float price) { if (numHistories < 25) { priceHistory[numHistories++] = PriceHistory(date, price); } } // Display the grocery item void display() const { cout << upc << "t" << description << "t" << cost << "t" << sellingPrice << "t" <<
  • 4. inventory << "t" << status << endl; } }; int Grocery::numGroceries = 0; int main() { const int MAX_GROCERIES = 100; Grocery groceries[MAX_GROCERIES]; // Open the input file ifstream infile("a4data.txt"); if (!infile.is_open()) { cout << "Error opening input file." << endl; return 1; } // Read the input file string line; while (getline(infile, line)) { string upc = line.substr(0, 6); string description = line.substr(6, 20); float cost = stof(line.substr(26, 6)); float sellingPrice = stof(line.substr(32, 6)); int inventory = stoi(line.substr(38, 3)); char status = line[41]; // Add the grocery item to the array if (Grocery::numGroceries < MAX_GROCERIES) { groceries[Grocery::numGroceries++] = Grocery(upc, description, cost, sellingPrice, inventory, status); } } // Print the grocery items cout << "=================================================================" << endl; cout << "UPCtDescriptionttCosttPricetInventorytStatus" << endl; cout << "=================================================================" << endl;
  • 5. for (int i = 0; i < Grocery::numGroceries; i++) { groceries[i].display(); } return 0; }