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.
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...
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...
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...
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 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...
C# Step 1: Create a Windows Forms Application. Step 2: Create a BankAccount class. Include: Private...
C# Step 1: Create a Windows Forms Application. Step 2: Create a BankAccount class. Include: Private data fields to store the account holder's name and the account balance A constructor with 0 arguments A constructor with 1 argument (account holder's name) A constructor with 2 arguments(account holder's name and account balance) Public properties for the account holder's name and the account balance. Do not use auto-implemented properties. A method to increase the balance (deposit) A method to decrease the balance...
Create a Software Application for XYZ Bank that allows its members to access their bank account...
Create a Software Application for XYZ Bank that allows its members to access their bank account information through an “ATM like” interface. The Menu Options should include: checking the account balance, debiting the account and crediting the account, along with an option to exit the program. Create an Account Class to represent the customers’ bank accounts. Include a data member of type float to represent the account balance. Provide a constructor that receives an initial balance and uses it to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT