Question
C++ Assignment. You will need to complete the following tasks. If you could do the code through Linux that would be great for me. 1.
C++ Assignment. You will need to complete the following tasks. If you could do the code through Linux that would be great for me.
1. Create isLessThan for class InventoryItem. It should compare the passed in price with the invoking item by price.
bool isLessThan (float price) const;
First add the function prototype in item.h, then put the function implementation in item.cpp and finally invoke/test the function in main. Please label your output clearly.
You need to read in the price to be compared from the user.
Sample invoking code:
InventoryItem item1; Float userPrice = 0;
if(item1.isLessThan(userPrice))
{
cout << item1 < userPrice << endl;
}
2. Create a public member function to insert one item into the inventory at the correct position. The inventory should be sorted by item name. The function returns true if the insertion is successful and it returns false if there is an existing item with the same name or the array is out of capacity.
bool addItem(const InventoryItem& anItem);
First add the public member function prototype inside class ItemList in itemList.h, then put the member function implementation in itemList.cpp and finally invoke/test the function in main.
You need to read in the item to be added from the user. When reading in from the user, you can read in individual attributes and invoke the set methods in InventoryItem.
You need to display the list after testing the addItem function. Please label your output clearly.
Use the provided makefile to compile your code:
To compile: make
To run: main
Note: If you dont have . In your PATH environment variable, you need to type: ./main
To clean up: make clean
We have these code given:
item.h
#pragma once #include
const int MAX_CHAR = 101;
class InventoryItem { private: char itemName[MAX_CHAR]; float itemPrice;
public: InventoryItem();
void setItemName(const char name[]); const char* getItemName() const;
void setItemPrice(float price); float getItemPrice() const;
void print() const; };
item.cpp
#include "item.h"
InventoryItem::InventoryItem() { strcpy(itemName, "no item"); itemPrice = 0; }
void InventoryItem::setItemName(const char name[]) { strcpy(itemName, name); }
const char* InventoryItem::getItemName() const { return itemName; }
void InventoryItem::setItemPrice(float price) { itemPrice = price; }
float InventoryItem::getItemPrice() const { return itemPrice; }
void InventoryItem::print() const { cout << itemName << "\t" << itemPrice << endl; }
main.h
#pragma once #include
int main(int argc, char ** argv);
main.cpp
#include "main.h"
int main(int argc, char ** argv) { ItemList inventory; char fileName[] = "items.txt";
//open file to read data and populate inventory ifstream in; in.open(fileName); if(!in) { cerr << "Fail to open " << fileName << " for input!" << endl; return 1; } inventory.readList(in);
//invoke your functions here to do what is required
cout << endl << "Current Inventory: " << endl; inventory.printList(); return 0; }
itemList.h
#pragma once #include "item.h"
const int CAPACITY = 100; class ItemList { private: InventoryItem list[CAPACITY]; int size; int capacity; public: ItemList(); InventoryItem& get (int index); void append(const InventoryItem& anItem);
void printList() const; void readList(istream& in); };
itemList.cpp
#include "itemList.h" ItemList::ItemList() { size = 0; capacity = CAPACITY; }
InventoryItem& ItemList::get (int index) { return list[index]; }
void ItemList::append(const InventoryItem& anItem) { list[size] = anItem; size++; }
void ItemList::readList(istream& in) { char itemName[MAX_CHAR]; float itemPrice; InventoryItem anItem;
in.get(itemName, MAX_CHAR, ':'); while (!in.eof()) { in.get(); in >> itemPrice; in.get(); anItem.setItemName(itemName); anItem.setItemPrice(itemPrice); append(anItem);
in.get(itemName, MAX_CHAR, ':'); } } void ItemList::printList() const { int index;
cout << fixed; cout.precision(2); for(index = 0; index < size; index++) { list[index].print(); } }
item.txt
apple:0.99 banana:0.69 cookie:0.50 donut:1.00 egg:3.88 fish:5.88 milk:2.99 yogurt:6.38
makefile
CC = g++ CPPFLAGS = -std=c++11 -g -Wall
main: main.o item.o itemList.o
main.o: main.h item.h itemList.h
itemList.o: item.h itemList.h
item.o: item.h
clean: rm *.o main
------------------------------------------------------------------------------------------------------------------------------------------------
After you write your functions your Sample Run could look like this:
Current Inventory: apple 0.99 banana 0.69 cookie 0.50 donut 1.00 egg 3.88 fish 5.88 milk 2.99 yogurt 6.38 Enter name of item1: pear Enter price of item1: 3.40 Enter price to compare: 5.99 Item1 < 5.99 Enter name of item to be added: pear Enter price of item: 3.40 After adding the new item, the updated list is: apple 0.99 banana 0.69 cookie 0.50 donut 1.00 egg 3.88 fish 5.88 milk 2.99 pear 3.40 yogurt 6.38 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started