+1 (315) 557-6473 

Program To Implement a Delivery Program Using a Linked List Data Structure in C++ Language Assignment Solution.


Instructions

Objective
Write a C++ homework to implement a delivery program using a linked list data structure.

Requirements and Specifications

implement delivery program using a linked list data structure in C++
implement delivery program using a linked list data structure in C++ 1

Source Code

/*

*

* COSC 051 Fall 2020

* Project #5 Code

*

* Due on: 7 December 2020

*

* In accordance with class policies and Georgetown’s Honor Code,

* I certify that, with the exception of the class resources and those

* items noted below, I have neither given nor received any assistance

* on this project.

*

* Note that you may use without citation any help from our TAs,

* professors, or any code taken from the course textbook.

*/

#include

#include

#include

using namespace std;

class Delivery {

 friend istream& operator>>(istream& in, Delivery& d) {

  string itemsLine;

  unsigned int milk;

  unsigned int cheese;

  unsigned int meat;

  getline(in, d.orderID);

  getline(in, d.address1);

  getline(in, d.address2);

  getline(in, itemsLine);

  stringstream s(itemsLine);

  s >> d.milk;

  s >> d.cheese;

  s >> d.meat;

  return in;

 }

 friend ostream& operator<<(ostream& out, Delivery& d) {

  out << d.orderID << endl;

  out << d.address1 << endl;

  out << d.address2 << endl;

  out << "Milk: " << d.milk << endl;

  out << "Cheese: " << d.cheese << endl;

  out << "Meat: " << d.meat << endl;

  return out;

 }

public:

 Delivery() {

  orderID = "";

  address1 = "";

  address2 = "";

  milk = 0;

  cheese = 0;

  meat = 0;

  nextDelivery = nullptr;

 }

 Delivery(string newID, string newA1, string newA2, unsigned int newMilk, unsigned int newCheese, unsigned int newMeat) {

  orderID = newID;

  address1 = newA1;

  address2 = newA2;

  milk = newMilk;

  cheese = newCheese;

  meat = newMeat;

  nextDelivery = nullptr;

 }

 void printAddress() {

  cout << address1 << " " << address2 << endl;

 }

 void printItems() {

  cout << "Milk: " << milk << endl;

  cout << "Cheese: " << cheese << endl;

  cout << "Meat: " << meat << endl;

 }

 void setNextDelivery(Delivery* newNext) {

  nextDelivery = newNext;

 }

 Delivery* getNextDelivery() const {

  return nextDelivery;

 }

 string getID() const {

  return orderID;

 }

 unsigned int getMilk() const {

  return milk;

 }

 unsigned int getCheese() const {

  return cheese;

 }

 unsigned int getMeat() const {

  return meat;

 }

private:

 string orderID;

 string address1;

 string address2;

 unsigned int milk;

 unsigned int cheese;

 unsigned int meat;

 Delivery* nextDelivery;

};

class Route {

public:

 Route() {

  head = nullptr;

 }

 Route(string filename) {

  head = nullptr;

  load_route_file(filename);

 } // Create new Route and load file

 int load_route_file(string filename) {

  ifstream f(filename);

  int n = 0;

  if (!f) {

   cout << "Can not open file: " + filename << endl;

   return n;

  }

  while (true) {

   Delivery *d = new Delivery();

   if (!(f >> (*d))) {

    delete d;

    break;

   }

   add_delivery(d);

   n++;

  }

  f.close();

  return n;

 } // Returns number of deliveries loaded

 void print_full_route() {

  Delivery* current = head;

  while (current != nullptr) {

   cout << (*current) << endl;

   current = current->getNextDelivery();

  }

 }

 void print_addresses() {

  Delivery* current = head;

  while (current != nullptr) {

   current->printAddress();

   current = current->getNextDelivery();

  }

 }

 void print_items() {

  Delivery* current = head;

  while (current != nullptr) {

   current->printItems();

   cout << endl;

   current = current->getNextDelivery();

  }

 }

 void count_items() {

  Delivery* current = head;

  unsigned int milk = 0;

  unsigned int cheese = 0;

  unsigned int meat = 0;

  while (current != nullptr) {

   milk += current->getMilk();

   cheese += current->getCheese();

   meat += current->getMeat();

   current = current->getNextDelivery();

  }

  cout << "Total milk: " << milk << endl;

  cout << "Total cheese: " << cheese << endl;

  cout << "Total meat: " << meat << endl;

 }

 void print_order(string ID) {

  Delivery* d = find_delivery(ID);

  if (d == nullptr) {

   cout << "No order with ID " << ID << " was found" << endl;

  }

  else {

   cout << (*d) << endl;

  }

 }

 void clear_deliveries() {

  Delivery* current = head;

  while (current != nullptr) {

   Delivery* next = current->getNextDelivery();

   delete current;

   current = next;

  }

  head = nullptr;

 }

private:

 void add_delivery(Delivery *d) {

  d->setNextDelivery(head);

  head = d;

 } // Add parameter to list

 Delivery* find_delivery(string ID) {

  Delivery* current = head;

  while (current != nullptr) {

   if (current->getID() == ID) {

    return current;

   }

   current = current->getNextDelivery();

  }

  return nullptr;

 } // Return matching Delivery or nullptr

 Delivery* head; // Head pointer of linked list

};

char showMenu() {

 string line;

 while (true) {

  cout << "Please, choose on of the options:" << endl;

  cout << "Load a delivery file (L)" << endl;

  cout << "Show all delivery information (D)" << endl;

  cout << "Show delivery addresses (A)" << endl;

  cout << "Show delivery items (I)" << endl;

  cout << "Show counts of items on route (C)" << endl;

  cout << "Find an order by ID (F)" << endl;

  cout << "Clear all loaded data (X)" << endl;

  cout << "Quit the program (Q)" << endl;

  cin >> line;

  char choice = toupper(line[0]);

  if (choice == 'L') {

   return 'L';

  }

  else if (choice == 'D') {

   return 'D';

  }

  else if (choice == 'A') {

   return 'A';

  }

  else if (choice == 'I') {

   return 'I';

  }

  else if (choice == 'C') {

   return 'C';

  }

  else if (choice == 'F') {

   return 'F';

  }

  else if (choice == 'X') {

   return 'X';

  }

  else if (choice == 'Q') {

   return 'Q';

  }

  else {

   cout << "Invalid input. Please, try again" << endl;

   cout << endl;

  }

 }

}

int main() {

 Route route;

 bool isOver = false;

 string filename;

 string userId;

 int size = 0;

 while (!isOver) {

  switch(showMenu()) {

   case 'L':

    cout << "Please, enter delivery filename: ";

    cin >> filename;

    size = route.load_route_file(filename);

    break;

   case 'D':

    if (size == 0) {

     cout << "Delivery data was not loaded yet" << endl;

    }

    else {

     route.print_full_route();

    }

    break;

   case 'A':

    if (size == 0) {

     cout << "Delivery data was not loaded yet" << endl;

    }

    else {

     route.print_addresses();

    }

    break;

   case 'I':

    if (size == 0) {

     cout << "Delivery data was not loaded yet" << endl;

    }

    else {

     route.print_items();

    }

    break;

   case 'C':

    if (size == 0) {

     cout << "Delivery data was not loaded yet" << endl;

    }

    else {

     route.count_items();

    }

    break;

   case 'F':

    if (size == 0) {

     cout << "Delivery data was not loaded yet" << endl;

    }

    else {

     cout << "Please, enter user id: ";

     cin >> userId;

     route.print_order(userId);

    }

    break;

   case 'X':

    route.clear_deliveries();

    cout << "Data was cleared" << endl;

    break;

   case 'Q':

    isOver = true;

    break;

  }

  cout << endl;

 }

 cout << "Good Bye!" << endl;

}