-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount.cs
156 lines (150 loc) · 4.76 KB
/
Account.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
using System;
using Newtonsoft.Json;
namespace BankingApp
{
public class BankAccount
{
public double ID {get; private set;}
public string username {get; private set;}
public string name {get; private set;}
public string PIN {get; private set;}
public string email {get; private set;}
public string phone {get; private set;}
public string AccountNumber {get; private set;}
public double Balance {get; private set;}
/// <summary>
/// Constructor for the BankAccount class
/// </summary>
/// <param name="ID"></param>
/// <param name="username"></param>
/// <param name="name"></param>
/// <param name="PIN"></param>
/// <param name="email"></param>
/// <param name="phone"></param>
public BankAccount(double ID, string username, string name, string PIN, string email, string phone)
{
this.ID = ID;
this.username = username;
this.name = name;
this.PIN = PIN;
this.email = email;
this.phone = phone;
this.AccountNumber = generateAccountNumber();
this.Balance = 0;
}
/// <summary>
/// Withdraws the specified amount from the account
/// </summary>
/// <param name="amount"></param>
/// <returns></returns>
public string Withdraw(double amount)
{
var response = new Object();
if (amount > this.Balance){
response = new {
status = false,
message = "Insufficient Funds"
};
}
else if (amount < 500){
response = new {
status = false,
message = "Minimum Withdrawal Amount is 500"
};
}
else{
this.Balance -= amount;
response = new {
status = true,
message = "Withdrawal Successful",
balance = this.Balance
};
}
return JsonConvert.SerializeObject(response);
}
/// <summary>
/// Deposits the specified amount into the account
/// </summary>
/// <param name="amount"></param>
/// <returns></returns>
public string Deposit(double amount)
{
var response = new Object();
if (amount < 500)
{
response = new {
status = false,
message = "Minimum Deposit Amount is 500"
};
}
else
{
this.Balance += amount;
response = new {
status = true,
message = "Deposit Successful",
balance = this.Balance
};
}
return JsonConvert.SerializeObject(response);
}
/// <summary>
/// Generates a random account number for the account
/// </summary>
/// <returns></returns>
private string generateAccountNumber()
{
int year = DateTime.Now.Year % 100;
return $"{ID}{year}{generateRandomNumber()}";
}
/// <summary>
/// Generates a random 4 digit number
/// </summary>
/// <returns></returns>
private double generateRandomNumber()
{
Random random = new Random();
return random.Next(1000, 9999);
}
/// <summary>
/// Returns the username of the account
/// </summary>
/// <returns></returns>
public string getUserName()
{
return username;
}
/// <summary>
/// returns the email of the account
/// </summary>
/// <returns></returns>
public string getEmail()
{
return email;
}
/// <summary>
/// returns the pin of the account
/// </summary>
/// <returns></returns>
public string getPIN()
{
return PIN;
}
/// <summary>
/// returns the account number of the account
/// </summary>
/// <returns></returns>
public string getAccountNumber()
{
return AccountNumber;
}
/// <summary>
/// Returns the string representation of the account
/// </summary>
/// <returns></returns>
public override string ToString()
{
return $"\nAccountNumber: {this.AccountNumber}\nUsername: {this.username}\nName: {this.name}\nEmail: {this.email}\nPhone: {this.phone}\nBalance: {this.Balance}\n";
}
}
}