Question

ATM Revisited In Java, design a subclass of the Account class called GoldAccount. It is still...

ATM Revisited

In Java, design a subclass of the Account class called GoldAccount. It is still an Account class, however it has an additional field and some additional functionality. But it will still retain all the behavior of the Account class.

Write a class called GoldAccount. This class will have an additional private field called bonusPoints. This field will require no get or set methods. But it will need to be initialized to 100 in the constructor. Thereafter, after a deposit to the gold account, bonusPoints will be increased by a random number between 100 and 200. This somewhat simulates a preferred client for the bank. If the bonusPoints increase to over 1000 then the gold account gets an additional $10 added to it, and bonusPoints is reduced by 1000. This is the only new behavior that we are adding to our class.

The benefit of inheritance is that this subclass, GoldAccount, does not have to re-write everything that is in the Accountsclass. Everything that is in Accounts gets included in GoldAccount simply by virtue of it’s being a subclass of Accounts. So all we need to add to our GoldAccount is this extra field called bonusPoints.

The other method to over ride is the toString() method. In the GoldAccount class, the toString() method needs to include the number of bonus points accrued. The gold account toString() method will call the superclass’ toString() method and then append the number of bonus points to the returned string.

GoldAccount Class

The GoldAccount class is not as long as the Accounts class. It consists of a single field, a constructor, and two methods. Again, the new field is called bonusPoints. The constructor will call the superclass constructor with the keyword super(), and then initialize bonusPoints to 100.

The two methods over ride methods of the Accounts class. The first method to over ride is the deposit() method. The GoldAccount deposit() method will make sure that the user has validated himself and then will add the deposit amount to the balance through the superclass’ deposit() method (again using the keyword super). It will then add a random number (between 100 to 200, inclusive) to bonusPoints. If bonusPoints goes over 1000 then an additional $10 will be added to the balance. Again, through the superclass’ deposit() method. The method will print to the screen in this case to alert the user that he has just been awarded an additional $10. Be sure to then subtract 1000 from bonusPoints.

My Account Class

import java.security.SecureRandom;

public class Account {

private String name;

private int pword;

private double balance;

private boolean validated;

private static int numOfAccounts = 0;

public Account(){

// assign password equal to account number

pword = numOfAccounts;

// set balance equal to random number between 0 and 1000

SecureRandom rng = new SecureRandom();

balance = rng.nextDouble()*1000;

// initially account is not validated

validated = false;

// assign name starting with A, B, C, ..., A1, B1, C1, ....

if(numOfAccounts < 25) name = Character.toString((char)(numOfAccounts+ 65));

else{

int multiple = numOfAccounts/26;

//name = Character.toString((char)multiple);

name = Character.toString((char)((numOfAccounts - 26*multiple)+65)) + multiple;

}

numOfAccounts++;

}

public boolean validate(int pword_guess){

if(pword_guess == pword) validated = true;

else validated = false;

return validated;

}

public boolean withdraw(double d) {

if(validated){

if(d > balance) return false;

else{

balance -= d;

return true;

}

}else return false;

}

public void deposit(double d){

if(validated) balance += d;

}

public double getBalance(){

if(validated) return balance;

else return 0;

}

public String getName(){

return name;

}

public boolean getValidated(){

return validated;

}

@Override

public String toString(){

if(validated)

return "\n\tName: " + name + "\tPassword: " + pword +"\tbalance: " + "$" + String.format("%.2f", balance)+ "\tValidated: " + validated;

else return "";

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

GoldAccount.java:

public class GoldAccount extends Account
{
private int bonusPoints;

public GoldAccount()
{
super();
bonusPoints = 100;
}

@Override
public void deposit(double d)
{
if (super.getValidated())
{
super.deposit(d);

// Math.random returns a value in [0, 1)
// Changing it to a different range, 100-200 (both inclusive)
bonusPoints += (int)(Math.random()*(201-100) + 100);
if (bonusPoints > 1000)
{
super.deposit(10);

System.out.println("\nCustomer has been awarded an additional $10!");
bonusPoints -= 1000;
}
}
}

@Override
public String toString()
{
return super.toString() + "\tBonusPoints: " + bonusPoints;
}
}

Driver.java:

// Driver class for testing
public class Driver
{
public static void main(String[] args)
{
GoldAccount gacc1 = new GoldAccount();
GoldAccount gacc2 = new GoldAccount();

System.out.println(gacc1.validate(1));
System.out.println(gacc1.validate(0));

System.out.println(gacc2);
System.out.println(gacc2.validate(1));
System.out.println(gacc2);

System.out.println();
gacc1.deposit(100);
gacc1.deposit(323);
gacc1.deposit(200);
System.out.println(gacc1);

gacc1.deposit(100);
gacc1.deposit(100);
gacc1.deposit(100);
System.out.println(gacc1);
}
}

Output:

avac -d bin *.java

Administrator: CWindows System32\cmd.exe java Driver false true BonusPoints: 100 true Name: B Password: 1 Balance: $829.77 Va

Add a comment
Know the answer?
Add Answer to:
ATM Revisited In Java, design a subclass of the Account class called GoldAccount. It is still...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • public class Fish { private String species; private int size; private boolean hungry; public Fish() {...

    public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...

  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods. A no- arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should...

  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

  • THE FOLLOWING PROGRAM IS NEEDED IN JAVA LANGUAGE Design a class torepresent account, include the following...

    THE FOLLOWING PROGRAM IS NEEDED IN JAVA LANGUAGE Design a class torepresent account, include the following members: -Data Members a)Name of depositor-Stringb)Account Number –intc)Type of account –Boolean d)Balance amount -doublee)AnnualInterestrate -double Methods: -(a)To assign initial values (use constructor)(b)To deposit an amount.(c)TO withdraw amount with the restriction the minimum balance is 50 rs. Ifyouwithdraw amount reduced the balance below 50 then print the error message.(d)Display the name and balance of the account.(e)Get_Monthly_intrestRate() -Return the monthly interestrate whichis nothing but Annualintrestrate/12. Annualinterestrate...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • Requirements:  Your Java class names must follow the names specified above. Note that they are...

    Requirements:  Your Java class names must follow the names specified above. Note that they are case sensitive. See our template below.  BankAccount: an abstract class.  SavingAccount: a concrete class that extends BankAccount. o The constructor takes client's firstname, lastname and social security number. o The constructor should generate a random 6-digit number as the user account number. o The initial balance is 0 and the annual interest rate is fixed at 1.0% (0.01).o The withdraw method signature...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT