Create and use an enumeration create an enumeration and use

Create and use an enumeration
create an enumeration and use it in a test application.


1. Import the project named ch13 ex 2 Enumeration that\'s in the ex starts folder, 2. Create an enumeration named CustomerType. This enumeration should contain constants that represent three types of customers: retail. trade, and college.

Use an enumeration

3. open the Customer Type App class. Then, add a method to this class that returns a discount percent C10 for retail. 30 for trade. and.20 for college) depending on the CustomerType variable that\'s passed to it.

The Customer Type App class file

package murach.customertype;

public class CustomerTypeApp {

    public static void main(String[] args) {
        // display a welcome message
        System.out.println(\"Welcome to the Customer Type Test application\ \");

        // get and display the discount percent for a customer type
        // display the value of the toString method of a customer type
    }

    // a method that accepts a CustomerType enumeration
}

4. Add code to the main method that declares a customer Type variable, assigns one of the customer types to it, gets the discount percent for that customer type, and displays the discount percent. It should display a message like this to the console:
discountpercent 0.3

5- Run the application to be sure that it works correctly.

Modify the toString method of an enumeration

6. Add a statement to the main method that displays the string returned by the toString method of the customer type. Then, run the application again to see the result of this method. Depending on the customer type, it should display the name of the constant like this:
toString: TRADE

7. Add a tostring method to the Customer Type enumeration. Depending on the customer type, this method should return a string that contains\"Retail\"Trade,\" or\"College\" like this:
tostring Trade

8. Run the application again to view the results of the toString method.

Solution

import java.util.*;
//Enumeration of CustomerType
enum CustomerType
{
   retail, trade, college
}

public class CustomerTypeApp
{
   static CustomerType ct;
  
   public static void main(String[] args)
   {
// Display a welcome message
System.out.println(\"Welcome to the Customer Type Test application\ \");

// get and display the discount percent for a customer type
CustomerTypeApp first = new CustomerTypeApp();
first.accept();
first.display();
  
// Display the value of the toString method of a customer type
System.out.println(ct);
}
  
   //Method to accept data
   void accept()
   {
       Scanner sc = new Scanner(System.in);
       System.out.println(\"Enter Customer type (retail / trade / college)\");
       String data = sc.next();
       ct = CustomerType.valueOf(data);
   }
  
   //Overloaded toString method
   public String toString(CustomerType ct)
   {
      
       String msg = ct.toString();      
       return msg;
   }
  
// A method that accepts a CustomerType enumeration
   public void display()
   {
switch (ct)
{
case retail:
System.out.println(\"Discount percent = \" + 0.1);
break;
  
case trade:
   System.out.println(\"Discount percent = \" + 0.3);
break;

case college:
   System.out.println(\"Discount percent = \" + 0.2);
break;
}//End of switch
}//End of method
}//End of class

Sample run 1:

Welcome to the Customer Type Test application

Enter Customer type (retail / trade / college)
retail
Discount percent = 0.1
retail

Sample run 2:

Welcome to the Customer Type Test application

Enter Customer type (retail / trade / college)
college
Discount percent = 0.2
college

Create and use an enumeration create an enumeration and use it in a test application. 1. Import the project named ch13 ex 2 Enumeration that\'s in the ex starts
Create and use an enumeration create an enumeration and use it in a test application. 1. Import the project named ch13 ex 2 Enumeration that\'s in the ex starts
Create and use an enumeration create an enumeration and use it in a test application. 1. Import the project named ch13 ex 2 Enumeration that\'s in the ex starts

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site