Answer to Question #294446 in Java | JSP | JSF for Seve

Question #294446

Enum


Create a class for enum named ‘TypeOfAccount’. And declare the following enum:


TypeOne 

TypeTwo 

TypeThree 


TypeOne represents “Saving Account”, TypeTwo for “Salary Account” and TypeThree is “Checking Account”


Also create a class with Main Method, where you will do the following operations:


Allows the user to input a TypeAccount enum

Displays the value that the enums represents

If the input enum is incorrect, display an error message


1
Expert's answer
2022-02-06T12:30:58-0500


import java.util.Scanner;


enum TypeOfAccount {
	TypeOne, TypeTwo, TypeThree;


	public static TypeOfAccount fromInteger(int x) {
		switch (x) {
		case 1:
			return TypeOne;
		case 2:
			return TypeTwo;
		case 3:
			return TypeThree;
		}
		return null;
	}
}


class App {


	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);


		// Allows the user to input a TypeAccount enum
		System.out.print("Input a TypeAccount enum 1,2,3: ");
		int typeAccountEnum = keyBoard.nextInt();
		// Displays the value that the enums represents
		if (TypeOfAccount.TypeOne == TypeOfAccount.fromInteger(typeAccountEnum)) {
			System.out.println("Saving Account");
		} else if (TypeOfAccount.TypeTwo == TypeOfAccount.fromInteger(typeAccountEnum)) {
			System.out.println("Salary Account");
		} else if (TypeOfAccount.TypeThree == TypeOfAccount.fromInteger(typeAccountEnum)) {
			System.out.println("Checking Account");
		}else {
			System.out.println("WRONG selection");
		}


		


		keyBoard.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS