-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson_42.cs
158 lines (136 loc) · 4.09 KB
/
lesson_42.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
class Account
{
public int Id { get; private set; } // номер счета
public int Sum { get; set; }
public Account(int _id)
{
Id = _id;
}
}
class Transaction<T> where T : Account
{
public T FromAccount { get; set; } // с какого счета перевод
public T ToAccount { get; set; } // на какой счет перевод
public int Sum { get; set; } // сумма перевода
public void Execute()
{
if (FromAccount.Sum > Sum)
{
FromAccount.Sum -= Sum;
ToAccount.Sum += Sum;
Console.WriteLine($"Счет {FromAccount.Id}: {FromAccount.Sum}$ \nСчет {ToAccount.Id}: {ToAccount.Sum}$");
}
else
{
Console.WriteLine($"Недостаточно денег на счете {FromAccount.Id}");
}
}
}
class Program
{
static void Main(string[] args)
{
Account acc1 = new Account(1857) { Sum = 4500 };
Account acc2 = new Account(3453) { Sum = 5000 };
Transaction<Account> transaction1 = new Transaction<Account>
{
FromAccount = acc1,
ToAccount = acc2,
Sum = 6900
};
transaction1.Execute();
Console.ReadLine();
}
}
class Program
{
private static void Main(string[] args)
{
Account<int> acc1 = new Account<int>(1857) { Sum = 4500 };
Account<int> acc2 = new Account<int>(3453) { Sum = 5000 };
Transaction<Account<int>> transaction1 = new Transaction<Account<int>>
{
FromAccount = acc1,
ToAccount = acc2,
Sum = 6900
};
transaction1.Execute();
Console.Read();
}
}
class Account<T>
{
public T Id { get; private set; } // номер счета
public int Sum { get; set; }
public Account(T _id)
{
Id = _id;
}
}
class Transaction<T> where T : Account<int>
{
public T FromAccount { get; set; } // с какого счета перевод
public T ToAccount { get; set; } // на какой счет перевод
public int Sum { get; set; } // сумма перевода
public void Execute()
{
if (FromAccount.Sum > Sum)
{
FromAccount.Sum -= Sum;
ToAccount.Sum += Sum;
Console.WriteLine($"Счет {FromAccount.Id}: {FromAccount.Sum}$ \nСчет {ToAccount.Id}: {ToAccount.Sum}$");
}
else
{
Console.WriteLine($"Недостаточно денег на счете {FromAccount.Id}");
}
}
}
Account<string> acc1 = new Account<string>("34") { Sum = 4500 };
Account<string> acc2 = new Account<string>("45") { Sum = 5000 };
// так нельзя написать, так как Bank должен быть типизирован классом Account<int> или его наследником
Transaction<Account<string>> transaction1 = new Transaction<Account<string>>
{
FromAccount = acc1,
ToAccount = acc2,
Sum = 900
};
class Account<T> where T : struct
{ }
class Transaction<T> where T : class
{ }
А также можно задать с помощью слова new в качестве ограничения класс или структуру, которые имеют общедоступный конструктор без параметров:
class Transaction<T> where T : new()
{ }
interface IAccount
{
int CurrentSum { get; set; }
}
class Person
{
public string Name { get; set; }
}
class Transaction<T> where T : Person, IAccount, new()
{
}
class Transaction<U, V>
where U : Account<int>
where V : struct
{
}
private static void Main(string[] args)
{
Account<int> acc1 = new Account<int>(1857) { Sum = 4500 };
Account<int> acc2 = new Account<int>(3453) { Sum = 5000 };
Transact<Account<int>>(acc1, acc2, 900);
Console.Read();
}
public static void Transact<T>(T acc1, T acc2, int sum) where T : Account<int>
{
if (acc1.Sum > sum)
{
acc1.Sum -= sum;
acc2.Sum += sum;
}
Console.WriteLine($"acc1: {acc1.Sum} acc2: {acc2.Sum}");
}