Photo AI
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 ... show full transcript
Step 1
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
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
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
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
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
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
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
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());
}
Report Improved Results
Recommend to friends
Students Supported
Questions answered