Analiza_Obiektowa/VendingMachine/Automat.cs

121 lines
3.0 KiB
C#
Raw Normal View History

2020-10-27 23:04:36 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VendingMachine
{
class Automat
{
Dictionary<string, int> zawartosc;
float stan;
float stanKarty;
bool isCard;
public Automat(Produkt[] a,Portfel b)
{
zawartosc = new Dictionary<string, int>();
foreach(Produkt i in a)
{
zawartosc.Add(i.getNazwa(), 10);
}
stan = 0;
stanKarty = b.getBalance();
isCard = false;
}
public void addDictValue(string key, int value)
{
zawartosc.Add(key, value);
}
public void setDictValue(string key, int value)
{
zawartosc[key] = value;
}
public Dictionary<string,int> getDict()
{
return zawartosc;
}
public void setBalance(float a)
{
stan = a;
}
public float buyProduct(Produkt i,Portfel b)
{
if(isCard == true)
{
isCard = false;
if (zawartosc[i.getNazwa()] <= 0)
{
return -2;
}
if (i.getCena() < b.getBalance())
{
zawartosc[i.getNazwa()] = zawartosc[i.getNazwa()] - 1;
float rest = stanKarty - i.getCena();
stanKarty = stanKarty - i.getCena();
b.setBalance(stanKarty);
stan = 0;
return rest;
}
else
{
return -1;
}
}
else
{
if (zawartosc[i.getNazwa()] <= 0)
{
return -2;
}
if (i.getCena() < stan)
{
zawartosc[i.getNazwa()] = zawartosc[i.getNazwa()] - 1;
float rest = stan - i.getCena();
stan = 0;
return rest;
}
else
{
return -1;
}
}
}
public void restock()
{
try
{
foreach (KeyValuePair<string, int> i in zawartosc)
{
zawartosc[i.Key] = 10;
}
}
catch (InvalidOperationException)
{
}
}
public float getBalance()
{
return stan;
}
public void setCardBalance(float a)
{
stanKarty = a;
}
public float getCardBalance()
{
return stanKarty;
}
public bool getCard()
{
return isCard;
}
public void setCard(bool a)
{
isCard = a;
}
}
}