|
| 1 | + |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +public class Account |
| 5 | +{ |
| 6 | + private String name; |
| 7 | + private double balance; |
| 8 | + |
| 9 | + // default constructor. |
| 10 | + public Account() |
| 11 | + { |
| 12 | + name = ""; |
| 13 | + balance = 0.0; |
| 14 | + } |
| 15 | + |
| 16 | + // parametrized constructor. |
| 17 | + public Account(String name, double balance) |
| 18 | + { |
| 19 | + this.name = name; |
| 20 | + if (balance > 0.0) |
| 21 | + { |
| 22 | + this.balance = balance; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + // deposite amount method. |
| 27 | + public void depositAmount(double amount) |
| 28 | + { |
| 29 | + if (amount > 0.0) |
| 30 | + { |
| 31 | + balance += amount; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // withdraw method |
| 36 | + public void withdrawn(double amount) |
| 37 | + { |
| 38 | + if (amount > 0.0 && amount <= balance) |
| 39 | + { |
| 40 | + balance -= amount; |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + System.out.println("\nWithdrawn amount exceed!."); |
| 45 | + System.out.println("Transaction failed.\n"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // balance getter method |
| 50 | + public double getBalance() |
| 51 | + { |
| 52 | + return balance; |
| 53 | + } |
| 54 | + |
| 55 | + // name setter method |
| 56 | + public void setName(String name) |
| 57 | + { |
| 58 | + this.name = name; |
| 59 | + } |
| 60 | + |
| 61 | + // name getter method |
| 62 | + public String getName() |
| 63 | + { |
| 64 | + return name; |
| 65 | + } |
| 66 | + |
| 67 | + // main driven function. |
| 68 | + public static void main(String[] args) |
| 69 | + { |
| 70 | + var input = new Scanner(System.in); |
| 71 | + |
| 72 | + var account = new Account(" ", 0); |
| 73 | + |
| 74 | + System.out.print("Enter your name : "); |
| 75 | + account.setName(input.nextLine()); |
| 76 | + |
| 77 | + System.out.println("Your account balance is " + account.getBalance()); |
| 78 | + |
| 79 | + System.out.print("Enter the amount to deposit in your account : "); |
| 80 | + account.depositAmount(input.nextDouble()); |
| 81 | + |
| 82 | + System.out.println("Your new balance is : " + account.getBalance()); |
| 83 | + |
| 84 | + System.out.print("Enter amount you wish to withdraw : "); |
| 85 | + account.withdrawn(input.nextDouble()); |
| 86 | + |
| 87 | + System.out.println("Your new balance is : " + account.getBalance()); |
| 88 | + |
| 89 | + input.close(); |
| 90 | + } |
| 91 | +} |
0 commit comments