-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bank.cs
91 lines (85 loc) · 2.93 KB
/
Bank.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
using System;
using System.Collections.Generic;
namespace CSE445Project2
{
public class Bank
{
public static List<string> cardList = new List<string>();
public static List<double> fundsList = new List<double>();
/*Validate orders by checking that the credit card has been registered and that it has sufficient funds*/
public static string validate(Int32 creditCardNumber, double amount)
{
ServiceReference1.ServiceClient serviceClient = new ServiceReference1.ServiceClient();
int index = -1;
for (int i = 0; i < cardList.Count && index == -1; i++)
{
if (creditCardNumber == Int32.Parse(serviceClient.Decrypt(cardList[i])))
{
index = i;
}
}
if (index != -1 && fundsList[index] > amount)
{
fundsList[index] -= amount;
return "valid";
}
else
{
return "not valid";
}
}
/*
* Generate card
* Add card number to list of cards and funds, starting with no funds
* Return the new card
*/
public static Int32 registerCreditCard()
{
ServiceReference1.ServiceClient serviceClient = new ServiceReference1.ServiceClient();
Random rnd = new Random();
bool valid;
Int32 newCard;
string encryptedCard = "";
do
{
valid = true;
newCard = rnd.Next(5000, 10000);
for (int i = 0; i < cardList.Count && valid; i++)
{
if(newCard == Int32.Parse(serviceClient.Decrypt(cardList[i])))
{
valid = false;
}
}
} while (!valid);
encryptedCard = serviceClient.Encrypt(newCard.ToString());
cardList.Add(encryptedCard);
fundsList.Add(0);
return newCard;
}
/*
* Adds funds and returns true if the creditCard is found, otherwise returns false
*/
public static bool deposit(Int32 creditCardNumber, double amount)
{
ServiceReference1.ServiceClient serviceClient = new ServiceReference1.ServiceClient();
int index = -1;
for (int i = 0; i < cardList.Count && index == -1; i++)
{
if(creditCardNumber == Int32.Parse(serviceClient.Decrypt(cardList[i])))
{
index = i;
}
}
if(index != -1)
{
fundsList[index] += amount;
return true;
}
else
{
return false;
}
}
}
}