Question

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

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\n");

        // 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.

Homework Answers

Answer #1

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\n");

// 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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has...
(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has a firstName, lastName, customerNumber, emailAddress. You can add some more fields if you want to, but those 4 are a minimum. Override the equals method, and the toString method from the Object class. Implement the Comparable interface. In the main method, create some instances of the Customer class, and demonstrate the use of the accessor and mutator methods, as well as the compareTo method.
create an array of Vehicle references. Within the application, you assign Sailboat objects and Bicycle objects...
create an array of Vehicle references. Within the application, you assign Sailboat objects and Bicycle objects to the same array. Then, because the different object types are stored in the same array, you can easily manipulate them by using a for loop. 1. Open a new file, and then enter the following first few lines of the VehicleDatabase program: import javax.swing.*; public class VehicleDatabase { public static void main(String[] args) { 2. Create the following array of five Vehicle references...
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
--USING C# ONLY-- You will create your own on-line shopping store in the console application. You...
--USING C# ONLY-- You will create your own on-line shopping store in the console application. You can choose different products to sell in your store (at least 8 products). You will create an product inventory text file. The program will display two options at the beginning of the program, Customer and Manager. Customer: If this is a new customer, the program will allow the customer to register with their information and assign a unique ID number to the new customer....
a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class...
a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class named PhoneCall that includes a String field for a phone number and a double field for the price of the call. Also include a constructor that requires a phone number parameter and that sets the price to 0.0. Include a set method for the price. Also include three abstract get methods - one that returns the phone number, another that returns the price of...
C# Programming Using the Example provided in this Week Folder,Create a Console application that solve the...
C# Programming Using the Example provided in this Week Folder,Create a Console application that solve the following problem: The heating system in a school should be switched on if the average temperature is less than 17 degrees Celsius. The average temperature is found from the temperatures in the Math, English and IT departments. You are required to write a program that allows the user to input 3 temperatures. The program calculates and displays the average temperature and then displays "heating...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
Create a client for your BankAccount classcalled BankAccountClient.java.    This a separate file from the BankAccount file....
Create a client for your BankAccount classcalled BankAccountClient.java.    This a separate file from the BankAccount file. Both files must be in the same directory to compile. Display all dollar amounts using the DecimalFormat class. Create an account Ask the user for the type of account, the bank account number, the amount that they will deposit to start the account. Set interest earned to 0. Print the account information using an implicit or explicit call to toString Update an account Use...
please use linux/unix command terminal to complete, step 1 1-create a new directory named by inlab4...
please use linux/unix command terminal to complete, step 1 1-create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() { char str[100]; /*Buffer to hold reversed string */ reverse("cat", str); /*Reverse the string "cat" */ printf("reverse(\"cat\")=%s\n", str); /*Display result */ reverse("noon", str); /*Reverse the string "noon" */ printf("reverse(\"noon\")=%s\n", str); /*Display result */ } reverse(char *before,...
the coding language is java The start of Polymorphism Assignment Outcome: Student will demonstrate the ability...
the coding language is java The start of Polymorphism Assignment Outcome: Student will demonstrate the ability to apply polymorphism in a Java program. Program Specifications: Design the following program: A person has a name, and age. An Employee is a person. An Employee has a company and position. A SoftwareEngineer is an employee and a person. A SoftwareEngineer has a rank (Junior, Middle, and Senior) A SoftwareEngineer either C programmer, Jave programmer or both. A Manager is an Employee and...