└── Assignment5
├── App.config
├── Properties
└── AssemblyInfo.cs
├── Assignment5.csproj
└── Program.cs
/Assignment5/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Assignment5/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Assignment5")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Assignment5")]
13 | [assembly: AssemblyCopyright("Copyright © 2017")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("9f87bdd7-6894-42a7-baaa-8e2d32d1604b")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Assignment5/Assignment5.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {9F87BDD7-6894-42A7-BAAA-8E2D32D1604B}
8 | Exe
9 | Assignment5
10 | Assignment5
11 | v4.6.1
12 | 512
13 | true
14 |
15 |
16 | AnyCPU
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | AnyCPU
27 | pdbonly
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/Assignment5/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Assignment5
8 | {
9 | class Program
10 | {
11 | static bool isCorrect;
12 | static void Main(string[] args)
13 | {
14 | int choice = -1;
15 | do
16 | {
17 | Console.WriteLine("WELCOME TO ASSIGNMENT 5");
18 | String playContinue = "y";
19 | print_menu();
20 | Console.Write("Please choice your option: ");
21 | while (!(Int32.TryParse(Console.ReadLine(), out choice)))
22 | {
23 | Console.Write("Bad input. Try-again: ");
24 | }
25 |
26 | switch (choice)
27 | {
28 | case 1: // Hangman game
29 | while (playContinue.Equals("y"))
30 | {
31 | String word;
32 | Console.Write("Enter the hangman word: ");
33 | word = Console.ReadLine();
34 | String hiddenWord = getHiddenWord(word);
35 | int count = 0;
36 | while (!word.Equals(hiddenWord))
37 | {
38 | Console.Write("(Guess) Enter a letter in word " + hiddenWord + " > ");
39 | char ch = Console.ReadKey().KeyChar;
40 | Console.WriteLine();
41 |
42 | if (!IsAlreadyInWord(hiddenWord, ch))
43 | {
44 |
45 | hiddenWord = getGuess(word, hiddenWord, ch);
46 |
47 | if (!isCorrect)
48 | {
49 | Console.WriteLine(ch + " is not in the word.");
50 | count++;
51 | }
52 |
53 | }
54 | else
55 | {
56 | Console.WriteLine(ch + " is already in word.");
57 | }
58 |
59 | }
60 | Console.WriteLine("The word is '" + hiddenWord + "'. You missed " + count + " time(s).");
61 | Console.Write("Do you want to guess another word? Enter y or n>: ");
62 | playContinue = Console.ReadLine();
63 | }
64 | Console.WriteLine("Good-bye and thanks for playing");
65 | break;
66 | case 2: //Baby name popularity ranking
67 | while (playContinue.Equals("y"))
68 | {
69 | string filePath;
70 | int year;
71 | string gender, name;
72 | Console.Write("Enter the directory path of the data files: ");
73 | filePath = Console.ReadLine();
74 | Console.Write("Enter the year: ");
75 | while (!(Int32.TryParse(Console.ReadLine(), out year)))
76 | {
77 | Console.Write("Bad input. Try-again: ");
78 | }
79 | Console.Write("Enter the gender (M/F): ");
80 | while (true)
81 | {
82 | gender = Console.ReadLine();
83 | if (gender.Equals("M") || gender.Equals("F"))
84 | break;
85 | else
86 | {
87 | Console.Write("Bad input. Try-again: ");
88 | }
89 | }
90 | Console.Write("Enter the name: ");
91 | name = Console.ReadLine();
92 |
93 | //Read in file and lookup for name
94 | string filename = filePath + "\\yob" + year.ToString() + ".txt";
95 | // Read each line of the file into a string array. Each element
96 | // of the array is one line of the file.
97 | string[] lines = System.IO.File.ReadAllLines(filename);
98 | int ranked = 0;
99 | bool found = false;
100 | foreach (string line in lines)
101 | {
102 | string[] atti = line.Split(',');
103 | //Console.WriteLine(line);
104 | ranked++;
105 | if (atti[0].Equals(name) && atti[1].Equals(gender))
106 | {
107 | Console.WriteLine(name + " is ranked #" + ranked.ToString() + " in year " + year.ToString());
108 | found = true;
109 | }
110 | }
111 | // Not found this name in the database
112 | if (found == false)
113 | {
114 | Console.WriteLine("The name " + name + " is no ranked in year " + year.ToString());
115 | }
116 | Console.Write("Do you want to continue? Enter y or n>: ");
117 | playContinue = Console.ReadLine();
118 |
119 | }
120 | break;
121 | case 3: // Ranking Summary
122 | while (playContinue.Equals("y"))
123 | {
124 | string filePath;
125 | int year, ranking;
126 | Console.Write("Enter the directory path of the data files: ");
127 | filePath = Console.ReadLine();
128 | Console.Write("Enter the year: ");
129 | while (!(Int32.TryParse(Console.ReadLine(), out year)))
130 | {
131 | Console.Write("Bad input. Try-again: ");
132 | }
133 | Console.Write("Enter the number of rankings: ");
134 | while (!(Int32.TryParse(Console.ReadLine(), out ranking)))
135 | {
136 | Console.Write("Bad input. Try-again: ");
137 | }
138 | //Read in file and lookup for name
139 | string filename = filePath + "\\yob" + year.ToString() + ".txt";
140 | // Read each line of the file into a string array. Each element
141 | // of the array is one line of the file.
142 | string[] lines = System.IO.File.ReadAllLines(filename);
143 | bool found = false;
144 | string[] TopMaleBaby = new string[ranking];
145 | string[] TopFemaleBaby = new string[ranking];
146 | int mraking, fraking;
147 | mraking = fraking = 0;
148 | foreach (string line in lines)
149 | {
150 | string[] atti = line.Split(',');
151 | if (atti[1].Equals("M") && mraking < ranking)
152 | {
153 | TopMaleBaby[mraking] = atti[0];
154 | mraking++;
155 | found = true;
156 | }
157 | if (atti[1].Equals("F") && fraking < ranking)
158 | {
159 | TopFemaleBaby[fraking] = atti[0];
160 | fraking++;
161 | found = true;
162 | }
163 | }
164 | // Not found this name in the database
165 | if (found == false)
166 | {
167 | Console.WriteLine("No Female or Male exist in " + year.ToString());
168 | }
169 | else
170 | {
171 | Console.WriteLine("Top " + ranking.ToString() + " Baby Names of " + year.ToString());
172 | Console.WriteLine("--------------------------------------------------------------------");
173 | Console.WriteLine("Rank\t\tMale name\t\tFemale name");
174 | Console.WriteLine("--------------------------------------------------------------------");
175 | for (int i = 0; i < ranking; ++i)
176 | {
177 | Console.Write(" " + (i + 1).ToString() + "\t\t");
178 |
179 | if (i < mraking)
180 | Console.Write(TopMaleBaby[i] + "\t\t");
181 | else
182 | Console.Write("\t\t\t");
183 | if (i < fraking)
184 | Console.Write(TopFemaleBaby[i]);
185 | Console.WriteLine();
186 | }
187 | }
188 | Console.Write("Do you want to continue? Enter y or n>: ");
189 | playContinue = Console.ReadLine();
190 | }
191 | break;
192 | case 4: // BankAccount class
193 | BankAccount bankAccount;
194 | bankAccount = new BankAccount();
195 | int menuChoice = 0;
196 | bool bValid = false;
197 | Console.Write("\nNew Account Information");
198 | Console.Write("\n---------------------------");
199 | Console.Write("\nEnter account id: ");
200 | int accountID;
201 | while (!(Int32.TryParse(Console.ReadLine(), out accountID)))
202 | {
203 | Console.Write("Invalid Account ID. Try-again: ");
204 | }
205 | bankAccount.SetID(accountID);
206 | //GET BALANCE
207 | double value = 0;
208 | bankAccount.SetBalanceAsk();
209 |
210 | do
211 | {
212 | do
213 | {
214 | try
215 | {
216 | Console.WriteLine("Account Menu");
217 | Console.WriteLine("----------------");
218 | Console.WriteLine("1. Set Annual Interest Rate");
219 | Console.WriteLine("2. Withdraw");
220 | Console.WriteLine("3. Deposit");
221 | Console.WriteLine("4. Print Account Information");
222 | Console.WriteLine("5. Exit");
223 | Console.Write("Enter a choice: ");
224 |
225 | while (!(Int32.TryParse(Console.ReadLine(), out menuChoice)))
226 | {
227 | Console.Write("Bad input. Try-again: ");
228 | }
229 | bValid = true;
230 | }
231 | catch (Exception l)
232 | {
233 | bValid = false;
234 | Console.WriteLine("Invalid data, please input your menu choice again.");
235 | }
236 | } while (bValid == false);
237 |
238 | switch (menuChoice)
239 | {
240 | case 1: //Annual Interest Rate
241 | {
242 | bankAccount.SetAnnualInterestRate();
243 | bankAccount.GetAnnualInterestRate();
244 | break;
245 | }
246 | case 2: //Withdraw
247 | {
248 | bankAccount.Withdraw();
249 | break;
250 | }
251 | case 3: //Deposit
252 | {
253 | bankAccount.Deposit();
254 | break;
255 | }
256 | case 4: //Print Account Information
257 | {
258 | double annualInterestRate = bankAccount.GetAnnualInterestRate();
259 | double monthlyInterestRate = bankAccount.CalculateMonthlyInterestRate(annualInterestRate);
260 | DateTime getDateTime = bankAccount.GetDateTime();
261 |
262 | Console.WriteLine("Account Info");
263 | Console.WriteLine("---------------");
264 | Console.WriteLine("ID: {0}", bankAccount.GetID());
265 | Console.WriteLine("Balance: ${0}", bankAccount.GetBalance());
266 | Console.WriteLine("Monthly Interest Rate: {0}", bankAccount.CalculateMonthlyInterestRate(annualInterestRate));
267 | Console.WriteLine("Monthly Interest: ${0}", bankAccount.CalculateMonthlyInterest(bankAccount.GetBalance(), monthlyInterestRate));
268 | Console.WriteLine("Date Created: {0}", getDateTime.ToShortDateString());
269 | break;
270 | }
271 | case 5:
272 | {
273 | Console.WriteLine("Thank you for using the Bank Account Manager");
274 | break;
275 | }
276 | default:
277 | {
278 | Console.Write("Please input your menu choice again: ");
279 | break;
280 | }
281 | } //closes menuChoice switch
282 | } while (menuChoice != 5);
283 | break;
284 | case 5: // ATM Machine
285 | bool exit = false;
286 | while (playContinue.Equals("y"))
287 | {
288 | Console.Write("Enter the full path of the data file: ");
289 | string pathfile = Console.ReadLine();
290 | BankAccount[] dataBankAccount = new BankAccount[10];
291 | BankAccount account = new BankAccount();
292 | string[] lines = System.IO.File.ReadAllLines(pathfile);
293 | int indexBank = 0;
294 | foreach (string line in lines)
295 | {
296 | if(indexBank >= 10)
297 | {
298 | Console.WriteLine("Data file contain more than 10 bank account. Just take 10 account data.");
299 | break;
300 | }
301 | string[] atti = line.Split(',');
302 | int id;
303 | if (!Int32.TryParse(atti[0], out id))
304 | {
305 | Console.WriteLine("Invalid data in data file. Exit to main menu.");
306 | exit = true;
307 | break;
308 | }
309 | double balance;
310 | if (!Double.TryParse(atti[1], out balance))
311 | {
312 | Console.WriteLine("Invalid data in data file. Exit to main menu.");
313 | exit = true;
314 | break;
315 | }
316 | dataBankAccount[indexBank] = new BankAccount(id, balance);
317 | indexBank++;
318 | }
319 | if (exit == true)
320 | break;
321 | bool found = false;
322 | while (found == false)
323 | {
324 | int bankid;
325 | Console.Write("Enter account id: ");
326 | while (!(Int32.TryParse(Console.ReadLine(), out bankid)))
327 | {
328 | Console.Write("Bad input. Try-again: ");
329 | }
330 |
331 | for (int i = 0; i < 10; ++i)
332 | {
333 | if (dataBankAccount[i].GetID() == bankid)
334 | {
335 | account = dataBankAccount[i];
336 | found = true;
337 | break;
338 | }
339 | }
340 | Console.WriteLine("Invalid count id. There are no accounts that account id");
341 | }
342 | int atmchoice = -1;
343 | while(atmchoice != 4)
344 | {
345 | Console.WriteLine("Main menu");
346 | Console.WriteLine("1: check baclance");
347 | Console.WriteLine("2: withdraw");
348 | Console.WriteLine("3: deposit");
349 | Console.WriteLine("4: exit");
350 | Console.Write("Enter a choice: ");
351 | while (!(Int32.TryParse(Console.ReadLine(), out atmchoice)))
352 | {
353 | Console.Write("Bad input. Try-again: ");
354 | }
355 | switch(atmchoice)
356 | {
357 | case 1: // check balace
358 | Console.WriteLine("The balance is $" + account.GetBalance().ToString());
359 | break;
360 | case 2: // withdraw
361 | account.Withdraw();
362 | break;
363 | case 3: // deposit
364 | account.Deposit();
365 | break;
366 | case 4:
367 | Console.WriteLine("Exit the Main menu for ATM Machine");
368 | exit = true;
369 | break;
370 | default:
371 | Console.WriteLine("Invalid choice. Try again.");
372 | break;
373 | }
374 | }
375 | if (exit == true)
376 | break;
377 | Console.Write("Do you want to continue? Enter y or n>: ");
378 | playContinue = Console.ReadLine();
379 | }
380 | break;
381 | case 6: // Exit
382 | Console.WriteLine("Exit the program!!!!");
383 | return;
384 | default:
385 | Console.WriteLine("Invalid choice. Please try again.");
386 | break;
387 | }
388 |
389 | } while (choice != 6);
390 | }
391 | public static void print_menu()
392 | {
393 | Console.WriteLine("======MENU=======");
394 | Console.WriteLine("1. Hangman Game");
395 | Console.WriteLine("2. Baby name popularity ranking");
396 | Console.WriteLine("3. Baby Name Ranking Summary");
397 | Console.WriteLine("4. BankAccount class");
398 | Console.WriteLine("5. ATM Machine");
399 | Console.WriteLine("6. Exit");
400 | }
401 | public static String getHiddenWord(String word)
402 | {
403 |
404 | String hidden = "";
405 | for (int i = 0; i < word.Length; i++)
406 | {
407 | hidden += "*";
408 | }
409 | return hidden;
410 | }
411 |
412 | static public String getGuess(String word, String hiddenWord, char ch)
413 | {
414 |
415 | isCorrect = false;
416 | StringBuilder s = new StringBuilder(hiddenWord);
417 | for (int i = 0; i < word.Length; i++)
418 | {
419 |
420 | if (ch == word[i] && s[i] == '*')
421 | {
422 | isCorrect = true;
423 | s.Replace(s[i], ch, i, 1);
424 | }
425 | }
426 | return s.ToString();
427 | }
428 |
429 | public static bool IsAlreadyInWord(String hiddenWord, char ch)
430 | {
431 |
432 | for (int i = 0; i < hiddenWord.Length; i++)
433 | {
434 |
435 | if (ch == hiddenWord[i])
436 | {
437 | return true;
438 | }
439 | }
440 | return false;
441 | }
442 | }
443 | class BankAccount
444 | {
445 | private int iD = 0;
446 | private double balance = 0;
447 | private double annualInterestRate = 0;
448 | private DateTime dateCreated;
449 |
450 | public BankAccount(int id = 0, double bal = 0, double annual = 0)
451 | {
452 | iD = id;
453 | balance = bal;
454 | annualInterestRate = annual;
455 | dateCreated = DateTime.Now;
456 | }
457 |
458 | public void SetID(int value)
459 | {
460 | iD = value;
461 | } //closes SetID()
462 | public int GetID()
463 | {
464 | return iD;
465 | } //closes GetID()
466 | public void SetBalanceAsk()
467 | {
468 | bool bValid = false;
469 | double value;
470 |
471 | do
472 | {
473 | Console.Write("Enter initial account balance: ");
474 | while (!(Double.TryParse(Console.ReadLine(), out value)))
475 | {
476 | Console.Write("Bad input. Try-again: ");
477 | }
478 | if (value > 0)
479 | {
480 | bValid = true;
481 | balance = value;
482 | }
483 | else
484 | {
485 | bValid = false;
486 | Console.WriteLine("Invalid balance - balance must be greater than zero. ");
487 | }
488 | } while (bValid == false);
489 |
490 | } //closes SetBalanceAsk()
491 | public void SetBalance(double ba)
492 | {
493 | balance = ba;
494 | }
495 | public double GetBalance()
496 | {
497 | return balance;
498 | } //closes GetBalance()
499 | public void SetAnnualInterestRate()
500 | {
501 | bool bValid = false;
502 | double value = 0;
503 | do
504 | {
505 | Console.Write("Enter annual interest rate: ");
506 | while (!(Double.TryParse(Console.ReadLine(), out value)))
507 | {
508 | Console.Write("Bad input. Try-again: ");
509 | }
510 | if (value > 0)
511 | {
512 | bValid = true;
513 | annualInterestRate = value / 100;
514 | }
515 | else
516 | {
517 | bValid = false;
518 | Console.WriteLine("Invalid annual interest rate - rate must be greater than 0. ");
519 | }
520 | } while (bValid == false);
521 | } //closes SetAnnualInterestRate()
522 | public double GetAnnualInterestRate()
523 | {
524 | return annualInterestRate;
525 | } //closes GetAnnualInterestRate()
526 | public DateTime GetDateTime()
527 | {
528 | //dateCreated = DateTime.Today;
529 | return dateCreated;
530 | }
531 | public double CalculateMonthlyInterestRate(double annualInterestRate)
532 | {
533 | //Will return the monthly interest rate
534 | //Note: annualInterestRate is a percentage, for example 4.5%. You need to divide it by 100.
535 | double monthlyInterestRate = 0;
536 | monthlyInterestRate = annualInterestRate / 12;
537 |
538 | return monthlyInterestRate;
539 | } //closes CalculateMonthlyInterestRate()
540 | public double CalculateMonthlyInterest(double balance, double monthlyInterestRate)
541 | {
542 | //Will return the monthly interest amount ($$)
543 | double monthlyInterest = 0;
544 | monthlyInterest = balance * monthlyInterestRate;
545 |
546 | return monthlyInterest;
547 | } //closes CalculateMonthlyInterest()
548 | public double Withdraw()
549 | {
550 | Console.Write("Enter an amount to withdraw: ");
551 | double withdraw;
552 | while (!(Double.TryParse(Console.ReadLine(), out withdraw)))
553 | {
554 | Console.Write("Bad input. Try-again: ");
555 | }
556 | if (withdraw > balance)
557 | {
558 | Console.WriteLine("Invalid withdraw amount");
559 | }
560 | else
561 | {
562 | balance = balance - withdraw;
563 | }
564 | return balance;
565 | } //closes Withdraw()
566 | public double Deposit()
567 | {
568 | Console.Write("Enter an amount to deposit: ");
569 | double deposit;
570 | while (!(Double.TryParse(Console.ReadLine(), out deposit)))
571 | {
572 | Console.Write("Bad input. Try-again: ");
573 | }
574 | balance = balance + deposit;
575 | return balance;
576 | } //closes Deposit()
577 |
578 | }
579 | }
580 |
--------------------------------------------------------------------------------