Write a class called BankAccount that has a string data member called customerNa
ID: 3665229 • Letter: W
Question
Write a class called BankAccount that has a string data member called customerName, a string data member called customerID, and a double data member called customerBalance. The class should have a constructor that takes two strings and a double (name, ID, balance) and uses them to initialize the data members.
The data members of this class do not need to be set after they are initialized, so this class doesn't need any set methods - therefore the constructor will directly assign values to the data members instead of calling set methods to do it.
The class should have a get method for each data member. It should have a method called withdraw that takes a double parameter and deducts it from the current balance (the balance is allowed to be negative). It should have a method called deposit that takes a double parameter and adds it to the current balance.
Your functions should have the following names:
- getCustomerName
- getCustomerID-
- getCustomerBalance
- withdraw
- deposit
Your functions should have the following names: getCustomerName getCustomerID getCustomerBalance withdraw deposit The files must be named: BankAccount.hpp and BankAccount.cpp
Just wondering how a tutor would go about this on here? Trying to work this problem using vim on Atom.
-Thanks!
Explanation / Answer
I don't have idea about any other language but In Java :
package com.he.capillary;
public class BankAccount {
private Double customerBalance;
private String customerID;
private String customerName;
public Double getCustomerBalance() {
return customerBalance;
}
public String getCustomerID() {
return customerID;
}
public String getCustomerName() {
return customerName;
}
public BankAccount(Double customerBalance, String customerID, String customerName) {
super();
this.customerBalance = customerBalance;
this.customerID = customerID;
this.customerName = customerName;
}
public synchronized void withdraw(double amount) {
this.customerBalance -= amount;
}
public synchronized void deposit(double amount) {
this.customerBalance += amount;
}
}
I have used synchronized to avoid race condition where user will be depositing and withdrawing at the same time.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.