Использование класса в операторе switch
Я почти закончил писать заявление, но у меня есть небольшая проблема. У меня есть класс (UserChoices), который содержит несколько переменных и функций, и я планировал использовать этот класс в операторе switch. Но это утверждение неверно, оно говорит: "несовместимый тип: UserChoices не может быть преобразован в int".
package bankapp; import java.util.*; import java.text.*; class UserChoice{ //Variables for logging in private String user, pass; //username and password where users type in private String username = "Kate", password = "abc456"; //username and password of the account //Variables for loan calculator private int amount, tenure; //the amount and duration of loan private double interest, MonthlyInterest; //the annual interest and monthly interest double payment; //monthly payment private int choose; //Variables for deposit & withfraw private int balance, Amount; //balance in the account & the amount of deposit/withdrawal private String acc, pin; //account number & PIN number private String AccNumber = "4344789", PinNumber = ""; //account number & PIN number of the account Scanner scanner = new Scanner(System.in); public void SignIn(){ //For logging in System.out.println("Enter your username"); user = scanner.next(); System.out.println("Enter your password"); pass = scanner.next(); if ((user .equals(username)) && (pass .equals(password))) System.out.println("You've successfuly signed in"); else System.out.println("Sign in details incorrect"); } public void Loan(){ //Personal Loan and Housing Loan System.out.println("Enter the amount you want to borrow"); amount = scanner.nextInt(); System.out.println("Enter the tenure of your loan"); tenure = scanner.nextInt(); System.out.println("Enter the interest rate as a decimal"); interest = scanner.nextDouble(); MonthlyInterest = Math.pow(1+(interest/12), - tenure); payment = (amount * (interest/12)) / (1 - MonthlyInterest); DecimalFormat decimal = new DecimalFormat("##.##"); System.out.println("This is the amount you have to pay monthly :" + decimal.format(payment)); System.out.println("-------------------------------"); System.out.println("Do you wish to apply for the loan \n1 Yes \n2 No"); choose = scanner.nextInt(); if (choose ==1) System.out.println("You have applied for the loan"); else System.out.println("Your application have been cancelled."); } public void Deposit(){ //To deposit into account System.out.println("Enter account number"); acc = scanner.next(); if (acc .equals(AccNumber)) { System.out.println("Enter the amount you wish to deposit"); amount = scanner.nextInt(); } else System.out.println("Error: account number invalid"); } public void withdraw(){ //To withdraw from account System.out.println("Enter pin number"); pin = scanner.next(); if (pin .equals (PinNumber)) { System.out.println("How much would you like to withdraw ?"); amount = scanner.nextInt(); } else System.out.println("Error: Invalid PIN number"); } } public class BankApp { public static void main(String[] args) { UserChoice choices = new UserChoice(); System.out.println("What would you like to do ? \n1 Sign in \n2 Apply for a Personal Loan \n3 Apply for a Housing Loan \n4 Deposit \n5 Withdraw"); switch (choices){ case 1: choices.SignIn(); break; case 2: choices.Loan(); break; case 3: choices.Loan(); break; case 4: choices.Deposit(); break; case 5: choices.withdraw(); break; default: System.out.println("Error: Invalid entry"); } } }
Что я уже пробовал:
Я попробовал использовать switch (choices == 1), это сработало в прошлый раз в другом проекте. Но только не этот.