Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Classes
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Administrator administrator = new Administrator();
- Seller seller = new Seller();
- Customer customer = new Customer();
- administrator.Shop(seller, customer);
- }
- }
- class Administrator
- {
- public void Shop(Seller seller, Customer customer)
- {
- const int CommandExit = 0;
- bool isRunning = true;
- while (isRunning)
- {
- Console.Clear();
- Console.Write("Sellers wallet: ");
- seller.ShowMoney();
- int rightCursorPosition = 0;
- Console.WriteLine("Sellers cart: ");
- seller.ShowCart(ConsoleColor.Red, rightCursorPosition);
- int leftCursorPosition = 25;
- Console.SetCursorPosition(leftCursorPosition, 0);
- Console.Write("Customers wallet: ");
- customer.ShowMoney();
- Console.SetCursorPosition(leftCursorPosition, 1);
- Console.WriteLine("Customers cart: ");
- customer.ShowCart(ConsoleColor.Green, leftCursorPosition);
- Console.SetCursorPosition(rightCursorPosition, 15);
- Console.Write("Enter a number to buy or 0 to exit\n" +
- "Enter the item number you want to buy: ");
- int.TryParse(Console.ReadLine(), out int userInput);
- if (userInput == CommandExit)
- {
- isRunning = false;
- }
- else if (0 < userInput && userInput <= seller.Count)
- {
- if (customer.BuyItem(seller.GetItem(userInput - 1)))
- {
- seller.SellItem(userInput - 1);
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("not enough money!");
- Console.ResetColor();
- Console.ReadKey();
- }
- }
- }
- }
- }
- class Person
- {
- protected List<Product> Cart = new List<Product>();
- protected int Money;
- protected Random random = new Random();
- public void ShowCart(ConsoleColor consoleColor, int leftCursorPosition)
- {
- int topCursorPosition = 2;
- int number = 1;
- foreach (Product product in Cart)
- {
- Console.ForegroundColor = consoleColor;
- Console.SetCursorPosition(leftCursorPosition, topCursorPosition++);
- Console.WriteLine(number++ + " - " + product.Name + " - price " + product.Price);
- Console.ResetColor();
- }
- }
- public void ShowMoney()
- {
- Console.WriteLine(Money);
- }
- }
- class Seller : Person
- {
- public Seller()
- {
- FillCart();
- Money = 0;
- }
- private void FillCart()
- {
- int productCount = 10;
- int minPrice = 10;
- int maxPrice = 20;
- for (int i = 0; i < productCount; i++)
- {
- Cart.Add(new Product("Item " + (i + 1), random.Next(minPrice, maxPrice)));
- }
- }
- public int Count => Cart.Count;
- public Product GetItem(int itemNumber)
- {
- Product item = Cart[itemNumber];
- return item;
- }
- public void SellItem(int itemNumber)
- {
- Money += Cart[itemNumber].Price;
- Cart.RemoveAt(itemNumber);
- }
- }
- class Customer : Person
- {
- public Customer()
- {
- FillWallet();
- }
- private void FillWallet()
- {
- int maxWalletValue = 200;
- Money = random.Next(maxWalletValue);
- }
- public bool BuyItem(Product item)
- {
- if (Money >= item.Price)
- {
- Cart.Add(new Product(item.Name, item.Price));
- Money -= item.Price;
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- class Product
- {
- public Product(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement