Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm almost done making my application, but I have a slight problem. I have a class(UserChoices) which contains multiple variables and functions and I was planning to use the class in a switch statement. But the statement is wrong, it says 'Incompatible type: UserChoices cannot be converted to 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");
       }
       
    }
}


What I have tried:

I tried using switch(choices == 1),this worked last time in another project. But not this one.
Posted
Updated 11-Mar-17 23:58pm
v3

1 solution

If you look at the documentation: The switch Statement (The Java™ Tutorials)[^] it's pretty clear on the subject:
Quote:
A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer
Since your class doesn't meet any of those criteria, you cannot use it in a switch.

To be honest, what you are doing looks pretty wrong: your class that deals with the account manipulation (deposits, withdrawals, and so forth) shouldn't be directly interfacing to the user: what if there are two different accounts? How would the user know which one he is affecting? The user interface should be separate from the "account mechanics", and should just pass info to the account handling class once it's been acquired and validated.
 
Share this answer
 
Comments
NexGen39 12-Mar-17 5:59am    
Okay, so how do you suggest I do it, can I still use switch statement or do I have to change it ? Thanks for pointing out the account manipulation thing.
OriginalGriff 12-Mar-17 6:07am    
Well you can't use your class in a switch - but if you revise your design properly then you don't need to: you use the switch on the using input to select which function you call on the class instance instead. Switch is the right thing to use, but it's the rest of the design that needs rework!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900