Demo - Customer - The if-else-if Statement
The if-else-if
statement tests a series of conditions. It is often simpler to test a series of conditions with the if-else-if
statement than with a set of nested if-else
statements.
package CustomerDemo;
// Author: Kevin Roark
// Customer class
public class Customer {
private String firstName;
private String lastName;
private int points;
private String rewardLevel;
//Constructor
public Customer(String firstName, String lastName, int points) {
this.firstName = firstName;
this.lastName = lastName;
this.points = points;
this.rewardLevel = "NA";
}
public Customer() {
this.firstName = "Unknown";
this.lastName = "Unknown";;
this.points = 0;
this.rewardLevel = "NA";
}
//Getters and Setters
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
public String getRewardLevel() {
if(points >= 5000)
return "Platinum";
else if (points >= 3000)
return "Gold";
else if (points >= 1500)
return "Silver";
else return "Bronze";
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFullName()
{
return firstName + " " + lastName;
}
//Public Helper Functions
public String printInfo()
{
String myReturn = "Name: " + getFullName() + "\n";
myReturn += "Level: " + getRewardLevel() + "\n";
return myReturn;
}
}
The Java code you provided defines a Customer
class within the CustomerDemo
package. Here's what this class entails:
Private Instance Variables:
firstName
,lastName
,points
, andrewardLevel
are the four private instance variables in theCustomer
class.firstName
andlastName
are strings representing the customer's first and last names.points
is an integer that might represent the points a customer has earned (possibly through purchases, rewards, etc.).rewardLevel
is a string that might represent the level of the customer based on their points (like "Platinum", "Gold", "Silver", "Bronze").
Constructors: There are two constructors in the class.
The first constructor accepts three parameters:
firstName
,lastName
, andpoints
. It assigns these parameters to the respective instance variables and setsrewardLevel
as "NA".The second constructor is a no-argument constructor that initializes the
firstName
andlastName
to "Unknown",points
to 0, andrewardLevel
to "NA".
Getters and Setters: These methods allow controlled access to the instance variables.
Getters (
getLastName()
,getPoints()
,getRewardLevel()
,getFirstName()
) return the current value of the respective instance variable.Setters (
setLastName(String lastName)
,setPoints(int points)
,setFirstName(String firstName)
) are used to set or update the value of the respective instance variable.
Other methods:
getRewardLevel()
method returns the reward level of the customer based on their points. This method includes an if-else structure that checks the customer's points and assigns a reward level ("Platinum", "Gold", "Silver", "Bronze") accordingly.getFullName()
method returns the full name of the customer by concatenating thefirstName
andlastName
with a space in between.printInfo()
is a helper function that returns a string displaying the customer's full name and their reward level.
This class is a representation of a customer in a system that likely tracks customer rewards or a similar customer loyalty program. The system appears to determine a customer's reward level based on the number of points they have accumulated.
Driver:
package CustomerDemo;
// Author: Kevin Roark
// Customer class Demo
import java.util.Scanner; // Import the Scanner class to allow input from the user
public class CustomerDriver {
public static void main(String[] args)
{
Customer myCustomer = new Customer();
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your first Name: ");
myCustomer.setFirstName(keyboard.nextLine());
System.out.print("Enter your last Name: ");
myCustomer.setLastName(keyboard.nextLine());
System.out.print("Enter your points earned: ");
myCustomer.setPoints(keyboard.nextInt());
System.out.println(myCustomer.printInfo());
}
}
COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark