I want to create a method in my enumeration?
java
Solution
Enums are quite powerful to keep static data in one place. You can do something like that:
public enum CustomerType {
Retail(.1, "Retail customer"),
College(.2, "College customer"),
Trade(.3, "Trade customer");
private final double discountPercent;
private final String description;
private CustomerType(double discountPercent, String description) {
this.discountPercent = discountPercent;
this.description = description;
}
public double getDiscountPercent() {
return discountPercent;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return description;
}
}
Problem
I want to add a `toString` method to my `CustomerType` enumeration. My class returns a `System.out.println()` discount percent message if depending on my `cutomerType` which is now .20 because it is customerType college. I am new to enumerations and I want to be able to add a `toString` method to my enumeration that prints "College customer" depending on the customer type. Am having some trouble accomplishing this? What exactly am I doing wrong? Hear's my Class: ``` import java.text.NumberFormat; public class CustomerTypeApp { public static void main(String[] args) { // display a welcome message System.out.println("Welcome to the Customer Type Test application\n"); // get and display the discount percent for a customer type double Customer = getDiscountPercent(CustomerType.College); NumberFormat percent = NumberFormat.getPercentInstance(); String display = "Discount Percent: " + percent.format(Customer); System.out.println(display); } // a method that accepts a CustomerType enumeration public static double getDiscountPercent (CustomerType ct) { double discountPercent = 0.0; if (ct == CustomerType.Retail) discountPercent = .10; else if (ct == CustomerType.College) discountPercent = .20; else if (ct == CustomerType.Trade) discountPercent = .30; return discountPercent; } } ``` and this is my enumeration: ``` public enum CustomerType { Retail, Trade, College; public String toString() { String s = ""; if (this.name() == "College") s = "College customer"; return s; } } ```