CustomerType
열거 형에 toString
메서드를 추가하고 싶습니다. 내 에 따라 달라지면 내 클래스는 System.out.println()
할인율 메시지를 반환합니다. 이는 고객 유형 대학이기 때문에 .20입니다. 열거 형을 처음 사용하고 고객 유형에 따라 "대학 고객"을 인쇄하는 열거 형에 toString
메서드를 추가 할 수 있기를 원합니다. 이 문제를 해결하는 데 어려움이 있습니까? 정확히 내가 뭘 잘못하고 있니?열거 형에 메서드를 만들려고합니까?
듣고는 내 클래스 :
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;
}
}
이 내 열거 :
public enum CustomerType {
Retail,
Trade,
College;
public String toString() {
String s = "";
if (this.name() == "College")
s = "College customer";
return s;
}
}
무엇이 오류입니까? – brandonsbarber
한눈에이 코드가 잘 보입니다. – Armand