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

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

96%

114 rated

Answer

The constructor method is designed to initialize the card holder's attributes. It takes the card number, cellphone number, and loyalty points as parameters. The method definition is as follows:

public CardHolder(String cardNumber, String cellNumber, int loyaltyPoints) {
    this.cardNumber = cardNumber;
    this.cellNumber = cellNumber;
    this.loyaltyPoints = loyaltyPoints;
    this.numVisits = 0; // Initialize the number of visits to 0
    this.healthLevel = 'S'; // Initialize health level to Silver
}

Step 2

Write code for a method to set the number of visits the card holder has made this month and assign the value to the numVisitis attribute.

99%

104 rated

Answer

The mutator method setNumVisits allows for updating the number of visits based on user input:

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

Step 3

Write code for a method to increaseLoyaltyPoints that receives the total amount for this month as a parameter and increases the current number of loyalty points based on the rounded value.

96%

101 rated

Answer

The method increaseLoyaltyPoints calculates the new loyalty points based on the total spent. The implementation is as follows:

public void increaseLoyaltyPoints(double totalSpent) {
    int earnedPoints = (int) Math.round(totalSpent);
    this.loyaltyPoints += earnedPoints; // Update loyalty points
}

Step 4

Write code for a method called updateHealthLevel that will receive the total amount spent this month as a parameter.

98%

120 rated

Answer

The updateHealthLevel method updates the health level based on the percentage of total spending:

public void updateHealthLevel(double totalSpent) {
    double percentage = (totalSpent / 1500) * 100; // Assuming 1500 is the total limit for calculation
    if (percentage < 10) {
        this.healthLevel = 'G';
    } else if (percentage >= 10 && percentage < 40) {
        this.healthLevel = 'Y';
    } else {
        this.healthLevel = 'P';
    }
}

Step 5

Write code to complete the isCorrect method provided.

97%

117 rated

Answer

The isCorrect method verifies the access code against the cellphone number:

public boolean isCorrect(String accessCode) {
    String processedCellNumber = this.cellNumber.replace("0", ""); // Remove zeros
    int sum = 0;
    if (processedCellNumber.length() % 2 == 0) {
        for (int i = 0; i < processedCellNumber.length(); i += 2) {
            sum += Integer.parseInt(processedCellNumber.substring(i, i + 2));
        }
    } else {
        sum += Integer.parseInt(processedCellNumber.substring(0, 1));
        for (int i = 1; i < processedCellNumber.length(); i += 2) {
            sum += Integer.parseInt(processedCellNumber.substring(i, i + 2));
        }
    }
    return Integer.toString(sum).equals(accessCode);
}

Step 6

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

97%

121 rated

Answer

The identifyStarShopper method checks if the card holder qualifies as a STAR shopper:

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

Step 7

The user is required to select a card number from the four card numbers provided in the combo box.

96%

114 rated

Answer

The button event handler to check the access code and read data from the file could be structured as follows:

private void checkAccessCode() {
    String cardNumber = comboBox.getSelectedItem().toString();
    String accessCode = textField.getText();
    // Logic to read details from the file and validate...
    if (objCardHolder.isCorrect(accessCode)) {
        // Process the card holder details
    }
}

Step 8

Write code to use the toString method to display the card holder object data in the appropriate format.

99%

104 rated

Answer

The toString method provides a string representation of the card holder's details:

@Override
public String toString() {
    return "Card Number: " + this.cardNumber + ", Cellphone: " + this.cellNumber + ", Loyalty Points: " + this.loyaltyPoints + ", Health Level: " + this.healthLevel;
}

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

;