C++ program with pointers | Computer Science homework help

 

The program reads data from two files, itemsList-0x.txt and inventoryList-0x.txt. File extensions on Linux may be arbitrary–i.e., these files could have been named with .dat as the extensions.

The first file, itemsList-0x.txt, lists all possible items. Each line represents one item in the form id name.

Example 1: Sample itemsList-0x.txt0 Air 1 HP Potion 2 MP Potion 5 Iron Ore 3 Bow Tie 4 Dirt 6 Diamond Ore 7 Iron Ingot 8 Diamond 9 Diamond Block 

The second file, inventoryList-0x.txt, lists each individual inventory–or storage chest–followed by a list of items.

Example 2: Sample inventoryList-0x.txt# 5 - 1 10 - 2  5 - 3  2 # 6 - 4  3 - 5 27 - 6 44 - 7 55 - 8  1 - 9  4 - 4  3 # 2 - 2  5 - 9  4 - 8  1 - 5  2 - 10 5 

Each line preceded by # denotes the start of a new inventory. Each line preceded by denotes an item. The program creates a new inventory each time a # is encountered.

When a is encountered, a stack of items, ItemStack, is created. The ItemStack is placed in the Inventory based on the following rules:

  1. If the Inventory is empty, store the ItemStack, and return true.
  2. If the Inventory is not empty, examine the Inventory.
    • If a matching ItemStack is found, merge the two ItemStacks and return true.
    • If no matching ItemStack is found, store the new ItemStack and return true.
  3. If the Inventory is full, return false.

Through the magic of abstraction, this is not one function, but four (4) functions in total. Yes, it does seem unnecessary at first. However, each function does one thing and only one thing. This is an exercise in understanding the thought process behind abstraction, interfaces, and the S/O in S.O.L.I.D (with some C++ code) in a multi-ADT program.

Most of your time will be spent on understanding the abstractions (and interfaces) as opposed to spamming cobblestone blocks… I mean C++ code.

3.2 Output

The output consists of three reports written to standard output, one after the other.

  1. A report listing items that were stored or discarded.
  2. A report listing all valid items.
  3. Finally, a detailed report is printed. listing data for each inventory:
    • Maximum Capacity–i.e., total slots.
    • Utilized Capacity–i.e., occupied slots
    • Listing of all items.

If the program is run with the provided input files, the following output should be generated…

Example 3: Sample OutputProcessing Log:  Stored (10) HP Potion  Stored ( 5) MP Potion  Stored ( 2) Bow Tie  Stored ( 3) Dirt  Stored (27) Iron Ore  Stored (44) Diamond Ore  Stored (55) Iron Ingot  Stored ( 1) Diamond  Stored ( 4) Diamond Block  Stored ( 3) Dirt  Stored ( 5) MP Potion  Stored ( 4) Diamond Block  Discarded ( 1) Diamond  Discarded ( 2) Iron Ore  Item List:    0 Air    1 HP Potion    2 MP Potion    3 Bow Tie    4 Dirt    5 Iron Ore    6 Diamond Ore    7 Iron Ingot    8 Diamond    9 Diamond Block  Storage Summary:  -Used 3 of 5 slots   (10) HP Potion   ( 5) MP Potion   ( 2) Bow Tie   -Used 6 of 6 slots   ( 6) Dirt   (27) Iron Ore   (44) Diamond Ore   (55) Iron Ingot   ( 1) Diamond   ( 4) Diamond Block   -Used 2 of 2 slots   ( 5) MP Potion   ( 4) Diamond Block  

3.3 Running the Program

The easiest way to see generate the expected output is to run the sample executable solution I have provided. These two files are named as command-line parameters when the program is executed.

For example, if the sample data above is kept in files itemList-01.txt and inventoryList-01.txt, then to run this program, do:

./storage itemList-01.txt inventoryList-01.txt

(On a Windows system, you would omit the “./”. If you are running from Code::Blocks or a similar development environment, you may need to review how to supply command-line parameters to a running program.)

4 Your Tasks

One of the most important skills in our craft is interpreting error messages. Remember the ones you receive when you attempt to compile and run the unmodified code.

The key abstractions employed in this program are Item, ItemStack, and Inventory. Complete ADT implementations have been provided for Item and Inventory.

A partial implementation has been provided for the ItemStack. Your task is to finish the update ItemStack ADT.

This assignment is smaller than the previous two (in terms of code and number of new concepts). Most of your time will be spent reviewing the basics of pointers. Spend the time reviewing. Practice with pointers. You will need to use pointers (in one form or another) for the reminder of the semester.

You must implement the:

  1. Copy Constructor
  2. Destructor
  3. Assignment Operator
    • Note this is already provided and complete. Refer to our discussions of the copy-and-swap method.
    • Once you have completed the Copy Constructor, Destructor, and swap you are done with the Big-3.
  4. Logical Equivalence (i.e., operator==).
  5. Less-Than (i.e., operator<).
  6. swap

Refer to the comments in each function for additional detail.

Employ your Head-to-Head Testing Skills from CS 250.

4.1 Three Main Functions?

As you look through the provided code, you will find three main functions: one in storage.cpp (as expected), one in TestInventory.cpp, and one in TestItemStack.cpp. If you are creating a project in your IDE do not include both in your project settings. You will need to either create multiple targets in your project settings, or rely on the makefile.

You should probably run the tests on a Linux machine… You can compile the main program (storage) and test drivers (testInventory and testItemStack) with

make

Take note of the semicolon (;) after testInventory. This is a standard Linux trick to run two commands back-to-back.

You can then run storage as described above. You can run the Inventory and ItemStack test drivers with:

./testInventory; ./testItemStack 

If you implemented everything correctly you will see:

Inventory:
 PASSED -> testDefaultConstructor
 PASSED -> testConstructorSizeN
 PASSED -> testAddItemStackNoCheck
 PASSED -> testAddItemWithDuplicateItems
 PASSED -> testAddItemAfterFull
 PASSED -> testCopyConstructorForEmpty
 PASSED -> testCopyConstructor
 PASSED -> testAssignmentOperator
 PASSED -> testDisplay
ItemStack:
 PASSED -> testDefaultConstructor
 PASSED -> testSecondConstructor
 PASSED -> testCopyConstructor
 PASSED -> testAssignment
 PASSED -> testAddItems
 PASSED -> testAddItemsFrom
 PASSED -> testLogicalEquivalence
 PASSED -> testLessThan
 PASSED -> testDisplay
 PASSED -> testSwap

If you see FAILED you must revisit revisit the corresponding function(s). There is a mistake somewhere in your code. Where should you look? Pay close attention to the line immediately before FAILED… use that as a starting point.

Remember to ask questions if you get stuck.

4.1.1 Segmentation Faults & Getting Started

Since ItemStack’s item data member is a pointer

Item* item;

segmentation faults are a consideration. If you download, compile and run the tests, without implementing anything, you will receive test output similar to:

Inventory:
 PASSED -> testDefaultConstructor
 PASSED -> testConstructorSizeN
[1]    21524 segmentation fault (core dumped)  ./testInventory
ItemStack:
[1]    21526 segmentation fault (core dumped)  ./testItemStack

Here is a free hint. Go to the Copy Constructor and add

   this->item = src.item->clone();

This line will create a deep copy of src.item. Once you have made that one-line addition, recompile everything and run

./testInventory; ./testItemStack

again. You should see:

Inventory:
 PASSED -> testDefaultConstructor
 PASSED -> testConstructorSizeN
FAILURE: testAddItemStackNoCheck:89 -> (*(it++) == stacksToAdd[0])
 FAILED -> testAddItemStackNoCheck
FAILURE: testAddItemWithDuplicateItems:126 -> (*(it++) == stacksToAdd[0])
 FAILED -> testAddItemWithDuplicateItems
FAILURE: testAddItemAfterFull:172 -> (*(it++) == stacksToAdd[0])
 FAILED -> testAddItemAfterFull
 PASSED -> testCopyConstructorForEmpty
FAILURE: testCopyConstructor:268 -> (aCopy == source)
 FAILED -> testCopyConstructor
FAILURE: testAssignmentOperator:305 -> (aCopy == source)
 FAILED -> testAssignmentOperator
FAILURE: testDisplay:204 -> (bagString.find(stacksAsStrings[0]) != std::string::npos)
 FAILED -> testDisplay
ItemStack:
 PASSED -> testDefaultConstructor
 PASSED -> testSecondConstructor
FAILURE: testCopyConstructor:70 -> (aCopy.size() == 9002)
 FAILED -> testCopyConstructor
FAILURE: testAssignment:91 -> (aCopy.size() == 9002)
 FAILED -> testAssignment
 PASSED -> testAddItems
 PASSED -> testAddItemsFrom
FAILURE: testLogicalEquivalence:142 -> (stack1 == stack2)
 FAILED -> testLogicalEquivalence
FAILURE: testLessThan:164 -> (stack3 < stack1)
 FAILED -> testLessThan
 PASSED -> testDisplay
FAILURE: testSwap:198 -> (stack1.getItem().getName() == "Ice")
 FAILED -> testSwap

There is nothing wrong with Inventory. Inventory is dependent on ItemStack. Until ItemStack is complete you will see failures in testInventory.

Punctual Essays
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.