简体   繁体   中英

java having trouble with multiple classes

so I'm trying to use a switch so that when i click on a range of 1 to 4, each one is directed to a class and performs its function. The choice "1" should asks for the user to input the id, name, other name and marks, then calculate it's average. The second class should then display all the information and I'm not sure how to do it.

Here is my main code:

public class lab3q1 {

    public static void main (String args[]){

        Scanner sc = new Scanner(System.in);

        Entries entriesobject = new Entries(); //object declaration
        display displayobject = new display(); //object declaration
        displayall displayallobject = new displayall(); //object declaration
        sortdata sortdataobject = new sortdata();

        System.out.println("1. Add new entries: ");
        System.out.println("2. Display an entry: ");
        System.out.println("3. Display all entries: ");
        System.out.println("4. Sort Data: ");
        System.out.println("5. Exit: ");
        int s = sc.nextInt();

        switch(s){

                case 1:{
                    if(s==1)
                    try{
                        entriesobject.method0();
                    }
                    catch(Exception e){
                        System.out.println("You can't do that");

                    }
                }
                case 2:{
                    if(s==2){
                        try{
                        displayobject.method();
                        }
                        catch(Exception e){
                            System.out.println("You can't do that");
                        }
                    }

                }

                case 3:{
                    if(s==3){
                        try{
                        displayallobject.method2();
                    }
                        catch(Exception e){
                            System.out.println("You can't do that");
                        }   
                    }
                }
                case 4:{
                    if(s==4){
                        try{
                            sortdataobject.method3();
                        }
                            catch(Exception e){
                                System.out.println("You can't do that");
                    }   
                    }
                }
                case 5:{
                    if(s==5){break;}
                }               
   }
  }
}

Here is the first class:

public class Entries {    
    public void method0(){          
        int total=0,total2;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the student id: ");
        int id = sc.nextInt();
        sc.nextLine();
        System.out.println("Enter the student name: ");
        String name = sc.nextLine();
        System.out.println("Enter the student other names: ");
        String othername = sc.nextLine();

        for(int i=1;i<=4;i++){

        System.out.println("Enter the student marks" +(i));
        int mark = sc.nextInt();
        total += mark;
        total2 =total/4;
        System.out.println("The average marks is: "+total2); 
        }           
    }       
}

And here is my second class:

public class display {
  public void method() {            
        int n;          
        Scanner sc = new Scanner(System.in); 
        System.out.println("Here is the student id: ");         
    }    
}

As you can see i can't seem to link them.

To answer your question: when different classes want to know about data in other classes (aka fields of other objects), you need ways to access that, like:

public class Student {
  private int id;
  ... methods to put a value into id

  public int getId() { return id; }

and then some other class that has one more Student objects can do.

Beyond that: you are getting your "separation of concerns" wrong. You should have one class that uses a scanner to "collect" data from the user. This class creates various other objects; and puts the data from the user into those objects.

All your other classes do not have / need a scanner object. They get their data, for example as parameters to their constructor.

System.out.println("The id is: " + someStudentObject.getId());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM