Skip to content

@pemby /inventory.cpp secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
linked list
//
// Inventory.cpp
#include "Inventory.h"
#include <cstdlib>
#include <iostream>
#include "Item.h"
using namespace std;
Inventory::Inventory()
{
head = nullptr;
curr = nullptr;
temp = nullptr;
}
Inventory::~Inventory()
{
}
void Inventory::AddItem(const Item& addData){
nodePtr n = new node;
n->next = nullptr;
n->aItem = addData;
if (head != nullptr)
{
curr = head;
while (curr->next != nullptr)
{
curr = curr->next;
}
curr->next = n;
}
else
{
head = n;
}
}
void Inventory::RemoveItem(char *delData){
nodePtr delPtr = nullptr;
temp = head;
curr = head;
while (curr != nullptr && curr->aItem.getName() != delData) // overload =!
{
temp = curr;
curr = curr->next;
}
if (curr == nullptr)
{
cout << delData << " was not found i n the Inventory \n";
delete delPtr;
}
else
{
delPtr = curr;
curr = curr->next;
temp->next = curr;
cout << "The value " << delData << " was deleted\n";
delete delPtr;
}
}
void Inventory::PrintInventory(){
curr = head;
while (curr != nullptr)
{
cout << curr->aItem;
curr = curr->next;
}
}
#ifndef __lab1__Inventory__
#define __lab1__Inventory__
#include <iostream>
#include "Item.h"
class Inventory {
private:
typedef struct node
{
Item aItem;
node* next;
}* nodePtr;
nodePtr head;
nodePtr curr;
nodePtr temp;
public:
Inventory();
virtual ~Inventory();
void AddItem(const Item& addData);
void RemoveItem(char *delData);
void PrintList();
void PrintInventory();
};
#endif /* defined(__lab1__Inventory__) */
#include "Item.h"
#include <iostream>
#include <iomanip>
#include <string.h>
#pragma warning(disable:4996)
using namespace std;
Item::Item() {
name = nullptr;
weight = 0;
}
Item::Item(char *name, double weight) {
name = nullptr;
weight = 0;
Item::setName(name);
}
char *Item::getName() const
{
return this->name;
}
Item::Item(const Item& orig) {
}
Item::~Item() {
if(name)
{
delete [] name;
}
}
void Item::GetName(char *name)const
{
strcpy(name, this->name);
}
double Item::getWeight() const
{
return this->weight;
}
void Item::setName(char* name)
{
if(this->name)
delete [] this->name;
this->name = new char[strlen(name)+1];
strcpy(this->name, name);
}
void Item::setWeight(double weight)
{
this->weight = weight;
}
ostream& operator<<(ostream& out, const Item& aItem)
{
out << aItem.name << " " <<aItem.weight << endl;
return out;
}
const Item& Item::operator=(const Item& aItem)
{
if(this == &aItem)
return *this;
else
{
setName(aItem.name);
setWeight(aItem.weight);
}
}
bool operator==(const Item& item_1, const Item& item_2)
{
if(strcmp(item_1.getName(), item_2.getName()) == 0)
return true;
else
return false;
}
bool operator!=(const Item& item_1, const Item& item_2)
{
if(strcmp(item_1.getName(), item_2.getName()) != 0)
{
if (item_1.getWeight() != item_2.getWeight()) {
return true;
}
}
else
return false;
}
#ifndef __lab1__Item__
#define __lab1__Item__
#include <iostream>
class Item {
public:
Item();
Item(char *name, double weight);
Item(const Item& orig);
virtual ~Item();
//Getters
void GetName(char *name) const;
double getWeight() const;
//Setters
void setName(char* name);
void setWeight(double weight);
char *getName() const;
//Overloading operators
friend std::ostream& operator<<(std::ostream& out, const Item& aItem);
const Item& operator=(const Item& aItem);
private:
char *name;
double weight;
};
//Operators
bool operator< (const Item& item_1, const Item& item_2);
bool operator== (const Item& item_1, const Item& item_2);
bool operator!= (const Item& item_1, const Item& item_2);
#endif /* defined(__lab1__Item__) */
//
// main.cpp
// lab1
#include <iostream>
#include "item.h"
#include "inventory.h"
#include <stdlib.h>
#include <string.h>
#pragma GCC diagnostic ignored "-Wwrite-strings"
#ifdef _WIN32
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
using namespace std;
void AddItem(Inventory& inv,char* name,double weight)
{
int len = strlen(name)+1;
char* itemName = new char[len];
strncpy(itemName,name,len);
cout << "Adding " << itemName << " with a weight of " << weight << "." << endl;
inv.AddItem(Item(itemName,weight));
delete [] itemName;
}
void RemoveItem(Inventory& inv,char* name)
{
cout << "Removing " << name << "." << endl;
inv.RemoveItem(name);
}
int main(int argc, const char * argv[])
{
std::cout << "start" << endl;
Inventory inv;
std::cout << "class" << endl;
// Make sure printing an empty inventory works
inv.PrintInventory();
std::cout << "print 1"<< endl;
// Make sure adding the first one works
AddItem(inv,"helmet",5);
std::cout << "add 1"<< endl;
inv.PrintInventory();
std::cout << "print 2"<< endl;
std::cout << "Hello, World!\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.