Skip to main content

Practice Code Structure

Section 1.49 Code Refactoring

Checkpoint 1.49.1.

You will see a copy of the following code block in an interactive mode than you can modify and compile. We want you to improve the style of the code as an expert would view it. You must not change the code behavior. To ensure this you can run your refactored code and ensure that it passes all the test cases.
public class FinalPrice {
	public static double calculateFinalPrice(double basePrice, boolean isMember) {
		double discount = 0;
		if (isMember) {
			discount = basePrice * 0.1;
			System.out.println("Thank you for being a loyal customer!");
			System.out.println("Final price is: $" + (basePrice - discount));
		} else {
			discount = basePrice * 0.05;
			System.out.println("Join our membership for more discounts!");
			System.out.println("Final price is: $" + (basePrice - discount));
		}
		return basePrice - discount;
	}
}

Activity 1.49.1.

    Which of the following code blocks is the most similar to your final refactored code for the previous question?
  • public class FinalPrice {
        public static double calculateFinalPrice(double basePrice, boolean isMember) {
            double discountRate = 0;
            String message = "";
            
            if (isMember) {
                discountRate = 0.1;
                message = "Thank you for being a loyal customer!";
            } else {
                discountRate = 0.05;
                message = "Join our membership for more discounts!";
            }
    
            double discount = basePrice * discountRate;
            System.out.println(message);
            System.out.println("Final price is: $" + (basePrice - discount));
            
            return basePrice - discount;
        }
    }
    
  • You refactored correctly!
  • public class FinalPrice {
        public static double calculateFinalPrice(double basePrice, boolean isMember) {
            double discountRate = 0.05;
            String message = "Join our membership for more discounts!";
            
            if (isMember) {
                discountRate = 0.1;
                message = "Thank you for being a loyal customer!";
            }
    
            double discount = basePrice * discountRate;
            System.out.println(message);
            System.out.println("Final price is: $" + (basePrice - discount));
            
            return basePrice - discount;
        }
    }
    
  • You refactored correctly!
  • public class FinalPrice {
        public static double calculateFinalPrice(double basePrice, boolean isMember) {
            double discountRate = 0;
            String message = "";
            
            if (isMember) {
                discountRate = 0.1;
                message = "Thank you for being a loyal customer!";
            } else {
                discountRate = 0.05;
                message = "Join our membership for more discounts!";
            }
    
            double finalPrice = basePrice * (1 - discountRate);
            System.out.println(message);
            System.out.println("Final price is: $" + finalPrice);
            
            return finalPrice;
        }
    }
    
  • You refactored correctly!
You have attempted 1 of 3 activities on this page.