Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

main .cpp file /* Intermediate Computer Programming * Spring 2020 - Lab 4 * * Driver program */ #include #include #include #include // user made

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

 main .cpp file /* Intermediate Computer Programming
 * Spring 2020 - Lab 4
 *
 * Driver program
 */
 
#include 
#include 
#include 
#include 
 
// user made classes
#include "apple.h"
#include "potato.h"
 
// using statements
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::ifstream;
using std::exit;
 
// user defined functions
int processPotatoFile(vector &potatoes);
int processAppleFile(vector &apples);
void displayStock(vector &potatoes, vector &apples);
void displayPotatoStock(vector &potatoes);
void displayAppleStock(vector &apples);
 
 
int main()
{
 // initial vectors
 vector potatoes;
 vector apples;
 
 // function to process file
 processPotatoFile(potatoes);
 processAppleFile(apples);
 cout  
 
 // display stock list before...
 cout  
 displayStock(potatoes, apples);
 
 // testing functions...
 // potatoes
 potatoes[0].setStock(456);
 potatoes[1].setPrice(1.99);
 potatoes[2].setType("Purple");
 potatoes[2].setOrganic(1);
 
 // apples
 apples[0].setOrchardLocation("New York, NY");
 apples[0].setType("Granny Smith");
 apples[2].setPrice(0.45);
 
 // display stock list after...
 cout  
 displayStock(potatoes, apples);
 
 
 // testing other functions...
 cout  
 cout  
 cout  
 
 cout  
 cout  
 
 return 0;
}
 
// processing potato file
int processPotatoFile(vector &potatoes)
{
 ifstream infile;
 string line;
 
 // initial variables
 string type, itemID;
 int quantity, flag;
 float price;
 
 // static potatoList file
 infile.open("potatoList.txt");
 
 // checking for file success
 if(infile.is_open())
 {
 cout  
 }
 
 // exits program otherwise
 else
 {
 cout  
 exit(EXIT_FAILURE);
 }
 
 // loop to read file
 while(getline(infile, line))
 {
 if(line == "potato")
 {
 // initial object
 // constructor: no parameters
 Potato temp;
 
 // get potato data...
 getline(infile, type);
 getline(infile, itemID);
 infile >> quantity;
 infile >> price;
 infile >> flag;
 
 // ignore newline for next getline...
 infile.ignore();
 
 // set potato data
 temp.setType(type);
 temp.setItemID(itemID);
 temp.setStock(quantity);
 temp.setPrice(price);
 temp.setOrganic(flag);
 
 // add to appropriate vector
 potatoes.push_back(temp);
 }
 
 }
 
 return 0;
}
 
int processAppleFile(vector &apples)
{
 ifstream infile;
 string line;
 
 // initial variables
 string type, itemID, location;
 int quantity, flag;
 float price;
 
 // static appleList file
 infile.open("appleList.txt");
 
 // checking for file success
 if(infile.is_open())
 {
 cout  
 }
 
 // exits program otherwise
 else
 {
 cout  
 exit(EXIT_FAILURE);
 }
 
 while(getline(infile, line))
 {
 if(line == "apple")
 {
 // get apple data...
 getline(infile, type);
 getline(infile, itemID);
 infile >> quantity;
 infile >> price;
 infile >> flag;
 
 // ignore newline...
 infile.ignore();
 
 // final line...
 getline(infile, location);
 
 // create object
 // constructor: with parameters
 Apple temp(type, location, itemID, quantity, price, flag);
 
 // add to appropriate vector
 apples.push_back(temp);
 }
 
 }
}
 
// function to actually display the stock
void displayStock(vector &potatoes, vector &apples)
{
 cout  
 
 displayPotatoStock(potatoes);
 displayAppleStock(apples);
}
 
void displayPotatoStock(vector &potatoes)
{
 // display potatoes
 for(int i = 0; i  
 {
 cout  
 cout  
 cout  
 cout  
 cout  
 
 if(potatoes[i].isOrganic())
 {
 cout  
 }
 
 else
 {
 cout  
 }
 }
}
 
void displayAppleStock(vector &apples)
{
 // display apples
 for(int i = 0; i  
 {
 cout  
 cout  
 cout  
 cout  
 cout  
 cout  
 
 if(apples[i].isOrganic())
 {
 cout  
 }
 
 else
 {
 cout  
 }
 }
}
 
Objectives: Continue practicing past concepts Practice building basic C++ classes Practice building C++ classes that use inheritance Assignment: For this lab you will be given a driver file (main.cpp) and two text files (potatoList.txt and appleList.txt) that will be testing classes that you should create. You should not change any of these files. (Note: For your main.cpp -- feel free to comment out portions in development if you don't have a particular class working yet. For changes, we are referring to changing what a particular line actually does/how it does it.) Your job will be to create the classes that will enable this main file to work. There should be three classes: Inventory (base class) Apple (derived class) Potato (derived class) Once you create your classes and are finished, the final project should statically print out what is contained in the code execution portion. That is how you know that you have completed the program The classes you should create are outlined below in UML Class diagrams. For a UML class diagram cheat sheet, refer to the last page of the lab assignment details. UML Specifications: . Inventory: o getItem Total(int quantity) - returns the item's price multiplied by the parameter quantity o is Organic() - acts as a standard getter o setOrganic(int flag) - takes in an integer -- if the integer is 1 organic should be true, else if the integer is 0, it should be false o getters/setters - act as usual Apple o Apple() - standard empty constructor -- takes all items and sets them to their empty counterparts ("" for strings, O for numbers -- false for booleans); statically sets itemName to "Apples" o Apple(with parameters) - takes in user parameters to set apple and inventory data equal to; statically sets itemName to "Apples" For organic: see setOrganic standards above o getters/setters - act as usual Potato o Potato() - standard empty constructor -- takes all items and sets them to their empty counterparts (*" for strings, O for numbers -- false for booleans); statically sets itemName to "Potatoes" o Potato(with parameters) - takes in user parameters to set apple and inventory data equal to; statically sets itemName to "Potatoes" For organic: see set Organic standards above o getPotato Total(float weight) - returns the weight parameter multiplied by the price of the potato o getters/setters - act as usual Inventory Apple Potato -type: string - orchardLocation: string - type: string #itemID: string #itemName: string #stock: int # price: float #organic: bool + Apple + Apple(string type, string location, string itemID, int stock, float price, int flag) + Potato + Potato(string type, string itemID, int stock, float price, int + getitemIDO: string + netitemName string flag) + Sellype(Shiny type). VIU +setOrchardLocation(string location): void + getPolato Totaltloat Weight) float + isOrganico: bool + setitemID(string itemID): void + setStock(int stock): void + setPrice(float price): void + setOrganic(int flag): void + getItem Total(int quantity): float Code Execution: Successful file opening. ***STOCK BEFORE CHANGES*** We have the following items in stock: Item: 0001 568 Idaho Potatoes at 0.33 per lb Organic: no Item: 0002 67 Sweet Potatoes at 0.97 per lb Organic: yes Item: 0003 32 Golden Potatoes at 0.57 per lb Organic: no Item: 0004 345 Fuji Apples at $0.66 per item Sourced from: Salt Lake City, UT Organic: yes Item: 0005 53 Pink Lady Apples at $0.92 per item Sourced from: Boise, ID Organic: yes Item: 0006 345 Red Delicious Apples at $0.4 per item Sourced from: Detroit, MI Organic: no JUU TUANIU TULLUUS ALV.JJ Pel IU Organic: no Item: 0002 67 Sweet Potatoes at 0.97 per lb Organic: yes Item: 0003 32 Golden Potatoes at 0.57 per lb Organic: no Item: 0004 345 Fuji Apples at $0.66 per item Sourced from: Salt Lake City, UT Organic: yes Item: 0005 53 Pink Lady Apples at $0.92 per item Sourced from: Boise, ID Organic: yes Item: 0006 345 Red Delicious Apples at $0.4 per item Sourced from: Detroit, MI Organic: no ***STOCK AFTER CHANGES*** We have the following items in stock: Item: 0001 456 Idaho Potatoes at 0.33 per lb Organic: no Item: 0002 67 Sweet Potatoes at 1.99 per lb Organic: yes Item: 0003 32 Purple Potatoes at 0.57 per lb Organic: yes Item: 0004 345 Granny Smith Apples at $0.66 per item Sourced from New York, NY Organic: yes Item: 0005 53 Pink Lady Apples at $0.92 per item Sourced from: Boise, ID Organic: yes Item: 0006 345 Red Delicious Apples at $0.45 per item Sourced from: Detroit, MI Organic: no ***MISCELLANEOUS*** Buying 44 Pink Lady Apples will cost $40.48 Buying 1341bs worth of Idaho Potatoes will cost $44.22 - D X appleList - Notepad File Edit Format View Help Happle Fuji 0004 345 0.66 1 Salt Lake City, UT apple Pink Lady 0005 53 0.92 Boise, ID apple Red Delicious 0006 345 0.40 Detroit, MI Ln 1, Col 1 100% Unix (LF) UTF-8 - D X potatoList - Notepad File Edit Format View Help potato Idaho 0001 568 0.33 potato Sweet 0002 67 0.97 potato Golden 0003 32 0.57 Ln 1, Col 1 100% Unix (LF) UTF-8

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions