-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccount.java
More file actions
78 lines (60 loc) · 1.87 KB
/
Account.java
File metadata and controls
78 lines (60 loc) · 1.87 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
package src;
import java.util.Scanner;
public class Account {
private String name;
private double balance;
// default src.constructor.
public Account() {
name = "";
balance = 0.0;
}
// parametrized src.constructor.
public Account(String name, double balance) {
this.name = name;
if (balance > 0.0) {
this.balance = balance;
}
}
// deposit amount method.
public void depositAmount(double amount) {
if (amount > 0.0) {
balance += amount;
}
}
// withdraw method
public void withdrawn(double amount) {
if (amount > 0.0 && amount <= balance) {
balance -= amount;
} else {
System.out.println("\nWithdrawn amount exceed!.");
System.out.println("Transaction failed.\n");
}
}
// balance getter method
public double getBalance() {
return balance;
}
// name setter method
public void setName(String name) {
this.name = name;
}
// name getter method
public String getName() {
return name;
}
// main driven function.
public static void main(String[] args) {
var input = new Scanner(System.in);
var account = new Account(" ", 0);
System.out.print("Enter your name : ");
account.setName(input.nextLine());
System.out.println("Your account balance is " + account.getBalance());
System.out.print("Enter the amount to deposit in your account : ");
account.depositAmount(input.nextDouble());
System.out.println("Your new balance is : " + account.getBalance());
System.out.print("Enter amount you wish to withdraw : ");
account.withdrawn(input.nextDouble());
System.out.println("Your new balance is : " + account.getBalance());
input.close();
}
}