Photo AI

The local supermarket has a loyalty card system where customers can receive rewards depending on the number of visits, the number of loyalty points gathered and the percentage of health food bought - NSC Information Technology - Question 2 - 2017 - Paper 1

Question icon

Question 2

The-local-supermarket-has-a-loyalty-card-system-where-customers-can-receive-rewards-depending-on-the-number-of-visits,-the-number-of-loyalty-points-gathered-and-the-percentage-of-health-food-bought-NSC Information Technology-Question 2-2017-Paper 1.png

The local supermarket has a loyalty card system where customers can receive rewards depending on the number of visits, the number of loyalty points gathered and the ... show full transcript

Worked Solution & Example Answer:The local supermarket has a loyalty card system where customers can receive rewards depending on the number of visits, the number of loyalty points gathered and the percentage of health food bought - NSC Information Technology - Question 2 - 2017 - Paper 1

Step 1

2.1.1 Write code for a constructor method to receive the card number, cellphone number and loyalty points gathered as parameter values.

96%

114 rated

Answer

In the constructor for the CardHolder class, we need to define three parameters: cardNumber, cellNumber, and loyaltyPoints. We will then assign these parameters to the corresponding attributes of the class, ensuring to initialize the numVisits to 0 and set the healthLevel to 'S' for Silver level.

public CardHolder(String cardNumber, String cellNumber, int loyaltyPoints) {
    this.cardNumber = cardNumber;
    this.cellNumber = cellNumber;
    this.loyaltyPoints = loyaltyPoints;
    this.numVisits = 0;
    this.healthLevel = 'S';
}

Step 2

2.1.2 Write code for a method to setNumVisits that will be able to set the attribute for the number of visits with a value received as a parameter.

99%

104 rated

Answer

The setNumVisits method should accept an integer as a parameter to update the numVisits attribute. This method does not return a value.

public void setNumVisits(int visits) {
    this.numVisits = visits;
}

Step 3

2.1.3 Write code for a method called increaseLoyaltyPoints that receives the total amount for this month as a parameter.

96%

101 rated

Answer

The increaseLoyaltyPoints method should calculate the earned points based on the amount spent during the month. For every R4 spent, one loyalty point is awarded. Therefore, we will take the input totalAmount, divide it by 4, and then add the calculated points to the loyaltyPoints attribute.

public void increaseLoyaltyPoints(double totalAmount) {
    int pointsEarned = (int) (totalAmount / 4);
    this.loyaltyPoints += pointsEarned;
}

Step 4

2.1.4 Write code for a method called updateHealthLevel that will receive the total amount spent on health food and total amount as parameters.

98%

120 rated

Answer

The updateHealthLevel method should first calculate the percentage of the amount spent on health food against the total amount. Based on the value of this percentage, we will update the healthLevel.

public void updateHealthLevel(double healthAmount, double totalAmount) {
    double percentage = (healthAmount / totalAmount) * 100;
    if (percentage < 10) {
        this.healthLevel = 'P';
    } else if (percentage >= 40) {
        this.healthLevel = 'G';
    } else {
        this.healthLevel = 'S';
    }
}

Step 5

2.1.5 Write code to complete the isCorrect method provided.

97%

117 rated

Answer

The isCorrect method should verify access code by removing zeros from the cellphone number, summing the remaining digits based on the specified rules, and comparing the sum to the access code value.

public boolean isCorrect(String accessCode) {
    String cleanedNumber = this.cellNumber.replaceAll("0", "");
    int sum = 0;
    int length = cleanedNumber.length();
    
    for (int i = 0; i < length; i++) {
        if (i % 2 == 0 && i + 1 < length) {
            sum += Integer.parseInt(cleanedNumber.charAt(i) + "" + cleanedNumber.charAt(i + 1));
            i++;  // Skip next digit
        } else {
            sum += Integer.parseInt(cleanedNumber.charAt(i) + "");
        }
    }
    
    return sum == Integer.parseInt(accessCode);
}

Step 6

2.1.6 Write code to indicate whether the card holder is a STAR shopper or not.

97%

121 rated

Answer

To indicate if the card holder is a STAR shopper, the identifyStarShopper method should check if the loyaltyPoints are more than 2000 or if the numVisits are more than 10. It will return a string indicating the result.

public String identifyStarShopper() {
    if (this.loyaltyPoints > 2000 || this.numVisits > 10) {
        return "STAR shopper";
    }
    return "Not a STAR shopper";
}

Step 7

2.2.1 Button – [2.2.1 – Check access code]

96%

114 rated

Answer

When a card number is selected from the combo box, the corresponding cellphone number, card number, and loyalty points are displayed. The user must enter an access code and the program will validate it.

public void validateAccessCode() {
    // Extract card number and check access code
    String chosenCard = comboBox.getSelectedItem();
    // Call method to validate the card
    if (objCardHolder.isCorrect(accessCodeInput.getText())) {
        // Code to process further
    }
}

Step 8

2.2.2 Button – [2.2.2 – Display card holder details]

99%

104 rated

Answer

Using the toString method, we can display the details of the card holder in the designated output area. This should also incorporate the result from identifyStarShopper.

public void displayCardHolderDetails() {
    outputArea.setText(objCardHolder.toString() + "\n" + objCardHolder.identifyStarShopper());
}

Join the NSC students using SimpleStudy...

97% of Students

Report Improved Results

98% of Students

Recommend to friends

100,000+

Students Supported

1 Million+

Questions answered

;