Photo AI

The local supermarket has a loyalty 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-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 system where customers can receive rewards depending on the number of visits, the number of loyalty points gathered and the perce... show full transcript

Worked Solution & Example Answer:The local supermarket has a loyalty 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

To implement the constructor method for the CardHolder class, the method should accept three parameters: cardNumber, cellPhoneNumber, and loyaltyPoints. Here's the code:

public CardHolder(String cardNumber, String cellPhoneNumber, int loyaltyPoints) {
    this.cardNumber = cardNumber;
    this.cellPhoneNumber = cellPhoneNumber;
    this.loyaltyPoints = loyaltyPoints;
    this.numVisits = 0;
    this.healthLevel = 'S'; // Initialized to Silver level
}

Step 2

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

99%

104 rated

Answer

The method setNumVisits should take an integer parameter representing the number of visits. The implementation is as follows:

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

Step 3

2.1.3 Write a method, increaseLoyaltyPoints, that receives the total amount for this month as a parameter and increases the current number of loyalty points based on the amount received.

96%

101 rated

Answer

This method will calculate loyalty points based on the amount spent. The implementation is:

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 for the month as a parameter and determine the health level attribute.

98%

120 rated

Answer

To implement updateHealthLevel, we need to categorize the health level based on the spending amount. Here's the implementation:

public void updateHealthLevel(double totalSpent) {
    int percentage = (int)((totalSpent / maximumBudget) * 100);
    if (percentage < 10) {
        this.healthLevel = 'G';
    } else if (percentage >= 10 && percentage < 40) {
        this.healthLevel = 'S';
    } else {
        this.healthLevel = 'P';
    }
}

Step 5

2.1.5 Write code to complete the isCorrect method provided. The method must test whether the access code received as a parameter is correct by applying the following instructions on the attribute for the cellphone number.

97%

117 rated

Answer

The isCorrect method will evaluate the access code against the cellphone number as follows:

public boolean isCorrect(String accessCode) {
    String modifiedCellNumber = this.cellPhoneNumber.replaceAll("0", "");
    int sum = 0;
    int length = modifiedCellNumber.length();
    for (int i = 0; i < length; i++) {
        // Add logic based on whether the index is even or odd
    }
    // Compare the computed sum with the access code here and return true or false
}

Step 6

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

97%

121 rated

Answer

The identifyStarShopper method should return a string indicating whether the card holder qualifies as a STAR shopper:

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

The event handler for this button should:

  1. Extract the selected card number from the combo box.
  2. Use the selected card number to retrieve further details.
  3. Confirm the access code using the isCorrect method.
  4. Process the data associated with the card number if the access code is valid. The code snippet looks as follows:
private void checkAccessCode() {
    String selectedCardNumber = comboBox.getSelectedItem().toString();
    String accessCode = accessCodeField.getText();
    if (objCardHolder.isCorrect(accessCode)) {
        // Process data
    } else {
        // Show error message
    }
}

Step 8

2.2.2 Button – [2.2.2 – Display card holder details]

99%

104 rated

Answer

The toString method of the CardHolder class should be called to display the card holder's details. Implement as follows:

private void displayCardHolderDetails() {
    String details = objCardHolder.toString();
    outputArea.setText(details);
}

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

;