Create a class called Account with member variable balance. Create three methods one for Depositing Money, the second for withdrawing money and the third for checking balance. In addition to the three classes, add a constructor to initialize the balance variable. Have the initial balance of the account set to 712,983.88. Have a user deposit any amount and prompt them if they wish to withdraw any amount. If not, print out the balance of the account.
public class Account {
private double balance;
public Account(double balance) {
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
public double getBalance() {
return balance;
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Account account = new Account(712983.88);
System.out.println("Deposit:");
account.deposit(Double.parseDouble(in.nextLine()));
System.out.println("Want withdraw(Y | N):");
if (in.nextLine().equalsIgnoreCase("y")) {
System.out.println("Amount:");
account.withdraw(Double.parseDouble(in.nextLine()));
} else {
System.out.println("Balance: " + account.getBalance());
}
}
}
Comments
Leave a comment