Deakin University
Object Oriented Development
OnTrack Submission
Validating Accounts
Submitted By:
Le viet quoc Ngo Tutor:
s222301733 Alain Hennebelle
2023/08/28 12:37
Outcome Weight
Evaluate Code
Principles
Build Programs
Design
Justify
thanks
August 28, 2023
Produced by Doubtfire
File 1 of 2 BankSystem.cs
1 using System;
2
3 public class Account
4 {
5 private decimal _balance;
6
7 public Account(decimal initialBalance)
8 {
9 _balance = initialBalance;
10 }
11
12 public bool Deposit(decimal amount)
13 {
14 if (amount > 0)
15 {
16 _balance += amount;
17 return true; // Deposit successful
18 }
19 else
20 {
21 return false; // Deposit failed
22 }
23 }
24
25 public bool Withdraw(decimal amount)
26 {
27 if (amount > 0 && _balance >= amount)
28 {
29 _balance -= amount;
30 return true; // Withdrawal successful
31 }
32 else
33 {
34 return false; // Withdrawal failed
35 }
36 }
37
38 public void Print()
39 {
40 Console.WriteLine($"Account Balance: {_balance:C}");
41 }
42 }
43
44 public enum MenuOption
45 {
46 Withdraw = 1,
47 Deposit,
48 Print,
49 Quit
50 }
51
52 public class BankSystem
53 {
Page 1 of 6
File 1 of 2 BankSystem.cs
54 public static MenuOption ReadUserOption()
55 {
56 MenuOption choice;
57 do
58 {
59 Console.WriteLine("Choose an option:");
60 Console.WriteLine("1. Withdraw");
61 Console.WriteLine("2. Deposit");
62 Console.WriteLine("3. Print");
63 Console.WriteLine("4. Quit");
64
65 int userInput;
66 if (int.TryParse(Console.ReadLine(), out userInput) &&
,→ Enum.IsDefined(typeof(MenuOption), userInput))
67 {
68 choice = (MenuOption)userInput;
69 break;
70 }
71 else
72 {
73 Console.WriteLine("Invalid option. Please choose a valid option
,→ (1-4).");
74 }
75 } while (true);
76
77 return choice;
78 }
79
80 public static void DoDeposit(Account account)
81 {
82 Console.Write("Enter the amount to deposit: ");
83 decimal amount = decimal.Parse(Console.ReadLine());
84
85 if (account.Deposit(amount))
86 {
87 Console.WriteLine("Deposit successful.");
88 }
89 else
90 {
91 Console.WriteLine("Invalid amount. Deposit failed.");
92 }
93 }
94
95 public static void DoWithdraw(Account account)
96 {
97 Console.Write("Enter the amount to withdraw: ");
98 decimal amount = decimal.Parse(Console.ReadLine());
99
100 if (account.Withdraw(amount))
101 {
102 Console.WriteLine("Withdrawal successful.");
103 }
104 else
Page 2 of 6
File 1 of 2 BankSystem.cs
105 {
106 Console.WriteLine("Invalid amount or insufficient balance. Withdrawal
,→ failed.");
107 }
108 }
109
110 public static void DoPrint(Account account)
111 {
112 account.Print();
113 }
114
115 public static void Main(string[] args)
116 {
117 Console.WriteLine("Welcome to the Banking System!");
118 Console.Write("Enter the initial balance: ");
119 decimal initialBalance = decimal.Parse(Console.ReadLine());
120
121 Account userAccount = new Account(initialBalance);
122
123 MenuOption choice;
124
125 do
126 {
127 choice = ReadUserOption();
128
129 switch (choice)
130 {
131 case MenuOption.Withdraw:
132 DoWithdraw(userAccount);
133 break;
134 case MenuOption.Deposit:
135 DoDeposit(userAccount);
136 break;
137 case MenuOption.Print:
138 DoPrint(userAccount);
139 break;
140 case MenuOption.Quit:
141 Console.WriteLine("Thank you for using the Banking System!");
142 break;
143 }
144 } while (choice != MenuOption.Quit);
145 }
146 }
Page 3 of 6
File 2 of 2 Account.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 banksystem
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 {
14 public static MenuOption ReadUserOption()
15 {
16 MenuOption choice;
17 do
18 {
19 Console.WriteLine("Choose an option:");
20 Console.WriteLine("1. Withdraw");
21 Console.WriteLine("2. Deposit");
22 Console.WriteLine("3. Print");
23 Console.WriteLine("4. Quit");
24
25 int userInput;
26 if (int.TryParse(Console.ReadLine(), out userInput) &&
,→ Enum.IsDefined(typeof(MenuOption), userInput))
27 {
28 choice = (MenuOption)userInput;
29 break;
30 }
31 else
32 {
33 Console.WriteLine("Invalid option. Please choose a valid
,→ option (1-4).");
34 }
35 } while (true);
36
37 return choice;
38 }
39
40 public static void DoDeposit(Account account)
41 {
42 Console.Write("Enter the amount to deposit: ");
43 decimal amount = decimal.Parse(Console.ReadLine());
44
45 if (account.Deposit(amount))
46 {
47 Console.WriteLine("Deposit successful.");
48 }
49 else
50 {
51 Console.WriteLine("Invalid amount. Deposit failed.");
Page 4 of 6
File 2 of 2 Account.cs
52 }
53 }
54
55 public static void DoWithdraw(Account account)
56 {
57 Console.Write("Enter the amount to withdraw: ");
58 decimal amount = decimal.Parse(Console.ReadLine());
59
60 if (account.Withdraw(amount))
61 {
62 Console.WriteLine("Withdrawal successful.");
63 }
64 else
65 {
66 Console.WriteLine("Invalid amount or insufficient balance.
,→ Withdrawal failed.");
67 }
68 }
69
70 public static void DoPrint(Account account)
71 {
72 account.Print();
73 }
74
75 public static void Main(string[] args)
76 {
77 Console.WriteLine("Welcome to the Banking System!");
78 Console.Write("Enter the initial balance: ");
79 decimal initialBalance = decimal.Parse(Console.ReadLine());
80
81 Account userAccount = new Account(initialBalance);
82
83 MenuOption choice;
84
85 do
86 {
87 choice = ReadUserOption();
88
89 switch (choice)
90 {
91 case MenuOption.Withdraw:
92 DoWithdraw(userAccount);
93 break;
94 case MenuOption.Deposit:
95 DoDeposit(userAccount);
96 break;
97 case MenuOption.Print:
98 DoPrint(userAccount);
99 break;
100 case MenuOption.Quit:
101 Console.WriteLine("Thank you for using the Banking
,→ System!");
102 break;
Page 5 of 6
File 2 of 2 Account.cs
103 }
104 } while (choice != MenuOption.Quit);
105 }
106 }
107 }
108 }
109 }
Page 6 of 6