Write a program that creates a class Account that stores a variable balance(double data type). The class has the following methods:
void startAccount(double amt)
void deposit(double amt)
double withdraw(double amt)
double getBalance()
Answers
Answer:
Requirement: Write a class, BankAccount, that has two instance variables for the name of the owner (of type String) and the balance (of type double). Add to this class the following methods:
A constructor that initializes the BankAccount instance variables to the values in two parameters, the name of the owner and the initial balance (>=0.0).
deposit: Given an amount (>0.0) to deposit, this method deposits it into the account.
withdraw: Given an amount (>0.0 and <= current balance) to withdraw, this method withdraws it from the account.
getName: This method returns the owner name.
getBalance: This method returns tth current balance. (Don't try to format a number-just take the default.)
Include appropriate checks in your methods to ensure that the amounts deposited and withdrawn satisfy the constraints specified. Use good encapsulation guidelines.
Did I interpret this correctly and execute it correctly and if not what needs fixed and how. Any help is appreciated.
Code:
import java.util.*;
public class BankAccount {
static Scanner in = new Scanner (System.in);
private static String name;
private static double balance;
public BankAccount(String n, double b){
System.out.println("Enter your name: ");
n = in.nextLine();
name = n;
System.out.println("Enter your current balance: ");
b = in.nextDouble();
balance = b;
}
public void deposit(){
System.out.println("Enter the amount you would like to deposit: ");
double deposit = in.nextDouble();
if(deposit > 0.0){
balance = balance + deposit;
}
}
public void withdraw(){
System.out.println("Enter the amount you would like to withdraw: ");
double withdraw = in.nextDouble();
if(withdraw > 0.0 && withdraw <= balance){
balance = balance - withdraw;
}
}
public static String getName(){
return name;
}
public static double getBalance(){
return balance;
}
}