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(); } }This code defines a
Productclass with a constructor that takes in the name, price, and amount of the product. It also has aPrintProductmethod that prints out the product information in the desired format.In the
Mainmethod, we create three instances of theProductclass with the given information. We then call thePrintProductmethod on each of them to display their information.After that, we demonstrate the added behavior by changing the quantity of the first product and the price of the second product using the
ChangeQuantityandChangePricemethods respectively. We then print out the updated information of all three products.The output of this code will be:
Logitech mouse, price 70.00, amount 14 iPhone 5s, price 999.99, amount 3 Epson EB-U05, price 440.46, amount 1 --- Updated Products --- Logitech mouse, price 70.00, amount 20 iPhone 5s, price 899.99, amount 3 Epson EB-U05, price 440.46, amount 1