Photo AI
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 perce... show full transcript
Step 1
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
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
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
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
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
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
Answer
The event handler for this button should:
private void checkAccessCode() {
String selectedCardNumber = comboBox.getSelectedItem().toString();
String accessCode = accessCodeField.getText();
if (objCardHolder.isCorrect(accessCode)) {
// Process data
} else {
// Show error message
}
}
Step 8
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);
}
Report Improved Results
Recommend to friends
Students Supported
Questions answered