Skip to content

Instantly share code, notes, and snippets.

@tungurlakachakcak
Created January 4, 2022 00:21
Show Gist options
  • Save tungurlakachakcak/f81f1cf113f948f4ce62ded9830a4cd5 to your computer and use it in GitHub Desktop.
Save tungurlakachakcak/f81f1cf113f948f4ce62ded9830a4cd5 to your computer and use it in GitHub Desktop.
Code kata for a order list
#include <iostream>
#include <cstdint>
#include <vector>
#include <limits>
#include <cstring>
#include <sstream>
#include <chrono>
#include <set>
#include <stack>
#include <map>
#include <algorithm>
using namespace std;
struct OrderItem {
string name;
double price;
int quantity;
double calculateTotalPrice() const {
return price * quantity;
}
bool operator<(const OrderItem& other) const {
return calculateTotalPrice() < other.calculateTotalPrice();
}
friend ostream& operator<<(ostream& os, const OrderItem& item) {
os << item.name << " " << item.calculateTotalPrice();
return os;
}
};
struct Order {
vector<OrderItem> orderItems;
double calculateTotalPrice() const {
double total = 0.0;
for (const OrderItem& item : orderItems) {
total += item.calculateTotalPrice();
}
return total;
}
void sorted() {
sort(orderItems.rbegin(), orderItems.rend());
}
friend ostream& operator<<(ostream& os, const Order& order) {
os << "The total sum is: " << order.calculateTotalPrice() << " lv." << endl;
for (const OrderItem& item : order.orderItems) {
os << item << endl;
}
return os;
}
};
int main() {
int64_t numberOfShoppingItems;
cin >> numberOfShoppingItems;
Order order;
for (int64_t shoppingItemId = 0; shoppingItemId < numberOfShoppingItems; shoppingItemId++) {
OrderItem item;
cin >> item.name >> item.price >> item.quantity;
order.orderItems.push_back(item);
}
order.sorted();
cout << order;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment