Programlama dersi alıyorum. Elimde bir kod var. Bu kodu SIMPLICITY ,ORTHOGONALITY ,DATA…
- User
Programlama dersi alıyorum. Elimde bir kod var. Bu kodu SIMPLICITY ,ORTHOGONALITY ,DATA TYPES,EXPRESSIVITY,TYPE CHECKING ,EXCEPTION HANDLING , ALIASING SUPPORT FOR ABSTRACTION,SYNTAX DESIGN,DISTRUBUTION OF TASKS konularında değerlendirmeni istiyorum. Ve gerekli yerleri belirtip daha doğru olanı yazar mısın? kod: #include #include #include #include // added header
using namespace std;
// Struct for music items struct MusicItem { string name; long price; };
// Function to display menu and get user choice int menu() { int choice; cout << "1. Add item to cart\n"; cout << "2. Remove item from cart\n"; cout << "3. Show Cart\n"; cout << "4. Exit\n"; cout << "Enter your choice: "; cin >> choice; return choice; } // Function to show items in the cart void showCart(const vector& cart, long totalPrice) { if (cart.empty()) { cout << "Your cart is empty." << endl; } else { cout << "Items in your cart:" << endl; for (const MusicItem& item : cart) { cout << " " << item.name << " - " << item.price << " "<< endl; } cout << "Total price: " << totalPrice << "" << endl<<"****"<<endl; } }
string toLowerCase(const string& s) { string lower; transform(s.begin(), s.end(), back_inserter(lower), ::tolower); return lower; }
int main() { MusicItem inventory[] = { // initialize inventory with some items {"Guitar", 960}, {"Drum", 1455}, {"Bass", 840}, {"Microphone", 110}, {"Amplifier", 333}, {"Piano", 880}, {"String", 20}, };
cout << "Welcome to the music store!\n"; //prices of the instruments for(int i = 0; i < 7; i++){ cout << inventory[i].name << " $" << inventory[i].price << endl; } cout<<"******"<<endl; int numItems = sizeof(inventory) / sizeof(MusicItem); vector<MusicItem> cart; int choice; string itemName; long totalPrice = 0; do { choice = menu(); switch (choice) { case 1: // Add item to cart cout << "Enter item name: "; cin >> itemName; itemName = toLowerCase(itemName); // convert item name to lowercase for (int i = 0; i < numItems; i++) { if (toLowerCase(inventory[i].name) == itemName) { // compare lowercase item name with lowercase inventory item name cart.push_back(inventory[i]); totalPrice += inventory[i].price; cout << "Item added to cart! Total price: " << totalPrice << " $"<< endl<<"-----------------"<<endl; break; } if (i == numItems - 1) { cout << "Item not found in inventory.\n"; } } break; case 2: // Remove item from cart cout << "Enter item name: "; cin >> itemName; itemName = toLowerCase(itemName); // convert item name to lowercase for (int i = 0; i < cart.size(); i++) { if (toLowerCase(cart[i].name) == itemName) { // compare lowercase item name with lowercase cart item name totalPrice -= cart[i].price; cart.erase(cart.begin() + i); cout << "Item removed from cart! Total price: " << totalPrice << endl<<"--------------------"<<endl; break; } if (i == cart.size() - 1) { cout << "Item not found in cart.\n"; } } break; case 3: // Show Cart showCart(cart, totalPrice); break; case 4: // Exit cout << "Total price of items in cart: " << totalPrice << " $"<< endl; cout << "Exiting program...\n"; break; default: cout << "Invalid choice. Please try again.\n"; break; } } while (choice != 4); return 0; - Assistant