trash.cpp #include <iostream> #include <iomanip> #include <limits> #include <string> using namespace std; string itemName[100]; string itemCategory[100]; int itemPrice[100]; int itemQuantity[100]; int itemCount = 0; int vip = 0; int main() { int menu = -1; while (menu != 0) { cout << "\n1.Add 2.List 3.Checkout 4.ToggleVIP 0.Exit\n> "; cin >> menu; if (menu == 1) { if (itemCount == 100) { cout << "Cart is full.\n"; } else { cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "Name: "; getline(cin, itemName[itemCount]); cout << "Unit price: "; cin >> itemPrice[itemCount]; cout << "Quantity: "; cin >> itemQuantity[itemCount]; cout << "Category(food/other): "; cin >> itemCategory[itemCount]; itemCount++; cout << "Added.\n"; } } if (menu == 2) { long long subtotal = 0; long long discount = 0; for (int i = 0; i < itemCount; i++) { long long line = itemPrice[i] * itemQuantity[i]; subtotal += line; if (itemCategory[i] == "food") { discount += line * 5 / 100; } cout << i + 1 << ". " << itemName[i] << " x" << itemQuantity[i] << " = " << line << "\n"; } if (vip == 1) { discount += (subtotal - discount) * 10 / 100; } long long taxable = subtotal - discount; long long shipping = taxable >= 50000 ? 0 : 3000; long long tax = taxable * 10 / 100; cout << "VIP: " << (vip == 1 ? "ON" : "OFF") << "\n"; cout << "Subtotal: " << subtotal << "\n"; cout << "Discount: " << discount << "\n"; cout << "Shipping: " << shipping << "\n"; cout << "Tax: " << tax << "\n"; cout << "Total: " << taxable + shipping + tax << "\n"; } if (menu == 3) { long long a = 0; long long b = 0; for (int i = 0; i < itemCount; i++) { long long x = itemPrice[i] * itemQuantity[i]; a = a + x; if (itemCategory[i] == "food") { b = b + x * 5 / 100; } } if (vip == 1) { b = b + (a - b) * 10 / 100; } long long c = a - b; long long d = c >= 50000 ? 0 : 3000; long long e = c * 10 / 100; cout << "Charge: " << c + d + e << "\n"; itemCount = 0; cout << "Cart cleared.\n"; } if (menu == 4) { if (vip == 0) { vip = 1; } else { vip = 0; } cout << "VIP is " << (vip == 1 ? "ON" : "OFF") << ".\n"; } } return 0; }
refactoring.cpp #include <iostream> #include <limits> #include <string> #include <utility> #include <vector> using Money = long long; struct Item { std::string name; std::string category; Money unitPrice; int quantity; }; struct Totals { Money subtotal; Money discount; Money shipping; Money tax; Money grandTotal() const { return subtotal - discount + shipping + tax; } }; class Cart { public: void add(Item item) { items_.push_back(std::move(item)); } void toggleVip() { vip_ = !vip_; } bool isVip() const { return vip_; } const std::vector<Item>& items() const { return items_; } Totals calculate() const { Money subtotal = 0; Money categoryDiscount = 0; for (const Item& item : items_) { const Money lineTotal = item.unitPrice * item.quantity; subtotal += lineTotal; if (item.category == "food") { categoryDiscount += percentOf(lineTotal, kFoodDiscountPercent); } } const Money afterCategoryDiscount = subtotal - categoryDiscount; const Money vipDiscount = vip_ ? percentOf(afterCategoryDiscount, kVipDiscountPercent) : 0; const Money discount = categoryDiscount + vipDiscount; const Money taxable = subtotal - discount; const Money shipping = taxable >= kFreeShippingMinimum ? 0 : kShippingFee; const Money tax = percentOf(taxable, kTaxPercent); return {subtotal, discount, shipping, tax}; } void clear() { items_.clear(); } private: static constexpr int kFoodDiscountPercent = 5; static constexpr int kVipDiscountPercent = 10; static constexpr int kTaxPercent = 10; static constexpr Money kFreeShippingMinimum = 50000; static constexpr Money kShippingFee = 3000; static Money percentOf(Money amount, int percent) { return amount * percent / 100; } std::vector<Item> items_; bool vip_ = false; }; int readInt(const std::string& prompt, int minimum) { int value; while (true) { std::cout << prompt; if (std::cin >> value && value >= minimum) { return value; } std::cout << "Invalid input.\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } } std::string readLine(const std::string& prompt) { std::cout << prompt; std::string value; std::getline(std::cin >> std::ws, value); return value; } void printCart(const Cart& cart) { int index = 1; for (const Item& item : cart.items()) { const Money lineTotal = item.unitPrice * item.quantity; std::cout << index++ << ". " << item.name << " x" << item.quantity << " = " << lineTotal << "\n"; } const Totals totals = cart.calculate(); std::cout << "VIP: " << (cart.isVip() ? "ON" : "OFF") << "\n" << "Subtotal: " << totals.subtotal << "\n" << "Discount: " << totals.discount << "\n" << "Shipping: " << totals.shipping << "\n" << "Tax: " << totals.tax << "\n" << "Total: " << totals.grandTotal() << "\n"; } int main() { Cart cart; while (true) { std::cout << "\n1.Add 2.List 3.Checkout 4.ToggleVIP 0.Exit\n"; const int menu = readInt("> ", 0); if (menu == 0) { break; } if (menu == 1) { Item item; item.name = readLine("Name: "); item.unitPrice = readInt("Unit price: ", 0); item.quantity = readInt("Quantity: ", 1); item.category = readLine("Category(food/other): "); cart.add(std::move(item)); std::cout << "Added.\n"; } else if (menu == 2) { printCart(cart); } else if (menu == 3) { const Totals totals = cart.calculate(); std::cout << "Charge: " << totals.grandTotal() << "\n"; cart.clear(); std::cout << "Cart cleared.\n"; } else if (menu == 4) { cart.toggleVip(); std::cout << "VIP is " << (cart.isVip() ? "ON" : "OFF") << ".\n"; } else { std::cout << "Unknown menu.\n"; } } }
|
개비스콘 비포 & 애프터 ㅇㅅㅇ
반영 완료