No Title

09 March 2021

Views: 73

Step 1
EXPLANATION: -

The BankAccount class is defined as having instance variables for name, surname, address, phoneNum, balance, and lastActionPerformed.
The constructor is defined to instantiate the BankAccount.
Appropriate getter methods are defined for accessing the values of the private instance variables.
The credit and withdraw methods are defined as well.
The main method is defined in BankAccountMain class.
The static getBankAccount method is defined to return the object of BankAccount class using Scanner class object to read the input from the user.
The displayDetails static method prints the details of the BankAccount.
The amount is read from the user, based on the positive or negative amount entered by the user, the corresponding method is called.
The last opertion performed is printed on the console by called the getter method for that instance variable.
The user is given the prompt to enter the percent value to increase the balance.
Furthermore the balance is credited with the increased amount.
Step 2
//WORKING CODE: -

//BankAccount.java

public class BankAccount {

// declaring the instance variables
private String name, surName;
private String address;
private long phoneNum;
private double balance;

private String lastOperationPerformed;

// parameterized constructor to initialize te instance variables
public BankAccount(String name, String surName, String address, long phoneNum) {
super();
this.name = name;
this.surName = surName;
this.address = address;
this.phoneNum = phoneNum;
this.balance = 0.0;
this.lastOperationPerformed = "No Acction perfomed";
}

//getter method to get the firstname of the BankAccount holder
public String getName() {
return name;
}

//getter method to get the surname of the BankAccount holder
public String getSurName() {
return surName;
}

//getter method to get the address of the BankAccount holder
public String getAddress() {
return address;
}

//getter method to get the phoneNumber of the BankAccount holder
public long getPhoneNum() {
return phoneNum;
}

//getter method to get the balance of the BankAccount
public double getBalance() {
return balance;
}
//getter method to get the operation last performed on the BankAccount
public String getLastOperationPerformed() {
return lastOperationPerformed;
}

//method to store amount in bankAccount
public void credit(double amount) {
lastOperationPerformed = "Credit";
this.balance += amount;
System.out.println("New Balance after credit: "+balance);
}

//method to withdraw amount from bankAccount
public void withdraw(double amount) {
lastOperationPerformed = "Withdraw";
//amount can be withdrawn
if(amount <= this.balance) {
this.balance -= amount;
System.out.println("Left Balance: "+balance);
}else {
System.out.println("Insufficient Balance !!");
}
}
}

Step 3
//BankAccountMain.java

import java.util.Scanner;

public class BankAccountMain {

//instantiating the Scanner class
static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {
//get the BankAccount object
BankAccount account = getBankAccount();

//method for printing the details of the person
displayDetails(account);

System.out.println("
Enter the amount(positive to credit Or Negative to withdraw):");
double amount = sc.nextDouble();
sc.nextLine();

//performing credit or withdraw
if(amount >0) {
account.credit(amount);
}else {
account.withdraw(amount);
}

System.out.println("Last operation performed: "+account.getLastOperationPerformed());

//give prompt and read the percentage to increase the amount
System.out.println("
Enter the percentage to increase the balance: ");
int percent = sc.nextInt();
double increaseAmount = account.getBalance() * percent * 0.01;

//increase the balance by percent value
account.credit(increaseAmount);

}

//method to display the BankAccount detail
private static void displayDetails(BankAccount account) {
System.out.println("
Account details: ");
System.out.println("Name: "+account.getName() + " "+account.getSurName());
System.out.println("Address: "+account.getAddress());
System.out.println("Phone Num: "+account.getPhoneNum());
System.out.println("Balance: "+account.getBalance());
System.out.println("Last Operation performed: "+account.getLastOperationPerformed());
}

//method to retunr the BankAccount object
private static BankAccount getBankAccount() {
//declaring the BankAccount class reference variable
BankAccount account;

String name, surname, address;
long phoneNum;

System.out.println("Enter the following details for opening account: ");
System.out.println("First name: ");
name = sc.nextLine();

System.out.println("SurName: ");
surname = sc.nextLine();

System.out.println("Phone number: ");
phoneNum = Long.parseLong( sc.nextLine());

System.out.println("Address: ");
address = sc.nextLine();

//instantiating the BankAccount class
account = new BankAccount(name, surname,address, phoneNum);

return account;
}

}

Share