lingar
lingar

Reputation: 613

How to do Combination of condtions from some methods

Hi I have googled about this issue but don’t found something useful. I have one userUI object that represent the UI of regular user area. And I have AdminUI object that extends that, and I want to add the admin option to that class in the most right way without the need to make management to the shared options of that.

Trying to make that (do it like example for the convenience....

Is that the right way or there is more efficient and simple way.

Feeling like I miss something...

This is my code :

package main.java.draft;

import java.util.Scanner;

//Stackoverflow question
public class SOQ {

    public static void main(String[] args) {

        //How to do combination of some conditions. 
        //Trying on user. 
        UserUI userUI = new UserUI();

        System.out.println("If User regualr just 1-7, if admin 8-12 too");
        Scanner sc = new Scanner (System.in);
        int input = sc.nextInt();
        userUI.manageOptions(input);

        //trying to do that on admin 
        System.out.println("If User regualr just 1-7, if admin 8-12 too");
        AdminUI adminUI = new AdminUI();
        input = sc.nextInt();
        adminUI.manageOptions(input);

        sc.close();         
    }               
}


class UserUI{

    boolean admin = false;

    public void manageOptions(int  input){

        if(input > 0 && input < 8){             
            userMethods(input);
        }

        else if((input > 7 && input < 13) && admin){                
            AdminUI adminUI = new AdminUI();
            adminUI.manageAdminOptions(input);              
        }

        else{               
            System.out.println("Not proper option");
        }           
    }

    public void userMethods(int input){         
        System.out.println("User Methods - method " + input);
    }               
}

class AdminUI extends UserUI{

    public AdminUI(){           
        admin = true;
    }

    public void manageAdminOptions(int input){
        adminMethods(input);

    }

    public void adminMethods(int input){
        System.out.println("Admin Methods - method " + input);          
    }               
}

**

More details about the case.

**

This is the output - user screen: Please choose one of the following options:

1- Change messages. 2- change password. 3- Change Email. 4- Show my details. (with password ? – yeah ) 5- log off. 6- show messages. 7- Delete account (with password) .

This is the admin output:

Please choose one of the following options:

1- Change messages. 2- change password. 3- Change Email. 4- Show my details. (with password ? – yeah ) 5- log off. 6- show messages. 7- Delete account (with password) . 7 – show specific user by name 8 – show all users 9- delete one user . 10 – delete all users! 11- change user details (password/email)


I want the admin will extend the user because actually he's kind of user. I want to make method that make implementation for either user or admin , not make again all the shared implementation in the admin...

Upvotes: 0

Views: 65

Answers (1)

Scary Wombat
Scary Wombat

Reputation: 44854

I would change your code, so that in the main method you would create either an User or an Admin based upon the input. Then you can get rid of strange logic in the subclasses by having both subclasses implement an interface.

UserBase user = null;  // the interface
System.out.println("If User regular just 1-7, if admin 8-12 too");
Scanner sc = new Scanner (System.in);
int input = sc.nextInt();
if (input <= 7) 
{
    user = new UserUI();
}
else {
    user = new AdminUI ();
}

// then call common method
user.manageOptions();  // parameter are maybe not required ?

The subclasses would be like

//UserUI
public void manageOptions () { System.out.println("User Methods - method "); }

//AdminUI
public void manageOptions () { System.out.println("Admin Methods - method "); }

Upvotes: 2

Related Questions