Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/*********************************************************************** // Final project Milestone 51 // // File: main51.cpp // Version 1.0 // Author Fardad Soleimanloo // Description // // Revision History // -----------------------------------------------------------

/*********************************************************************** // Final project Milestone 51 // // File: main51.cpp // Version 1.0 // Author Fardad Soleimanloo // Description // // Revision History // ----------------------------------------------------------- ***********************************************************************/

#define _CRT_SECURE_NO_WARNINGS #include "AidMan.h" #include "Perishable.h" #include #include #include #include #include #include #include

using namespace std;

namespace sdds {

int AidMan::search(int sku) const { for (int i = 0; i < numProducts; i++) { if (products[i]->operator==(sku)) { return i; } } return -1; }

void AidMan::add() //: Menu_Obj("1- Perishable 2- Non-Perishable ----------------- ") { if (products[numProducts - 1] != nullptr) { products[numProducts - 1]->read(cin); }

save();

itemdel(); } void AidMan::remove(int index) { iProduct* temp = products[index];

for (int i = index; i < numProducts - 1; i++) { products[i] = products[i+1]; }

delete temp; numProducts--; save();

}

void AidMan::itemdel() { for (int i = 0; i < numProducts; i++) { delete products[i]; products[i] = nullptr; }

numProducts = 0; } unsigned int AidMan::menu() const {

cout << "Aid Management System" << endl; cout << "Date: " << m_date<< endl;

cout << "Data file: " << ((m_file) ? m_file : "No file"); cout << endl << "---------------------------------" << endl;

return Menu_Obj.run(); }

AidMan::AidMan(const char* file) : Menu_Obj("List Items\tAdd Item\tRemove Item\tUpdate Quantity\tSort\tShip Items\tNew/Open Aid Database ---------------------------------") { if (file != nullptr) { m_file = new char[strlen(file) + 1]; strcpy(m_file, file); } else { m_file = nullptr; } }

void AidMan::run() { ifstream ifstr; int n;

do { n = menu();

if (n != 0 && !m_file && n != 7) { //cout << "Database not loaded! Redirecting to Open Database menu..." << endl; n = 7;

} switch (n) { case 1: if (!m_file) { //cout << "****New/Open Aid Database****" << endl; n = 7; } else { cout << endl << "****List Items****" << endl; list("list"); break; } case 2: cout << endl << "****Add Item****" << endl;

if (numProducts > sdds_max_num_items) { cout << "Database full!"; } else { int choice = 0; int item_count = 0; Menu tempMenu("Perishable\tNon-Perishable -----------------"); choice = tempMenu.run();

if (choice == 0) { cout << "Aborted "; return; } if (choice == 1) {

products[numProducts] = new Perishable(); item_count = products[numProducts]->readSku(cin); save(); } else if (choice == 2) {

products[numProducts] = new Item(); item_count = products[numProducts]->readSku(cin); save(); }

if (search(item_count) != -1) { cout << "Sku: " << item_count << " is already in the system, try updating quantity instead. "; //delete products[numProducts];

} else {

numProducts++;

add();

load("data.dat"); save();

cout << " ";

}

} break; case 3:

cout << endl << "****Remove Item****" << endl; cout << "Item description: "; char str[10]; cin.ignore(); cin.getline(str, ' ');

list(str);

if (true) { int choice = 0; int sku; cout << "Enter SKU: "; cin >> sku; int i = search(sku); cout << "Following item will be removed: "; //<< endl; //cout << std::setw(10) << setfill(' ') << "";

products[i]->linear(false); cout << *products[i] << endl;

cout << "Are you sure" << endl; Menu tempMe("Yes!"); choice = tempMe.run();

if (choice == 1) { remove(i); cout << "Item removed! "; } } break; case 4: cout << endl << "****Update Quantity****" << endl << endl;

cout << "Item description: "; char strst[10]; cin.ignore(); cin.getline(strst, ' ');

list(strst);

if (true) { int cho = 0; int sku2; cout << "Enter SKU: "; cin >> sku2; int i = search(sku2);

Menu tempMeu("Add\tReduce"); cho = tempMeu.run();

if (cho == 0) { cout << "Aborted!" << endl << endl; } if (cho == 1 && numProducts) { cout << "Quantity Needed already fulfilled!" << endl << endl; } if (cho == 2) { int reduce_num=0;

cout << "Quantity to reduce: "; cin >> reduce_num;

//get_int();

} }

break; case 5: cout << endl << "****Sort****" << endl << endl; break; case 6: cout << endl << "****Ship Items****" << endl << endl; break; case 7: cout << endl << "****New/Open Aid Database****" << endl;

m_file = new char[50];

cout << "Enter file name: ";

cin.ignore(1000, ' ');

cin.getline(m_file, 50, ' ');

load("data.dat");

cout << numProducts << " records loaded! "; break; case 0: cout << "Exiting Program!" << endl; break; }

} while (n != 0); }

void AidMan::save() const { if (m_file) { std::ofstream outfile(m_file);

if (outfile.is_open()) {

for (int i = 0; i < numProducts; ++i) {

products[i]->save(outfile); outfile << ' '; }

outfile.close(); } }

} void AidMan::deallocate() {

for (size_t i = numProducts; i > 0;) { --i; delete products[i]; products[i] = nullptr; } numProducts = 0; }

bool AidMan::load(const char* File) { if (File) { delete[] m_file; m_file = nullptr; }

if (File != nullptr && File[0] != '\0') { m_file = new char[strlen(File) + 1]; strcpy(m_file, File); }

std::ifstream ifstr(m_file); if (!ifstr.is_open()) { // // ... return false; }

//deallocate(); numProducts = 0;

while (numProducts < sdds_max_num_items && ifstr.peek() != EOF) { char type = ifstr.peek(); if (type >= '1' && type <= '3') { products[numProducts] = new Perishable(); } else if (type >= '4' && type <= '9') { products[numProducts] = new Item(); } else { ifstr.clear(); ifstr.ignore(1000, ' '); continue; }

if (products[numProducts]->load(ifstr)) { numProducts++; } else { delete products[numProducts]; } }

ifstr.close();

// //cout << "Enter file name: " << m_file << endl; //cout << numProducts << " records loaded! " << endl; save(); if (numProducts > 0) { return true;

} return false; }

int AidMan::list(const char* sub_desc) { if (numProducts < 1) { cout << "The list is emtpy! "; } else { cout << " ROW | SKU | Description | Have | Need | Price | Expiry" << endl; cout << "-----+-------+-------------------------------------+------+------+---------+-----------" << endl;

for (int i = 0; i < numProducts; i++) { if (strstr(sub_desc, "list") || *products[i] == sub_desc) { cout.fill(' '); cout.setf(ios::right); cout.width(4); cout << i + 1 << " "; cout << "| "; products[i]->linear(true); cout << *products[i]; cout << endl; } }

cout << "-----+-------+-------------------------------------+------+------+---------+-----------" << endl;

if (strstr(sub_desc, "list")) { char choice; cout << "Enter row number to display details or to continue: "; cout << "> "; //<< endl; cout << endl; cin.ignore(); choice = getchar();

if (isdigit(choice)) { int op = (choice - '0') - 1;

products[op]->linear(false); cout << *products[op] << endl; }

//cout << " ";

} // else // { // int sku; // cout << "Enter SKU: "; // cout << "> "; // cin>>sku; // cout<<" "; // }

}

return numProducts;

}

/*do { cout << "Enter row number to display details or to continue:" << endl; cout << "> "; cin.ignore(); if (cin.peek() == ' ') {

cout << " "; break; } cin >> number; if (number >= 1 && number <= rowNum) { products[number - 1]->linear(false); products[number - 1]->display(cout); cout << ' ' << ' ';

} else { cout << "Invalid input. Please enter a valid row number." << endl; cin.clear(); cin.ignore(numeric_limits::max(), ' '); }

cin.get(); // Consume the newline character

} while (!validInput);

return rowNum; }*/ /*void AidMan::addProduct(iProduct* product) { if (numProducts < sdds_max_num_items) { products[numProducts] = product; numProducts++; } }*/

AidMan::operator bool() const { return m_file && numProducts; }

/*AidMan::AidMan(const char* filename) : File(nullptr) { if (filename) {

File = new char[std::strlen(filename) + 1]; std::strcpy(File, filename); } else { File = nullptr; } // Menu_Obj Menu_Obj.setMenu("1- List Items 2- Add Item 3- Remove Item 4- Update Quantity 5- Sort 6- Ship Items 7- New/Open Aid Database --------------------------------- ", 7); }*/

AidMan::~AidMan() { delete[] m_file; m_file = nullptr; deallocate(); } }

This is my code and I need help fo finishing this.

Milestone 54

Update Quantity

  • Prompts "Item description: " and receives a sub-description from the user dynamically.
  • Lists all the iProducts containing the description; if no match is found, it prints "No matches found! "
  • After displaying all the matches it asks the user for the SKU of the iProduct that needs updating by prompting "Enter SKU: "
  • Then it will search for the SKU to find the index of the iProcuct in the iProduct Pointers.
  • If a match is not found it will print "SKU not found! " and exits
  • After finding a match it will display a menu for adding or reducing the quantity:
1- Add 2- Reduce 0- Exit > 

  • if the user selects exit it will print "Aborted! " and exit.
  • If add is selected a fool-proof quantity value is received from 1 up to the maximum amount needed to fulfill the needed quantity using the prompt: "Quantity to add: ". Then the quantity is increased by the entered amount. After increasing quantity, a confirmation message is printed as follows: "X items added!", replacing X with the amount.
  • If reduce is selected a fool-proof quantity value is received from 1 up to the quantity on hand using the prompt: "Quantity to Reduce: ". Then the quantity is reduced by the entered amount. After reducing quantity a confirmation message is printed as follows: "X items removed!", replacing X with the amount.
  • Selecting Add on an already fulfilled needed quantity should result in issuing the message: "Quantity Needed already fulfilled! "
  • Selecting Reduce on a zero quantity should result in issuing the message: "Quaintity on hand is zero! "

Tester Programs - Submission Instructions

Milestone 55

Sort

Sorts the items in the iProduct Pointers array, based on difference between quantity needed and quantity on hand in descending order. When completed it will print "Sort completed! "

Tester Programs - Submission Instructions

Milestone 56

Ship Items

  • Make an ofstream for shipping-order-file under the name "shippingOrder.txt".
  • Print in the file: "Shipping Order, Date: 9999/99/99 " (9999/99/99 is replaced by the current date)
  • Print the table titles as follows:
 ROW | SKU | Description | Have | Need | Price | Expiry -----+-------+-------------------------------------+------+------+---------+----------- 

  • In a loop go through all the iProduct Pointers elements and if the quantity needed and quantity on hand of the product is a match print it in the linear format into the file and remove it from the iProduct Pointers array.
  • Count the number of printed (shipped) items
  • At end close the table by printing:
-----+-------+-------------------------------------+------+------+---------+----------- 

  • end the process by printing the number of items shipped on the screen: ""Shipping Order for 999 times saved!"" , 999 is replaced by the number of shipped (removed) items.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions