Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
2.4 kB
13
Indexable
Never
void read_orders(const string& filename) {
    ifstream orderFile(filename);
    if (!orderFile.is_open()) {
        cout << "Unable to open " << filename << endl;
        return;
    }

    cout << filename << " opened successfully." << endl;

    int lineCount = 0;
    string line;

    while (getline(orderFile, line)) {
        if (lineCount == 0) {
            auto orderFields = split(line, ',');
            if (orderFields.size() >= 3) {
                order customerOrder;
                customerOrder.cust_id = stoi(orderFields[0]);
                customerOrder.order_id = stoi(orderFields[1]);
                customerOrder.order_date = orderFields[2];

                for (size_t i = 3; i < orderFields.size(); ++i) {
                    lineitem item;
                    size_t pos = orderFields[i].find('-');
                    if (pos != string::npos && pos > 0 && pos < orderFields[i].size() - 1) {
                        item.item_id = stoi(orderFields[i].substr(0, pos));
                        item.quantity = stoi(orderFields[i].substr(pos + 1));
                        customerOrder.line_items.push_back(item);
                    }
                }
                sort(customerOrder.line_items.begin(), customerOrder.line_items.end());
                orders.push_back(customerOrder);
            }
        }
        else if (lineCount == 1) {
            auto paymentFields = split(line, ',');
            if (paymentFields.size() >= 2) {
                int paymentType = stoi(paymentFields[0]);
                payment* customerPayment = nullptr;

                if (paymentType == 1 && paymentFields.size() >= 3) {
                    customerPayment = new credit(paymentFields[1], paymentFields[2]);
                }
                else if (paymentType == 2 && paymentFields.size() >= 2) {
                    customerPayment = new paypal(paymentFields[1]);
                }
                else if (paymentType == 3 && paymentFields.size() >= 3) {
                    customerPayment = new wiretransfer(paymentFields[1], paymentFields[2]);
                }

                if (customerPayment) {
                    orders.back().Payment = customerPayment;
                }
            }
        }
        lineCount = (lineCount + 1) % 2;
    }
    orderFile.close();
}
Leave a Comment