-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanking-count-case.js
89 lines (76 loc) · 2.89 KB
/
banking-count-case.js
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
let customers = []
//tambah customer
function addCustomer(name, accountNumber) {
let newCustomer = {
name: name,
accountNumber: accountNumber,
balance: 0,
transactionHistory: []
}
customers.push(newCustomer)
console.log(customers)
}
// check account number
function checkAccount(accountNumber) {
return customers.find((customer) => customer.accountNumber === accountNumber)
}
function viewTransactionHistory(accountNumber) {
return customers.find((customers) => {
if (customers.accountNumber === accountNumber) {
console.log(customers.transactionHistory)
return customers.transactionHistory
}
})
}
function deposit(accountNumber, amount) {
let customer = checkAccount(accountNumber);
if (customer) {
customer.balance += amount;
customer.transactionHistory.push({ date: Date.now(), type: 'deposit', amount: amount });
console.log(`Deposit of ${amount} to account number ${accountNumber} was successful.`);
} else {
console.log(`Customer with account number ${accountNumber} not found.`);
}
}
function withdraw(accountNumber, amount) {
let customer = checkAccount(accountNumber);
if (customer) {
if (customer.balance >= amount) {
customer.balance -= amount;
customer.transactionHistory.push({ type: 'withdrawal', amount: amount });
console.log(`Withdrawal of ${amount} from account number ${accountNumber} was successful.`);
} else {
console.log(`Insufficient balance for withdrawal from account number ${accountNumber}.`);
}
} else {
console.log(`Customer with account number ${accountNumber} not found.`);
}
}
function transactionHistory(type, accountNumber) {
customers.find((customers) => {
if (customers.accountNumber == accountNumber) {
switch (type) {
case 'deposit':
let lastdeposit = customers.transactionHistory.find((transaction) => transaction.type == "deposit")
console.log(`Transaksi Deposit Saat Ini ${lastdeposit.amount}`)
break;
case 'withdraw':
let lastWithDraw = customers.transactionHistory.find((transaction) => transaction.type == "withdrawal")
lastWithDraw.amount > 0 ? console.log(`Transaksi WithDraw ${lastWithDraw.amount}`) : console.log("Saat Ini Kamu Belum Melakukan transaksi")
break;
default:
console.log("Invalid Transaction Type")
}
}
}
)
}
addCustomer("Jean", "123456789")
console.log(checkAccount("123456789"))
addCustomer("Amira", "12345678")
deposit("123456789", 2000)
withdraw("123456789", 500)
viewTransactionHistory("123456789")
transactionHistory("deposit", "123456789")
transactionHistory("withdraw", "123456789")
//deposit