· 5 years ago · Nov 17, 2020, 12:08 PM
1using System;
2using System.Linq;
3using System.Collections.Generic;
4using System.Web.Script.Serialization;
5using System.IO;
6using System.Xml.Serialization;
7using System.Text;
8using System.Data.SqlClient;
9
10namespace KIACS
11{
12 class Program
13 {
14 static void Main(string[] args)
15 {
16 Lab8_Using_List lab8 = new Lab8_Using_List();
17 Lab8_Using_Database bank = new Lab8_Using_Database();
18 //Lab8_Using_List.run();
19 Lab8_Using_Database.run();
20 }
21 }
22
23 class Lab8_Using_List
24 {
25
26 List<Account> AccountsList = new List<Account>();
27 public void run()
28 {
29
30 CreateAccount(20001, "David", 1000.50);
31 CreateAccount(20002, "Samme", 2000.70);
32 CreateAccount(20003, "Flick", 3000.99);
33
34 GetDetails(20001);
35 GetDetails(20002);
36 GetDetails(20003);
37
38
39 Deposit(20001, 1000);
40 Withdraw(20001, 3000);
41 GetDetails(20001);
42 AddInterest(20001);
43 GetDetails(20001);
44 GetDetails(20002);
45 Withdraw(20001, 500);
46 GetDetails(20001);
47
48 }
49
50 public void Deposit(int id, double amount)
51 {
52 var account = AccountsList.Find(item => item.id == id);
53 account.balance = account.balance + amount;
54 Console.WriteLine($"Account : {account.id} | Balance: {account.balance} ({amount} Deposited)");
55 }
56 public void Withdraw(int id, double amount)
57 {
58 var account = AccountsList.Find(item => item.id == id);
59 if ((account.balance - amount) < 0)
60 {
61 Console.WriteLine($"ERROR - Account : {account.id} does not have sufficient funds for this withdrawal ({amount})");
62 }
63 else
64 {
65 account.balance = account.balance - amount;
66 Console.WriteLine($"Account : {account.id} | Balance: {account.balance} ({amount} Withdrawn)");
67 }
68 }
69
70 public void AddInterest(int id)
71 {
72 double IntrestRate = 0.025;
73 var account = AccountsList.Find(item => item.id == id);
74 double StartBallance = account.balance;
75 double Intrest = StartBallance * IntrestRate;
76 Intrest = Math.Round(Intrest, 2);
77 Console.WriteLine($"Account : {account.id} | ({Intrest} Intrest Calculated)");
78 account.Deposit(Intrest);
79 }
80
81
82 public void CreateAccount(int id, string owner, double balance)
83 {
84 var account = new Account(id, owner, balance);
85 AccountsList.Add(account);
86 }
87
88 public void GetDetails(int id)
89 {
90 var account = AccountsList.Find(item => item.id == id);
91
92 Console.WriteLine($"Account : {account.id} | Name : {account.owner} | Balance: {account.balance}");
93 }
94
95
96 class Account
97 {
98 public Account(int id, string owner, double balance)
99 {
100 this.id = id;
101 this.owner = owner;
102 this.balance = balance;
103
104 }
105
106 public int id { get; }
107 public string owner { get; }
108 public double balance { get; set; }
109
110 public void Deposit(double amount)
111 {
112 this.balance = this.balance + amount;
113 Console.WriteLine($"Account : {this.id} | Balance: {this.balance} ({amount} Deposited)");
114 }
115
116 public void Withdraw(double amount)
117
118 {
119 if ((this.balance - amount) < 0)
120 {
121 Console.WriteLine($"ERROR - Account {this.id} does not have sufficient funds for this withdrawal {amount}");
122 }
123 else
124 {
125 this.balance = this.balance - amount;
126 Console.WriteLine($"Account : {this.id} | Balance: {this.balance} ({amount} Withdrawn)");
127 }
128 }
129 }
130 }
131
132 class Lab8_Using_Database
133 {
134 string connetionString = @"Server= W10-DESKTOP\SQLEXPRESS; Database= master; Integrated Security=True;";
135 public struct Result
136 {
137 public int ID;
138 public String Owner;
139 public double Balance;
140 }
141 public void run()
142
143 {
144 SQL_REINIT(); //Drop and recreate DB
145
146 CreateAccount(0, "QueriedAccount_Struct_Init", 0); // Initilise struct Result
147 Result QueriedAccount = getDetails(0); // Initilise struct Result
148
149 RUN_TESTS();
150
151 }
152 public void RUN_TESTS()
153 {
154 // ENTER TEST DATA
155 CreateAccount(5000, "David Maull", 1000.00);
156 CreateAccount(5001, "Richy McRich", 258387.15);
157 CreateAccount(5002, "Mr No Dosh", 21.71);
158
159 // TRY TO CREATE DUPLICATE ACCOUNT ID
160 Console.WriteLine(" ## TRY TO CREATE DUPLICATE ACCOUNT ID");
161 CreateAccount(5000, "Duplicate_User", 4000.00);
162
163 // GET USER ACCOUNT DETAILS
164 Console.WriteLine(" ## GET USER ACCOUNT DETAILS");
165 Print_Account_Details(5000); // ID
166
167 // MAKE DEPOSIT AND WITHDRAWAL AND PRINT AGAIN
168 Console.WriteLine(" ## MAKE DEPOSIT AND WITHDRAWAL AND PRINT AGAIN (SHOULD GO UP 500)");
169 Deposit(5000, 1000.00); // ID and Amount to Deposit
170 Withdraw(5000, 500.00); // ID and Amount to Withdraw
171 Print_Account_Details(5000); // ID
172
173 // TRY TO GET DETAILS OF INVALID USER ID
174 Console.WriteLine(" ## TRY TO GET DETAILS OF INVALID USER ID");
175 Print_Account_Details(4000); // ID
176
177 // ADD INTREST TO ACCOUNT
178 Console.WriteLine(" ## ADD INTREST TO ACCOUNT");
179 Print_Account_Details(5001); // ID
180 AddInterest(5001); // ID
181 Print_Account_Details(5001); // ID
182
183 // MAKE WITHDRAWAL FROM ACCOUNT WITHOUT ENOUGH FUNDS
184 Console.WriteLine(" ## MAKE WITHDRAWAL FROM ACCOUNT WITHOUT ENOUGH FUNDS");
185 Print_Account_Details(5002); // ID
186 Withdraw(5002, 500.00); // ID and Amount to Withdraw
187 Print_Account_Details(5002); // ID
188 }
189 public void Print_Account_Details(int id)
190 {
191 Result QueriedAccount = getDetails(id);
192 Console.WriteLine($"ID : { QueriedAccount.ID} | Owner : { QueriedAccount.Owner} | Balance : { QueriedAccount.Balance}");
193 }
194 public void Deposit(int id, double amount)
195 {
196 Result QueriedAccount = getDetails(id);
197 double Starting_Balance = QueriedAccount.Balance;
198 double Updated_Balance = Starting_Balance + amount;
199 Update_Balance(id, Updated_Balance);
200 }
201 public void Withdraw(int id, double amount)
202 {
203 Result QueriedAccount = getDetails(id);
204 double Starting_Balance = QueriedAccount.Balance;
205 int ID = QueriedAccount.ID;
206 if ((Starting_Balance - amount) < 0)
207 {
208 Console.WriteLine($"ERROR - ID : {ID} does not have sufficient funds for this withdrawal ({amount})");
209 }
210 else
211 {
212 double Updated_Balance = Starting_Balance - amount;
213 Update_Balance(id, Updated_Balance);
214 }
215 }
216 public void AddInterest(int id)
217 {
218 double IntrestRate = 0.025;
219 Result QueriedAccount = getDetails(id);
220 double Starting_Balance = QueriedAccount.Balance;
221 int ID = QueriedAccount.ID;
222 double Intrest = Starting_Balance * IntrestRate;
223 Intrest = Math.Round(Intrest, 2);
224 Console.WriteLine($"ID : {ID} | ({Intrest} Intrest Calculated)");
225 Deposit(ID, Intrest);
226 }
227 public Result getDetails(int id)
228 {
229 double ReadBalance = 0;
230 String ReadOwner = "NO ACCOUNT WITH ID " + id +"";
231
232 SqlConnection connection;
233 connection = new SqlConnection(connetionString);
234 string built_command = "SELECT * FROM bank WHERE id = " + id + ";";
235 SqlCommand command = new SqlCommand(built_command, connection);
236 connection.Open();
237 using (SqlDataReader reader = command.ExecuteReader())
238 {
239 while (reader.Read())
240 {
241 String StringBallance = (reader["balance"].ToString());
242 ReadBalance = (Convert.ToDouble(StringBallance));
243 ReadOwner = (reader["owner"].ToString());
244
245 }
246 }
247 connection.Close();
248 var result = new Result
249 {
250 ID = id,
251 Balance = ReadBalance,
252 Owner = ReadOwner
253 };
254
255 return result;
256 }
257 public double getBalance(int id)
258 {
259 double ReadBalance = 0;
260 SqlConnection connection;
261 connection = new SqlConnection(connetionString);
262 string built_command = "SELECT * FROM bank WHERE id = " + id + ";";
263 SqlCommand command = new SqlCommand(built_command, connection);
264 connection.Open();
265 using (SqlDataReader reader = command.ExecuteReader())
266 {
267 while (reader.Read())
268 {
269 String StringBallance = (reader["balance"].ToString());
270 ReadBalance = (Convert.ToDouble(StringBallance));
271 }
272 }
273 connection.Close();
274 return ReadBalance;
275 }
276 public void Update_Balance(int id, double new_balance)
277 {
278 SqlConnection connection;
279 connection = new SqlConnection(connetionString);
280 string built_command = "UPDATE bank SET balance = " + new_balance + " WHERE id =" + id + ";";
281 SqlCommand command = new SqlCommand(built_command, connection);
282 connection.Open();
283 using (SqlDataReader reader = command.ExecuteReader())
284 connection.Close();
285 }
286 public void SQL_REINIT()
287 {
288 string drop = "DROP TABLE IF EXISTS bank";
289 string create = "CREATE TABLE bank (id int, owner varchar(255), balance double PRECISION, PRIMARY KEY (id))";
290 SqlConnection connection;
291 connection = new SqlConnection(connetionString);
292 SqlCommand command = new SqlCommand(drop, connection);
293 connection.Open();
294 using (SqlDataReader reader = command.ExecuteReader())
295 connection.Close();
296 command = new SqlCommand(create, connection);
297 connection.Open();
298 using (SqlDataReader reader = command.ExecuteReader())
299 connection.Close();
300 }
301 public void CreateAccount(int id, String owner, double balance)
302 {
303 SqlConnection connection;
304 connection = new SqlConnection(connetionString);
305 string built_command = "INSERT INTO bank(id, owner, balance) VALUES('" + id + "','" + owner + "','" + balance + "');";
306 try
307 {
308 SqlCommand command = new SqlCommand(built_command, connection);
309 connection.Open();
310 using (SqlDataReader reader = command.ExecuteReader())
311 connection.Close();
312 }
313 catch (Exception)
314 {
315 Console.WriteLine("Error - Cannot create account - " + id + " - " + owner + " - " + balance + "");
316 }
317 }
318 }
319}
320
321