pemby / inventory.cpp secret
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

linked list

View inventory.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
//
// 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;
}
}
 
 
View inventory.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#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__) */
View inventory.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#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;
}
View inventory.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#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__) */
View inventory.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
//
// 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.