-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankingApplication.java
More file actions
110 lines (88 loc) · 3.41 KB
/
BankingApplication.java
File metadata and controls
110 lines (88 loc) · 3.41 KB
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
import java.util.Scanner;
public class BankingApplication {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
/*
JAVA BANKING PROGRAM
Helps understand the use of Methods. Beginner's project on a signature type of Java application.
Basic characteristics and actions such as:
create account, login and userbase system, check balance, add
or remove balance, take a loan with time module, and more.
INCLUDE Compound Interest Calculator here too.
And a currency converter.
*/
// DECLARE VARIABLES
double balance = 0;
boolean isRunning = true;
int choice;
// DISPLAY A MENU - user picks a choice, make deposit, show their balance, take a withdrawal etc.
// Keep it displayed as long as isRunning Boolean is true, so invalid choices keep the program on.
while(isRunning){
System.out.println("***********");
System.out.println("BANKING APP");
System.out.println("***********");
System.out.println("1) Show Balance");
System.out.println("2) Make a Deposit");
System.out.println("3) Withdraw Money");
System.out.println("4) Exit");
System.out.println("***********");
// GET AND PROCESS USERS CHOICE
System.out.print("Enter your choice (1-4): ");
choice = scanner.nextInt();
// An enhanced switch will be used to process the users' choice
switch(choice){
case 1 -> showBalance(balance);
case 2 -> balance = balance + deposit();
case 3 -> balance = balance - withdraw(balance);
case 4 -> isRunning = false;
default -> System.out.println(" - Invalid choice, please try again - ");
}
}
// the exit message
System.out.println("----------------");
System.out.println("Thanks for using");
System.out.println("----------------");
// USING METHODS outside this main class
// 1. showBalance()
// 2. deposit()
// 3. withdraw()
// 4. EXIT MESSAGE
//Later
// 5. Compound Interest Calculator
// 6. Currency Converter
scanner.close();
}
static void showBalance(double balance){
System.out.println("\nYour Balance");
System.out.printf("£%.2f\n\n", balance);
}
static double deposit(){
double amount;
System.out.print("Enter an amount to deposit: ");
amount = scanner.nextDouble();
if(amount < 0){
System.out.println("Amount cannot be negative");
return 0;
}
else{
return amount;
}
}
static double withdraw(double balance){
double amount;
System.out.print("How much do you wish to withdraw from balance: ");
amount = scanner.nextDouble();
if(amount > balance){
System.out.println("Insufficient funds to perform this withdrawal");
return 0;
}
else if(amount < 0){
//user inputs minus then amount, still process
amount *= -1;
return amount;
}
else{
return amount;
}
}
}