Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help! Rewrite without using == to compare. public class StarbucksAccount { //Do not modify the attributes or add new ones. private String firstName; private String

Help! Rewrite without using == to compare. image text in transcribed public class StarbucksAccount { //Do not modify the attributes or add new ones. private String firstName; private String lastName; private String membershipID; private double funds; private boolean hasReward; //true when account has reward available for use //Constructor to initialize variables public StarbucksAccount(String fn, String ln, String m, double funds) { firstName = fn; lastName = ln; membershipID = m; this.funds = funds; hasReward = false; //default } //Modifies the hasReward variable to indicate a reward i available public void earnedReward() { hasReward = true; } //----------Code below here--------------------

/* * 1. Write an accessor method for the funds attribute. You must use proper * naming convention for the method to receive credit */ public double getFunds() { return funds; }

/* * 2. Write an accessor method for the hasReward attribute: This method should * be named hasReward() */ public boolean hasReward() { return hasReward; }

/* * 3. Write a method called printAccountInfo that prints out the information in * the format below: Pay close attention to the last line of output. The example * below is the output for the joesAccount object created for you in * StarbucksAccountDemo.java * * Name: Joe King Membership ID: 98765432 Available funds: $10.0 Does not have a * reward. * * If joesAccount had a reward (hasReward evaluated to true), the following * should be printed: * * Name: Joe King Membership ID: 98765432 Available funds: $10.0 Has a reward. * * Hint: pay attention to how firstName and lastName print. */ public void printAccountInfo() { System.out.println("Name: " + firstName + " " + lastName); System.out.println("Membership ID: " + membershipID); System.out.println("Available funds: $" + funds); if (hasReward) { System.out.println("Has a reward."); } else { System.out.println("Does not have a reward."); } }

/* * 4. Write a method called addFunds that accepts a double parameter to increase * the funds by. Starbucks only allows users to increase their funds by 10.00, * 20.00, or 50.00 dollars. Note - this method stub is provided for you, * complete the code so that it works as follows: Increase the current funds by * the requested amount only when the deposit amount is one of the allowed * values specified above. otherwise - print the following error: * "Invalid deposit amount." */

public void addFunds(double deposit) { // Complete the code. if (deposit == 10 || deposit == 20 || deposit == 50) { funds = funds + deposit; } else { System.out.println("Invalid deposit amount."); } }

/* * 5. Write a method called makePurchase that accepts a double purchaseAmount * Note - this method stub is provided for you, complete the code so that it * works as follows: Ensure that purchaseAmount is a positive number, otherwise * print the error: "Purchase amount must be greater than 0." If purchase amount * is valid, deduct that amount from your funds only when there are enough funds * to cover the purchaseAmount - funds should never be negative (but could be * 0). Otherwise - print the following error message: * "Purchase amount exceeds funds." */ public void makePurchase(double purchaseAmount) { // Complete the code. if (purchaseAmount funds) { System.out.println("Purchase amount exceeds funds."); } else { funds = funds - purchaseAmount; } }

/* * 6. Write a method called generateUsername that accepts no parameters and * returns a username in the following format: first letter of the first name * (lowercase) first three letters of the last name (lowercase), and last three * digits of the membershipID Example1: firstName is Joe, lastName is King, * membershipID is 98765432. This method would return: jkin432 Example2: * firstName is Bill, lastName is Bob, membershipID is 58991 This method would * return: bbob991 * * You can assume firstName, lastName, and membershipID will have lengths of at * least 3. * * (Hint: charAt returns a char - you can't store it directly in a String!) * (Hint2: The String class has a toLowerCase method!) */ public String generateUsername() { String userName = Character.toString(firstName.toLowerCase().charAt(0));

userName = userName + lastName.toLowerCase().substring(0, 3);

userName = userName + membershipID.substring(membershipID.length() - 3);

return userName; } }

Current file: StarbucksDemo.java Load default template... dblic class StarbucksDemo [{ public static void main(String[] args) \{ StarbucksAccount joesAccount = new StarbucksAccount("Joe", "King", "98765432", 10.0); //Test your code here with the above object and some of your own! /* //Uncomment for Step 7. System.out.println("Please enter your option:"); System.out.println("\t1. Print Account Info"); System.out.println("\t2. Generate Username"); System.out.println("\t3. Deposit Funds"); System.out.println("\t4. Make Purchase")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions