Skip to main content

Learning about Code Structure

Section 1.17 Rule 3) Remove repeated code and logic from conditional branches

Continue watching to learn about the third rule and answer the following question.
Figure 1.17.1. Remove repeated code from branches part 3

Checkpoint 1.17.2.

Improve the style of generateOrderSummary(). The original function is below, followed by a version with six blanks. Fill in the blanks so that the original functionality is preserved, but the code is easier to maintain.
public static void generateOrderSummary(double totalPrice, boolean isMember) {
	String message = "";

	if (totalPrice > 100) {
		message = "High order value. ";
		if (isMember) {
	    	message += "You earned 500 loyalty points!";
		} else {
	    	message += "Join our membership to earn loyalty points!";
		}
		System.out.println(message);
	} else if (totalPrice > 50) {
		message = "Moderate order value. ";
		if (isMember) {
	    	message += "You earned 200 loyalty points!";
		} else {
	    	message += "Join our membership to earn loyalty points!";
		}
		System.out.println(message);
	} else {
		message = "Low order value. ";
		if (isMember) {
	    	message += "You earned 100 loyalty points!";
		} else {
	    	message += "Join our membership to earn loyalty points!";
		}
		System.out.println(message);
	}
}
public static void generateOrderSummary(double totalPrice, boolean isMember) {
	String messagePrefix = "";
	String messageSuffix = "";
	int points = 0;

	if (totalPrice > 100) {
		messagePrefix = -----Blank 1--------;
		points = 500; 
	} else if (totalPrice > 50) {
		messagePrefix = ------Blank 2-------;
		points = 200;
	} else {
		messagePrefix = -----Blank 3--------;
		points = ---Blank 4---;
	}

	if (isMember) {
		messageSuffix = ----Blank 5----;
	} else {
		messageSuffix = ----Blank 6----;
	}

	String message = messagePrefix + messageSuffix;
	System.out.println(message);
}
Put your responses here:
Blank 1:
Blank 2:
Blank 3:
Blank 4:
Blank 5:
Blank 6:
You have attempted 1 of 3 activities on this page.