public class JavaAccount { private String customerName; private long AccountNumb
ID: 3844786 • Letter: P
Question
public class JavaAccount { private String customerName; private long AccountNumber; private double balance; private double interestRate; JavaAccount(String customerName,long AccountNumber,double balance,double interestRate){ this.customerName=customerName; this.AccountNumber=AccountNumber; this.balance=balance; this.interestRate=interestRate; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public long getAccountNumber() { return AccountNumber; } public void setAccountNumber(long accountNumber) { AccountNumber = accountNumber; } public double getBalance() { return balance; } public void setBalance(double d) { this.balance = d; } public double getInterestRate() { return interestRate; } public void setInterestRate(double d) { this.interestRate = d; } public void deposit(double amount){ this.setBalance(this.getBalance()+amount); System.out.println("Deposit Successful"); System.out.println("New Amount:"+this.getBalance()); return; } public void withdraw(double amount){ this.setBalance(this.getBalance()-amount); System.out.println("Withdrawl Successful"); System.out.println("New Amount:"+this.getBalance()); return; } public void checkBalance(){ System.out.println("Balance:"+getBalance()); } }
(a) When you extends the javaAccount.java, you will need to override the super() constructors. (b) Add a static field called "minimumBalanceRequired" such that when account balance meets the minimumBalanceRequired, you get unlimited checking without fees. Otherwise, regular checking fees will apply. // hint: inside the withdraw() method, deduct a fee whenever the balance is below the minimum. (c) Add a static method to reset minimumBalanceRequired. (d) Add a new data fields called PIN# such that an internal PIN# has to match the input parameters before any transactions. Re-write the following methods with a PIN#: checkBalance(); deposit(), withdraw() and so on. (e) Add an implementation of the .toString() method to override any default methods: The toString() methods returns the premium account information #2. Add implementation of exception handling to the PremiumAccount.java class. (a) Inside each method with a PIN# input parameter, throw a appropriate exception object of your choice. (b) (OPTIONAL) Implement a new exception mechanism that allows only 3 consecutive trials of invalid PINs.
Explanation / Answer
Hi,
Please see the updated classes.
Please comment for any queries/feedbacks
Thanks,
JavaAccount.java
public class JavaAccount {
public static double minimumBalanceRequired = 20;
private static String PIN = "XXXYYYZZZ";
public static double checkFee = 2;
private String customerName;
private long AccountNumber;
private double balance;
private double interestRate;
JavaAccount(String customerName,long AccountNumber,double balance,double interestRate){
super();
this.customerName=customerName;
this.AccountNumber=AccountNumber;
this.balance=balance;
this.interestRate=interestRate;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public long getAccountNumber() {
return AccountNumber;
}
public void setAccountNumber(long accountNumber) {
AccountNumber = accountNumber;
}
public double getBalance() {
return balance;
}
public void setBalance(double d) {
this.balance = d;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double d) {
this.interestRate = d;
}
public void deposit(double amount, String pin){
if(pin.equalsIgnoreCase(PIN)){
this.setBalance(this.getBalance()+amount);
System.out.println("Deposit Successful");
System.out.println("New Amount:"+this.getBalance());
}
else{
System.out.println("Invalid PIN!");
}
return;
}
public void withdraw(double amount,String pin){
if(pin.equalsIgnoreCase(PIN)){
if(this.getBalance()>=minimumBalanceRequired){
this.setBalance(this.getBalance()-amount);
System.out.println("Withdrawl Successful");
System.out.println("New Amount:"+this.getBalance());
}
else{
//deduct a fee whenever the balance is below the minimum.
this.setBalance(this.getBalance() - checkFee);
}
}
else{
System.out.println("Invalid PIN!");
}
return;
}
public void checkBalance(String pin){
if(pin.equalsIgnoreCase(PIN)){
System.out.println("Balance:"+getBalance());
}
else{
System.out.println("Invalid PIN!");
}
}
/**
* static method to reset minimumBalanceRequired
* @param minAmt
*/
public static void resetMinimumBalanceRequired(double minAmt){
minimumBalanceRequired = minAmt;
}
}
PremiumAccount.java
public class PremiumAccount extends JavaAccount {
public static double minimumBalanceRequired = 20;
public static double checkFee = 2;
private static String PIN = "XXXYYYZZZ";
private static int invalidPinCount = 0;
/**
* Constructor
* @param customerName
* @param AccountNumber
* @param balance
* @param interestRate
*/
PremiumAccount(String customerName,long AccountNumber,double balance,double interestRate){
super( customerName, AccountNumber, balance, interestRate);
}
public void deposit(double amount, String pin){
if(invalidPinCount>=3){
System.out.println("3 consecutine inavlid PINs tried!");
return;
}
if(pin.equalsIgnoreCase(PIN)){
this.setBalance(this.getBalance()+amount);
System.out.println("Deposit Successful");
System.out.println("New Amount:"+this.getBalance());
invalidPinCount = 0;
}
else{
System.out.println("Invalid PIN!");
invalidPinCount++;
try {
throw new Exception("Invalid PIN!");
} catch (Exception e) {
e.printStackTrace();
}
}
return;
}
public void withdraw(double amount,String pin){
if(invalidPinCount>=3){
System.out.println("3 consecutine inavlid PINs tried!");
return;
}
if(pin.equalsIgnoreCase(PIN)){
if(this.getBalance()>=minimumBalanceRequired){
this.setBalance(this.getBalance()-amount);
System.out.println("Withdrawl Successful");
System.out.println("New Amount:"+this.getBalance());
}
else{
//deduct a fee whenever the balance is below the minimum.
this.setBalance(this.getBalance() - checkFee);
}
invalidPinCount = 0;
}
else{
System.out.println("Invalid PIN!");
invalidPinCount++;
try {
throw new Exception("Invalid PIN!");
} catch (Exception e) {
e.printStackTrace();
}
}
return;
}
public void checkBalance(String pin){
if(invalidPinCount>=3){
System.out.println("3 consecutine inavlid PINs tried!");
return;
}
if(pin.equalsIgnoreCase(PIN)){
System.out.println("Balance:"+getBalance());
invalidPinCount = 0;
}
else{
System.out.println("Invalid PIN!");
invalidPinCount++;
try {
throw new Exception("Invalid PIN!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* static method to reset minimumBalanceRequired
* @param minAmt
*/
public static void resetMinimumBalanceRequired(double minAmt){
minimumBalanceRequired = minAmt;
}
public String toString(){
String retString;
retString = "customerName : "+this.getCustomerName()+" AccountNumber : "+this.getAccountNumber()+" Balance : "+this.getBalance();
return retString;
}
}
AccountMain.java
public class AccountMain {
/**
* @param args
*/
public static void main(String[] args) {
PremiumAccount premium1 = new PremiumAccount("Rachel", 922337203685L, 500.00, .13);
premium1.deposit(300, "XXXYYYZZZ");
premium1.withdraw(200, "XXXYYYZZZ");
premium1.checkBalance("XXXYYYZZZ");
//wrong pin test
premium1.deposit(300, "YYY");
premium1.withdraw(200, "YYY");
premium1.checkBalance("YYY");
premium1.checkBalance("YYY");
}
}
Sample output:
Deposit Successful
New Amount:800.0
Withdrawl Successful
New Amount:600.0
Balance:600.0
Invalid PIN!
Invalid PIN!
Invalid PIN!
3 consecutine inavlid PINs tried!
java.lang.Exception: Invalid PIN!
at PremiumAccount.deposit(PremiumAccount.java:36)
at AccountMain.main(AccountMain.java:14)
java.lang.Exception: Invalid PIN!
at PremiumAccount.withdraw(PremiumAccount.java:64)
at AccountMain.main(AccountMain.java:15)
java.lang.Exception: Invalid PIN!
at PremiumAccount.checkBalance(PremiumAccount.java:84)
at AccountMain.main(AccountMain.java:16)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.