Skip to main content

Learning about Code Structure

Section 1.16 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.16.1. Remove repeated code from branches part 2
Click on the checkpoint and answer the question.

Checkpoint 1.16.2.

Improve the style of printProductStatus(). The original function is below, followed by a version with two blanks. Fill in the blanks so that the original functionality is preserved, but the code is easier to maintain.
public void printProductStatus(int quantity) {
	String status = "";
   
	if (quantity > 100) {
   	    status = "High Stock";
   	    System.out.println("Status: " + status);
	} else if (quantity > 0) {
   	    status = "In Stock";
   	    System.out.println("Status: " + status);
	} else {
   	    status = "Out of Stock";
   	    System.out.println("Status: " + status);
	}
}
public void printProductStatus(int quantity) {
    String status = ""; 

    if (quantity > 100) {
        status = "High Stock";
    } else if (quantity > 0) {
        status = "In Stock";
    } else {
    	status = "-----Blank1-----";
    }

    System.out.println(------Blank2-----);
}
Put your responses here:
Blank 1:
Blank 2:
You have attempted 1 of 3 activities on this page.