Skip to content

Commit e5d6527

Browse files
authored
Merge pull request #166 from hoaianhkhang/master
Adding A Banking Example
2 parents 19a29aa + 0bf71bc commit e5d6527

File tree

2 files changed

+212
-1
lines changed

2 files changed

+212
-1
lines changed

BankExample/banking.c

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
#include <stdio.h>
2+
#define MAX_ITEMS 10
3+
double balanceUp (double balance, double amount)
4+
{
5+
double at;
6+
if (amount >= 0)
7+
{
8+
at = balance + amount;
9+
return at;
10+
11+
}
12+
else
13+
{
14+
printf("Error");
15+
return 0;
16+
}
17+
}
18+
double balanceDown(double balance, double amount)
19+
{
20+
double at;
21+
if (amount >= 0)
22+
{
23+
at = balance - amount;
24+
return at;
25+
26+
}
27+
else
28+
{
29+
printf("Error");
30+
return 0;
31+
}
32+
}
33+
double interestCalc(double balance, double rate)
34+
{
35+
double in;
36+
in = balance * (rate / 100);
37+
// printf("%f\n", in);
38+
return in;
39+
}
40+
int main()
41+
{
42+
struct Account {
43+
int acc;
44+
double bala;
45+
};
46+
struct Account acct[MAX_ITEMS];
47+
acct[0].acc = 11111111;
48+
acct[1].acc = 22222222;
49+
acct[2].acc = 33333333;
50+
acct[3].acc = 44444444;
51+
acct[4].acc = 55555555;
52+
acct[0].bala = 123.45;
53+
acct[1].bala = 12365.50;
54+
acct[2].bala = 0;
55+
acct[3].bala = 1475;
56+
acct[4].bala = 25000.65;
57+
float a;
58+
double b;
59+
float c;
60+
float d;
61+
float e;
62+
double f;
63+
int choice, acc;
64+
int l = 1;
65+
int i;
66+
printf("***** Welcome to Savings Account Banking *****\n\n");
67+
while (l) {
68+
do {
69+
printf("1.) Deposit \n");
70+
printf("2.) Withdraw\n");
71+
printf("3.) Apply monthly interest earnings to all accounts\n");
72+
printf("4.) Apply service charges to all accounts\n");
73+
printf("5.) Account Summary\n");
74+
printf("0.) Log out\n\n");
75+
printf("Please enter an option to continue: ");
76+
scanf("%d", &choice);
77+
if (choice < 0 || choice > 5)
78+
{
79+
printf("Invalid input, try again: Please select from the following options:\n");
80+
}
81+
} while (choice < 0 || choice > 5);
82+
if (choice == 0)
83+
{
84+
l = 0;
85+
}
86+
if (choice == 1)
87+
{
88+
printf("\n-- Make a deposit --\n\n");
89+
printf("Enter account number: ");
90+
scanf("%d", &acc);
91+
for (i = 0; i < 5; i++)
92+
{
93+
94+
if (acc != acct[i].acc && i == 4)
95+
printf("ERROR: Account number does not exist.\n\n");
96+
if (acc == acct[i].acc)
97+
{
98+
printf("Enter amount to deposit (CAD): ");
99+
scanf("%f", &a);
100+
b = balanceUp(acct[i].bala, a);
101+
printf("Current balance is : %.2f\n", b);
102+
acct[i].bala = b;
103+
break;
104+
}
105+
}
106+
printf("\n");
107+
}
108+
if (choice == 2)
109+
{
110+
printf("\n-- Withdraw funds --\n\n");
111+
printf("Enter account number: ");
112+
scanf("%d", &acc);
113+
for (i = 0; i < 5; i++)
114+
{
115+
116+
if (acc != acct[i].acc && i == 4)
117+
printf("ERROR: Account number does not exist.\n");
118+
if (acc == acct[i].acc)
119+
{
120+
printf("Enter amount to withdraw (CAD): ");
121+
scanf("%f", &e);
122+
if (e > acct[i].bala)
123+
printf("Withdrawal failed. You only have : %.2f in your account\n",acct[i].bala);
124+
else {
125+
f = balanceDown(acct[i].bala, e);
126+
printf("Current balance is : %.2f\n", f);
127+
acct[i].bala = f;
128+
}
129+
break;
130+
}
131+
132+
}
133+
printf("\n");
134+
}
135+
if (choice == 3)
136+
{
137+
printf("\n-- Add monthly interest earnings to all accounts --\n\n");
138+
printf("Account# New Balance Interest Earnings (M)\n");
139+
printf("-------- ----------- ---------------------\n");
140+
for (i = 0; i < 5; i++)
141+
{
142+
if (acct[i].bala <= 500)
143+
{
144+
c = interestCalc(acct[i].bala, 0.99);
145+
146+
d = balanceUp(acct[i].bala, c);
147+
acct[i].bala = d;
148+
printf("%8d %11.2lf %21.2lf\n", acct[i].acc, d, c);
149+
}
150+
else if (acct[i].bala > 500 && acct[i].bala <= 1500)
151+
{
152+
c = interestCalc(acct[i].bala, 1.66);
153+
154+
d = balanceUp(acct[i].bala, c);
155+
acct[i].bala = d;
156+
printf("%8d %11.2lf %21.2lf\n", acct[i].acc, d, c);
157+
}
158+
else if (acct[i].bala > 1500)
159+
{
160+
c = interestCalc(acct[i].bala, 2.49);
161+
d = balanceUp(acct[i].bala, c);
162+
acct[i].bala = d;
163+
printf("%8d %11.2lf %21.2lf\n", acct[i].acc, d, c);
164+
}
165+
}
166+
printf("\n");
167+
}
168+
169+
if (choice == 4)
170+
{
171+
printf("\n-- Apply service charges to all accounts --\n\n");
172+
printf("Account# New Balance Service charges (M)\n");
173+
printf("-------- ----------- -------------------\n");
174+
for (i = 0; i < 5; i++)
175+
{
176+
if (acct[i].bala <= 1500)
177+
{
178+
d = balanceDown(acct[i].bala, 7.50);
179+
acct[i].bala = d;
180+
printf("%8d %11.2lf %19.2lf\n", acct[i].acc, d, 7.50);
181+
}
182+
else if (acct[i].bala > 1500)
183+
{
184+
185+
d = balanceDown(acct[i].bala, 2.50);
186+
acct[i].bala = d;
187+
printf("%8d %11.2lf %19.2lf\n", acct[i].acc, d, 2.50);
188+
}
189+
190+
}
191+
printf("\n");
192+
}
193+
194+
195+
if (choice == 5)
196+
{
197+
printf("\n-- Account information --\n");
198+
printf("\nAccount# Balance \n");
199+
printf("-------- ----------\n");
200+
for (i = 0; i < 5; i++)
201+
{
202+
printf("%8d %10.2lf\n", acct[i].acc, acct[i].bala);
203+
204+
}
205+
printf("\n");
206+
}
207+
}
208+
209+
return 0;
210+
}

contributors.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@
4444
42. [Nitish Kumar](https://github.com/nitish14kumr)
4545
43. [Seemant Aggarwal] (https://github.com/seemantaggarwal)
4646
44. [Vibhav Tiwari] (https://github.com/VibhavTiwari)
47-
45. [Your Name](https://github.com/yourusername)
47+
45. [Anh Hoai Ung](https://github.com/hoaianhkhang)
48+
46. [Your Name](https://github.com/yourusername)

0 commit comments

Comments
 (0)