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
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();
}
}
Comments
Leave a comment