Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this homework, you will create a code representation of an Auto Part. This wi

ID: 3744545 • Letter: I

Question

In this homework, you will create a code representation of an Auto Part. This will be used in an inventory system of car parts

. The Auto_Part Class

First you will create two C++ files called abc1234_Auto_Part.h and abc1234_Auto_Part.cpp. Below is a UML class diagram that shows the basic design of the Auto_Part class. The type field represents what type of part it is (tire, axle, seat, sparkplug, etc.).

The name field represents the name of the part (Firestone FR710, E3 Spark Plug, etc.).

The part number field corresponds to the inventory number of the part in the shop.

The price field represents how much that part sells for

. The default constructor assigns default values to the part (you get to pick the values).

The constructor assigns input parameters to the appropriate variables.

The accessor methods return the value that is in the function name.

The mutator methods set the value of the variable in the function name to the value of the input parameter.

The operator<< method formats output in the following way: “Type: <type value here>, Name: <name
value here>, Part Number: <part_number value here>, Price: $<price value here”
Example: Type: Tire, Name: Firestone FR710, Part Number: 1594346, Price: $84.00

The main program

You will create C++ file called abc1234_main.cpp consisting of a main function. This main program consists of a main function that should do the following: (you may have to write more loops then what is listed below, or more functions then just main)

Create a list (vector) of Auto_Parts.

Ask the user how many Auto_Parts to create.

Create a loop that runs as many times as the user states. o For each iteration, ask the user for a type, name, part number, and price.

o Create a part with those values. o Add that part to the list.

Output to terminal each part in the list.

Print out the average price for all parts in the list.

When grading this, you do not know how many Auto_Parts the GTA will put in, so make sure your code can handle different numbers of Auto_Parts.

Makefile

You are REQUIRED to provide a simple makefile that can build AND execute your program given the command “make”. The sample code from lecture 5 is a good example of a makefile to use for the homework. The only command the GTA will type is “make” to run and execute your program.

You will put all relevant files for the full credit portion of the homework in a folder called full_credit.  

Bonus question:

Create a new folder called bonus and copy all files from the full credit directory in this directory.

Implement the operator>> function in your Auto_Part class. This function will take an input stream as an input parameter. This function will read in input to create a new Auto_Part and return it.   In your main, you will open the Auto_Parts.txt file that contains the information for many Auto_Parts. Each value will be separated by a comma. This file must be used as a Command Line Argument. Your main function will run the same as the full credit version except input will come from the file and not user input.

Once that is complete, make a new makefile for this bonus to handle the command line arguments.  

Explanation / Answer

//File Name: abc1234_Auto_Part.h
#ifndef abc1234_Auto_Part_H
#define abc1234_Auto_Part_H
#include <iostream>
#include<string>
using namespace std;

// Defines a class AutoPart
class AutoPart
{
// Data member to store data
string name;
string partNumber;
double price;
public:
// Prototype of constructors
AutoPart();
AutoPart(string, string, double);
// Prototype of getter and setter functions
void setName(string);
void setPartNumber(string);
void setPrice(double);
string getName();
string getPartNumber();
double getPrice();
// Friend function overloaded for input and output
friend istream & operator >>(istream &, AutoPart &);
friend ostream & operator <<(ostream &, AutoPart &);
};// End of class
#endif

------------------------------------------------------------------------------------------

//File Name: abc1234_Auto_Part.cpp
#include <iostream>
#include "abc1234_Auto_Part.h"
using namespace std;

// Default constructor definition
AutoPart::AutoPart()
{
name = partNumber = "";
price = 0.0;
}// End of default constructor

// Parameterized constructor to assign parameter values to data member
AutoPart::AutoPart(string na, string pa, double pr)
{
name = na;
partNumber = pa;
price = pr;
}// End of parameterized constructor

// Function to set part name
void AutoPart::setName(string na)
{
name = na;
}// End of function

// Function to set part number
void AutoPart::setPartNumber(string pa)
{
partNumber = pa;
}// End of function

// Function to set part price
void AutoPart::setPrice(double pr)
{
price = pr;
}// End of function

// Function to return part name
string AutoPart::getName()
{
return name;
}// End of function

// Function to return part number
string AutoPart::getPartNumber()
{
return partNumber;
}// End of function

// Function to return part price
double AutoPart::getPrice()
{
return price;
}// End of function

// Function to overload insertion operator
istream & operator >>(istream &is, AutoPart &ap)
{
// Accepts data from the user
cout<<" Enter part name: ";
is>>ap.name;
cout<<" Enter the part number: ";
is>>ap.partNumber;
cout<<" Enter the price: ";
is>>ap.price;
// Returns the stream
return is;
}// End of function

// Function to overload extraction operator
ostream & operator <<(ostream &os, AutoPart &ap)
{
// Displays data
os<<" Part name: "<<ap.name;
os<<" Part number: "<<ap.partNumber;
os<<" Price: $"<<ap.price;
// Returns the stream
return os;
}// End of function

--------------------------------------------------------------------

//File Name: abc1234_main.cpp
#include <iostream>
#include <vector>
#include "abc1234_Auto_Part.cpp"
using namespace std;

// main function definition
int main()
{
// Creates a vector of type AutoPart
vector <AutoPart> autoparts;
// Creates an object of the class AutoPart
AutoPart ap;
// no to store number of items
// total to store calculate total price
int no, total = 0;
// Accepts number of parts from the user
cout<<" Enter how many part information you want to enter? ";
cin>>no;

// To accept information
// Loops till number of parts
for(int x = 0; x < no; x++)
{
// Accepts data from the user
cin>>ap;
// Adds the object to vector
autoparts.push_back(ap);
// Calculates total price
total += autoparts[x].getPrice();
}// End of for loop


// To display information
cout<<" Auto parts information ";
// Loops till the size of the vector
for(int x = 0; x < autoparts.size(); x++)
cout<<autoparts[x];
// Displays the average price of all the parts
cout<<" Average Price: $"<<total / autoparts.size();
}// End of main function

Sample Output:

Enter how many part information you want to enter? 3

Enter part name: Stearing

Enter the part number: 1111

Enter the price: 450.12

Enter part name: Gear

Enter the part number: 1223

Enter the price: 800.56

Enter part name: Glass.

Enter the part number: 5645

Enter the price: 200.32

Auto parts information

Part name: Stearing
Part number: 1111
Price: $450.12
Part name: Gear
Part number: 1223
Price: $800.56
Part name: Glass.
Part number: 5645
Price: $200.32
Average Price: $483

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote