Created
March 11, 2017 19:57
-
-
Save Soulstorm50/f557d85395cc5364087dbca7758f6b39 to your computer and use it in GitHub Desktop.
c# Struct check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Описать структуру Check: https://ru.wikipedia.org/wiki/%D0%9A%D0%B0%D1%81%D1%81%D0%BE%D0%B2%D1%8B%D0%B9_%D1%87%D0%B5%D0%BA | |
//Предусмотреть функцию добавления информации о товаре (наименование, количество, цена за единицу товара, скидка) в чек, функцию распечатки чека на экране консоли. | |
using System; | |
using System.Text.RegularExpressions; | |
using System.Collections; | |
using System.Text; | |
using System.Threading.Tasks; | |
// IEnumerator - 586 | |
namespace ConsoleApplication1 | |
{ | |
public struct Product | |
{ | |
public String name { get; private set; } | |
public double count { get; private set; } | |
public Double price { get; private set; } | |
public Double discount { get; private set; } | |
public Double sum { get; private set; } | |
public Product(String name, double count, Double price, Double discount) | |
{ | |
this.name = name; | |
this.count = count; | |
this.price = price; | |
this.discount = discount; | |
sum = (price - price * (discount / 100)) * count; | |
} | |
public Product(Product pr) | |
{ | |
this.name = pr.name; | |
this.count = pr.count; | |
this.price = pr.price; | |
this.discount = pr.discount; | |
sum = (price - price * (discount / 100)) * count; | |
} | |
public void Print() | |
{ | |
Console.WriteLine("{0} {1}*{2}={4}; \ndiscount = {3}%", name, count, price, discount, sum); | |
} | |
} | |
public struct Check : IEnumerable | |
{ | |
public Product[] p; | |
public Check(Product[] pr) | |
{ | |
p = new Product[pr.Length]; | |
for (int i = 0; i < pr.Length; i++) | |
{ | |
p[i] = new Product(pr[i]); | |
} | |
} | |
public Check(Product pr) | |
{ | |
p = new Product[1]; | |
p[1] = pr; | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return (IEnumerator)GetEnumerator(); | |
} | |
private IEnumerator GetEnumerator() | |
{ | |
return new ProductEnum(p); | |
} | |
} | |
class ProductEnum : IEnumerator | |
{ | |
public Product[] _p; | |
int position = -1; | |
public ProductEnum(Product[] list) | |
{ | |
_p = list; | |
} | |
public object Current | |
{ | |
get | |
{ | |
try | |
{ | |
return _p[position]; | |
} | |
catch (IndexOutOfRangeException) | |
{ | |
throw new InvalidOperationException(); | |
} | |
} | |
} | |
public bool MoveNext() | |
{ | |
position++; | |
return (position < _p.Length); | |
} | |
public void Reset() | |
{ | |
position = -1; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Product[] p = new Product[3]; | |
p[0] = new Product("баранка", 1, 2, 0); | |
p[1] = new Product("печенька", 2, 7.55, 0); | |
p[2] = new Product("пепси 0,5л", 1, 12.20, 3); | |
Check one = new Check(p); | |
PrintCheck(one); | |
Product p2 = new Product("малина", 1.138, 37.55, 2); | |
Console.WriteLine(); | |
Console.WriteLine(); | |
AddProduct(ref one, p2); | |
PrintCheck(one); | |
} | |
static void AddProduct(ref Check ch, Product p) | |
{ | |
Array.Resize(ref ch.p, ch.p.Length + 1); | |
ch.p[ch.p.Length - 1] = new Product(p); | |
} | |
static void PrintCheck(Check check) | |
{ | |
Double sum = 0; | |
Console.WriteLine("Check"); | |
Console.WriteLine("реклама"); | |
Console.WriteLine("с логотипом"); | |
foreach (Product p in check) | |
{ | |
p.Print(); | |
sum += p.sum; | |
} | |
//for (int i = 0; i < check.p.Length; i++) | |
//{ | |
// check.p[i].Print(); | |
// sum += check.p[i].sum; | |
//} | |
Console.WriteLine("Total sum {0:0.00}", sum); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment