Create a class Product that represents a product sold in a shop. A product has a price,…
- User
Create a class Product that represents a product sold in a shop. A product has a price, amount and name.
The class should have:
A constructor public Product(string name, double priceAtStart, int amountAtStart) A method public void PrintProduct() that prints a product in the following form: Banana, price 1.1, amount 13 Test your code by creating a class with main method and add three products there:
"Logitech mouse", 70.00 EUR, 14 units "iPhone 5s", 999.99 EUR, 3 units "Epson EB-U05", 440.46 EUR, 1 units Print out information about them.
Add new behaviour to the Product class:
possibility to change quantity possibility to change price Reflect your changes in a working application. Hi, please solve this with C# code.
- Assistant
Here is the solution in C#:
using System; public class Product { private string name; private double price; private int amount; public Product(string name, double priceAtStart, int amountAtStart) { this.name = name; this.price = priceAtStart; this.amount = amountAtStart; } public void PrintProduct() { Console.WriteLine($"{name}, price {price.ToString("0.00")}, amount {amount}"); } public void ChangeQuantity(int newAmount) { this.amount = newAmount; } public void ChangePrice(double newPrice) { this.price = newPrice; } } class Program { static void Main(string[] args) { Product product1 = new Product("Logitech mouse", 70.00, 14); Product product2 = new Product("iPhone 5s", 999.99, 3); Product product3 = new Product("Epson EB-U05", 440.46, 1); product1.PrintProduct(); product2.PrintProduct(); product3.PrintProduct(); product1.ChangeQuantity(20); product2.ChangePrice(899.99); Console.WriteLine("--- Updated Products ---"); product1.PrintProduct(); product2.PrintProduct(); product3.PrintProduct(); Console.ReadLine(); } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.