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
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
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
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
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
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
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
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
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;
}
Report Improved Results
Recommend to friends
Students Supported
Questions answered