C++Assignment 辅导、讲解C++Assignment解析C++语言编程,辅导留学生C++, 辅导C++ 语言编程|辅导C

- 首页 >> C/C++编程

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <algorithm>

using namespace std;


int MAX_TRANSFER_STATION_COUNT = 5;

enum DELIVERY_METHOD{

   air_transport, ground_transpotation, sea_transportation

   };


class GOOD

{

   string name;

   string ID;

   string start_point;

   string destination;

   DELIVERY_METHOD method;

   int delivery_count;

   int delivery_price;

   string delivery_date;

public:

   GOOD(string n = " ", string id = " ", string s_point = " ", string des = " ",

       DELIVERY_METHOD m = air_transport, int del_c = 0, int del_p = 0, string del_d = " ")

   {

       name = n; ID = id; start_point = s_point; destination = des;

       method = m;

       delivery_count = del_c; delivery_price = del_p;

       delivery_date = del_d;

   }


   void set_name(string n) {name = n;}

   void set_ID(string id) {ID = id;}

   void set_start_point(string s_point) {start_point = s_point;}

   void set_destination(string des) {destination = des;}

   void set_delivery_method(DELIVERY_METHOD m) {method = m;}

   void set_delivery_count(int del_c) {delivery_count = del_c;}

   void set_delivery_price(int del_p) {delivery_price = del_p;}

   void set_delivery_date(string del_d) {delivery_date = del_d;}


   string get_name() {return name;}

   string get_ID() {return ID;}

   string get_start_point() {return start_point;}

   string get_destination() {return destination;}

   DELIVERY_METHOD get_delivery_method() {return method;}

   int get_delivery_count() {return delivery_count;}

   int get_delivery_price() {return delivery_price;}

   string get_delivery_date() {return delivery_date;}


   void print()

   {

       cout << name << '\t' << ID << endl;

       cout << "From " << start_point << "To " << destination << endl;

       cout << "Delivery method: ";

       switch(method)

       {

           case 0:

               cout << "air_transport" << endl;

               break;

           case 1:

               cout << "ground_transpotation" << endl;

               break;

           case 3:

               cout << "sea_transportation" << endl;

               break;

           default:

               cout << "Oops, somthing is going to wrong." << endl;

       }

       cout << "Delivery count: " << delivery_count << endl;

       cout << "Delivery price: " << delivery_price << endl;

       cout << delivery_date << endl;

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

   }

};


class GOOD_MANAGE

{

   vector<GOOD> good_vec;

   bool isID(string s)

   {

       for(auto c : s)

       {

           if(c < '0' || c > '9')

               return false;

       }

       return true;

   }

public:

   GOOD_MANAGE()

   {}

   void add(GOOD g)

   {

       good_vec.push_back(g);

       cout << "Succesffuly Added!" << endl;

   }

   void show()

   {

       for(int i = 0; i < good_vec.size(); ++i)

       {

           cout << "No." << i + 1 << endl;

           good_vec[i].print();

       }

   }

   void store(string file_name = "Logistics_management.txt")

   {

       ofstream f(file_name);

       for(int i = 0; i < good_vec.size(); ++i)

       {

           

           f << "No." << i + 1 << endl;

           f << good_vec[i].get_name() << good_vec[i].get_ID() << endl;

           f << "From " << good_vec[i].get_start_point() << "To " << good_vec[i].get_destination() << endl;

           f << "Delivery method: " << good_vec[i].get_delivery_method() << endl;

           f << "Delivery count: " << good_vec[i].get_delivery_count() << endl;

           f << "Delivery price: " << good_vec[i].get_delivery_price() << endl;

           f << good_vec[i].get_delivery_date() << endl;

           f << "------------------------------------------------------" << endl << endl;

       }

       f.close();

   }

   int search(string key, bool b = true)

   {

       int i = 0;

       for(; i < good_vec.size(); ++i)

       {

           if(key == good_vec[i].get_ID() || key == good_vec[i].get_name())

           {

               if(b)

               {

                   cout << "Result:" << endl;

                   good_vec[i].print();

               }

               return i;

           }

       }

       if(b)

       {

           cout << "The Name/ID does not exit." << endl;

       }

       else

           cout << "The Name does not exit." << endl;

       return i;

   }

   //Name and ID can't be edit

   void edit_name(string old_name, string new_name)

   {

       int idx = search(old_name, false);

       if(idx != good_vec.size())

       {

           good_vec[idx].set_name(new_name);

           cout << "Successfully set." << endl;

       }

   }

   void edit_ID(string old_ID, string new_ID)

   {

       int idx = search(old_ID, false);

       if(idx != good_vec.size())

       {

           good_vec[idx].set_ID(new_ID);

           cout << "Successfully set." << endl;

       }

   }

   void edit_start_point(string name, string new_start_point)

   {

       int idx = search(name, false);

       if(idx != good_vec.size())

       {

           good_vec[idx].set_start_point(new_start_point);

           cout << "Successfully set." << endl;

       }

   }

   void edit_destination(string name, string new_des)

   {

       int idx = search(name, false);

       if(idx == good_vec.size())

       {

           good_vec[idx].set_destination(new_des);

           cout << "Successfully set." << endl;

       }

   }

   void edit_delivery_method(string name, DELIVERY_METHOD m)

   {

       int idx = search(name, false);

       if(idx != good_vec.size())

       {

           good_vec[idx].set_delivery_method(m);

           cout << "Successfully set." << endl;

       }

   }

   void edit_delivery_count(string name, int count)

   {

       int idx = search(name, false);

       if(idx != good_vec.size())

       {

           good_vec[idx].set_delivery_count(count);

           cout << "Successfully set." << endl;

       }

   }

   void edit_delivery_price(string name, int price)

   {

       int idx = search(name, false);

       if(idx != good_vec.size())

       {

           good_vec[idx].set_delivery_price(price);

           cout << "Successfully set." << endl;

       }

   }

   void edit_delivery_date(string name, string date)

   {

       int idx = search(name, false);

       if(idx != good_vec.size())

       {

           good_vec[idx].set_delivery_date(date);

           cout << "Successfully set." << endl;

       }

   }

   void del(string name)

   {

       int idx = search(name, false);

       if(idx != good_vec.size())

       {

           vector<GOOD>::iterator iter = good_vec.begin() + idx;

           good_vec.erase(iter);

           cout << "Successfully delete." << endl;

       }

   }

};


int main()

{

   GOOD good1("good1", "1", "nanjing", "shanghai", air_transport, 1, 10, "8012-8-8");

   GOOD good2("good2", "2", "shanghai", "nanjing", ground_transpotation, 1, 20, "8102-8-9");

   GOOD_MANAGE g_m;

   g_m.add(good1); g_m.add(good2);

   g_m.show();

   g_m.store();

   g_m.search("good1"); g_m.search("2");



   g_m.edit_name("good1", "good0");

   g_m.edit_name("good3", "good4");

   g_m.edit_ID("1", "0");

   g_m.edit_start_point("good0", "shanghai");

   g_m.edit_destination("good0", "nanjing");

   g_m.edit_delivery_count("good0", 2);

   g_m.edit_delivery_method("good0", sea_transportation);

   g_m.edit_delivery_price("good0", 20);

   g_m.edit_delivery_date("good0", "2018-8-8");

   g_m.show();


   g_m.del("good0");

   g_m.del("good3");

   g_m.show();

   return 0;

}


站长地图