Question

For this assignment, you should modify only the User.java file. Make sure that the InsufficientFundsException.java file...

For this assignment, you should modify only the User.java file. Make sure that the InsufficientFundsException.java file is in the same folder as your User.java File. To test this program you will need to create your own "main" function which can import the User, create an instance and call its methods. One of the methods will throw an exception in a particular circumstance. For this assignment, to throw the Exceptions you will use "throw new InsufficientFundsException();" Follow the TODOs and the documentation in the User.java file to complete this assignment.

User.java.file

public class User {
      // Class Attributes
      private String username;
      private double balance;

      /***
      * Default Constructor
      * Should set all values to their defaults
      * Strings should be set to "unknown"
      * balance should start at $0
      */
      public User() {
            // TODO 1 IMPLEMENT THIS METHOD
      }

      /***
      * Constructor
      * the class attributes should be set to the values passed in by the method parameters
      *
      * The remaining attributes should be set to their defaults:
      * balance should start at $0
      *
      * @param: username
      */
      public User(String username) {
            // TODO 2 IMPLEMENT THIS METHOD
      }

      /***
      *
      * @return the username
      */
      public String getUserName() {
            // TODO 3 IMPLEMENT THIS METHOD
            return "";
      }

      /***
      *
      * @return current balance amount as double
      */
      public double getBalance() {
            // TODO 4 IMPLEMENT THIS METHOD
            return 0.0;
      }

      /***
      *
      * @return current balance amount as formatted string with $ and 2 decimal point
      *         precision
      */
      public String getFormattedBalance() {
            // TODO 5 IMPLEMENT THIS METHOD
            return "";
      }

      /***
      * This method should add a deposit amount to the balance
      *
      * @param deposit
      */
      public void deposit(double deposit) {
            // TODO 6 IMPLEMENT THIS METHOD
      }

      /***
      * This method should detract a withdrawal from the balance
      *
      * @param withdrawal
      *
      * @throws InsufficientFundsException if the withdrawal will put the current
      *                                    balance below $0
      */
      public void withdrawal(double withdrawal) throws InsufficientFundsException {
            // TODO 7 IMPLEMENT THIS METHOD
      }

      /***
      *
      * Given a rate and a number of months, calculate the projected balance.
      *
      * Example Balance = $100
      * Months = 4
      * Rate = 0.01 (1%)
      *
      * After 1 Month Balance = $101 (100 + (100 * 0.01))
      * After 2 Months Balance = $102.01 (101 + (101 * 0.01))
      * After 3 Months Balance = $103.0301 (102.01 + (102.01 * 0.01))
      * After 4 Months Balance = $104.060401 (103.0301 + (103.0301 * 0.01))
      *
      * return 104.060401
      *
      * @param rate (Will always be positive)
      * @param months (Will always be greater than 1)
      * @return estimated balance after "months" months of cumulative interest at
      *         rate "rate"
      */
      public double projectedBalance(double rate, int months) {
            // TODO 8 IMPLEMENT THIS METHOD
            return 0.0;
      }
}

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

Hi

Here are all the implemented methods for the User.java class : )

thank you !

_____________________________________________________________________________________________

public class User {
    // Class Attributes
    private String username;
    private double balance;

    /***
     * Default Constructor
     * Should set all values to their defaults
     * Strings should be set to "unknown"
     * balance should start at $0
     */
    public User() {
        // TODO 1 IMPLEMENT THIS METHOD
        username = "";
        balance = 0.0;
    }

    /***
     * Constructor
     * the class attributes should be set to the values passed in by the method parameters
     *
     * The remaining attributes should be set to their defaults:
     * balance should start at $0
     *
     * @param: username
     */
    public User(String username) {
        // TODO 2 IMPLEMENT THIS METHOD
        this.username = username;
    }

    /***
     *
     * @return the username
     */
    public String getUserName() {
        // TODO 3 IMPLEMENT THIS METHOD
        return this.username;
    }

    /***
     *
     * @return current balance amount as double
     */
    public double getBalance() {
        // TODO 4 IMPLEMENT THIS METHOD
        return this.balance;
    }

    /***
     *
     * @return current balance amount as formatted string with $ and 2 decimal point
     *         precision
     */
    public String getFormattedBalance() {
        // TODO 5 IMPLEMENT THIS METHOD
        return "$" + String.format("%.2f", this.balance);
    }

    /***
     * This method should add a deposit amount to the balance
     *
     * @param deposit
     */
    public void deposit(double deposit) {
        // TODO 6 IMPLEMENT THIS METHOD
        if(deposit>0){
            this.balance+=deposit;
        }
    }

    /***
     * This method should detract a withdrawal from the balance
     *
     * @param withdrawal
     *
     * @throws InsufficientFundsException if the withdrawal will put the current
     *                                    balance below $0
     */
    public void withdrawal(double withdrawal) throws InsufficientFundsException {
        // TODO 7 IMPLEMENT THIS METHOD
        if(withdrawal>this.balance){
            throw new InsufficientFundsException("You do not have sufficient balance");
        }else{
            this.balance-=withdrawal;
        }
    }

    /***
     *
     * Given a rate and a number of months, calculate the projected balance.
     *
     * Example Balance = $100
     * Months = 4
     * Rate = 0.01 (1%)
     *
     * After 1 Month Balance = $101 (100 + (100 * 0.01))
     * After 2 Months Balance = $102.01 (101 + (101 * 0.01))
     * After 3 Months Balance = $103.0301 (102.01 + (102.01 * 0.01))
     * After 4 Months Balance = $104.060401 (103.0301 + (103.0301 * 0.01))
     *
     * return 104.060401
     *
     * @param rate (Will always be positive)
     * @param months (Will always be greater than 1)
     * @return estimated balance after "months" months of cumulative interest at
     *         rate "rate"
     */
    public double projectedBalance(double rate, int months) {
        // TODO 8 IMPLEMENT THIS METHOD
        double currentBalance=this.balance;
        for(int month=0;month<=months;month++){
            currentBalance+=currentBalance*rate;
        }
        return currentBalance;
    }
}
Add a comment
Know the answer?
Add Answer to:
For this assignment, you should modify only the User.java file. Make sure that the InsufficientFundsException.java file...
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
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