-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment3
More file actions
87 lines (68 loc) · 2.55 KB
/
assignment3
File metadata and controls
87 lines (68 loc) · 2.55 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
// write a program that simulates action within a bank account
import java.util.Scanner;
public class bankAccount1
{
static Scanner kb = new Scanner(System.in); // applies to whole main when before main
public static void main(String[ ] args)
{
char option;
double initialBal, balance;
System.out.println("Enter the initial balance ");
initialBal = kb.nextDouble();
balance = initialBal;
menu();
do
{
System.out.println("What is the option? ");
option = kb.next().charAt(0);
switch (option) // if the switch option was an integer it would be numbers not 'letter'
{
case 'D': balance = Deposit(balance); // this line is calling the method deposit, sending balance to it
break; // send the balance to method deposit then send balance back to new balance and print new balance
case 'W': balance = Withdraw(balance); // these methods must be returning methods!!
break; // need to return the balance because these fuctions (D/W) are related!!
// "when methods are related, we must return the value back"
case 'P': Print(initialBal, balance);
break;
case 'Q': System.out.println("Quit ");
break;
} // end switch
} // end loop
while(option != 'Q');
} // end main
public static void menu()
{
System.out.println("\t D: Deposit ");
System.out.println("\t W: Withdraw ");
System.out.println("\t P: Print "); // dont use print as separate method
System.out.println("\t Q: Quit ");
}
public static double Deposit(double balance) // Deposit is name of method
{
double dep, totalDep = 0; // local variables
System.out.println("Enter deposit ");
dep = kb.nextDouble();
balance = balance + dep;
totalDep = totalDep + dep;
System.out.println("Total Deposit = " + totalDep);
return balance;
}
public static double Withdraw(double balance) // Withdraw is name of method
{
double check, totalCheck = 0;
System.out.println("Enter check ");
check = kb.nextDouble();
balance = balance - check;
totalCheck = totalCheck + check;
System.out.println("Total check = " + totalCheck);
return balance;
}
public static void Print(double initialBal,
double balance)
{
System.out.println("The initial balance was = "
+ initialBal);
System.out.println("The current balance is = "
+ balance);
}
} // end class