Analiza_Obiektowa/Program.cs

137 lines
5.0 KiB
C#
Raw Permalink Normal View History

2020-10-27 18:03:13 +01:00
using System;
using System.Collections.Generic;
namespace Automat_prototype
{
class Program
{
public static List<Product> LoadProducts()
{
List<Product> ProductList = new List<Product>();
ProductList.Add(new Product("Pepsi", 3.00, "A1", 8));
ProductList.Add(new Product("Lipton", 2.50, "A2", 5));
ProductList.Add(new Product("Snickers", 1.00, "B3", 4));
ProductList.Add(new Product("Bounty", 2.00, "C5", 7));
return ProductList;
}
static void WorkLoop(List<Product> ProductList)
{
while (true) {
Console.WriteLine("Dostępne produkty:");
for (int i = 0; i < ProductList.Count; i++)
{
ProductList[i].ShowInfo();
}
Console.WriteLine("Wybierz produkt:");
string selected = Console.ReadLine();
for (int i = 0; i < ProductList.Count; i++)
{
if (String.Compare(selected, ProductList[i].Position) == 0)
{
if (ProductList[i].Amount == 0)
{
Console.WriteLine("Brak wybranego produktu");
break;
}
else
{
Console.WriteLine("Wrzuć pieniądze");
double money = 0;
try
{
money = double.Parse(Console.ReadLine());
}
catch (FormatException f) {
Console.WriteLine(f.Message);
}
if (money < ProductList[i].Price)
{
Console.WriteLine("Wrzuciłeś za mało, następuje zwrot pieniędzy");
break;
}
else
{
double rest = money - ProductList[i].Price;
rest = Math.Round(rest, 2);
Console.WriteLine("Wrzucono " + money + "zł, reszta wynosi " + rest);
if(rest > 0)
{
RestCounter(rest);
}
}
Console.WriteLine("Już podaję");
ProductList[i].Amount = ProductList[i].Amount - 1;
break;
}
}
if (i == ProductList.Count - 1)
{
Console.WriteLine("Brak wybranego produktu");
break;
}
}
}
}
static int RestCounter(double rest)
{
int[] coins = new int[6] {0, 0, 0, 0, 0, 0};
while (rest != 0)
{
if(rest >= 5)
{
rest = Math.Round(rest - 5, 2);
coins[0] = coins[0] + 1;
}
else if (rest >= 2)
{
rest = Math.Round(rest - 2, 2);
coins[1] = coins[1] + 1;
}
else if (rest >= 1)
{
rest = Math.Round(rest - 1, 2);
coins[2] = coins[2] + 1;
}
else if (rest >= 0.5)
{
rest = Math.Round(rest - 0.5, 2);
coins[3] = coins[3] + 1;
}
else if (rest >= 0.2)
{
rest = Math.Round(rest - 0.2, 2);
coins[4] = coins[4] + 1;
}
else if (rest >= 0.1)
{
rest = Math.Round(rest - 0.1, 2);
coins[5] = coins[5] + 1;
}
else
{
Console.WriteLine("Nastąpił błąd przy wydawaniu reszty, proszę skontaktować się z pomocą techniczną");
return -1;
}
}
Console.WriteLine("Wydano " + coins[0] + " monet 5zł, " + coins[1] + " monet 2zł, " + coins[2] + " monet 1zł, " + coins[3] + " monet 50gr, " + coins[4] + " monet 20gr, " + coins[5] + " monet 10gr");
return 0;
}
static void Main(string[] args)
{
List<Product> ProductList = new List<Product>();
ProductList = LoadProducts();
WorkLoop(ProductList);
}
}
}