-1

let's say in a package using Java we are using 4 files. First one say StudentModel.java ,second one say studentView.java,the third studentController.java and fourth studentMain.java.

Now the structure of studentMain.java is :

package student;

import java.util.*;



public class studentMain{
    public static void main(String[] args) {
    menu();
 }
  public static void menu(){
    Scanner sc = new Scanner(System.in);
        //fetch student record based on his roll no from the database
        studentModel model  = new studentModel();
        //Create a view : to write student details on console
        studentView view = new studentView();
    ArrayList<studentController> list = new ArrayList  <studentController>();
    int choice=1,roll1=0;boolean flag=false;
    studentController controller;
    while(true){
        System.out.println("\n************* Main Menu ********************\n");
        System.out.println("Enter 1 to input student");
        System.out.println("Enter 2 to display all students");
        System.out.println("Enter 3 to display a particular student");
        System.out.println("Enter 4 to remove a particular student");
        System.out.println("Enter 5 to Update the marks of a student"); 
         System.out.println("Enter 6 to exit program");
          System.out.println("\n*********************************************\n");  
        System.out.println("Enter choice : ");
        choice=sc.nextInt();
        switch(choice){

             case 1 :
                 list.add((new studentController(model,view)).input());
                    break;

             case 2 :controller = new studentController(model,view);
                 for(int i=0;i<list.size();i++){
                     controller=list.get(i);
                    controller.show_for_all();
                }
                break;
               case 3 :flag=false;
                  controller= new studentController(model,view);
                System.out.println("\nEnter Roll Number of Student :");
                roll1=sc.nextInt();
                for(int i=0;i<list.size();i++){
                    controller=list.get(i);
                    if(controller.compare_roll(roll1)){
                        controller.show();
                        flag=true;
                    break;
                    }
                }
                if(!flag)
                     System.out.println("Roll Number Does NOT exist !!");
                break;
            case 4:controller = new studentController(model,view);
                System.out.println("\nEnter Roll Number of Student :");
                roll1=sc.nextInt();
                flag=false;
                for(int i=0;i<list.size();i++){
                    controller=list.get(i);
                    if(controller.compare_roll(roll1)){
                        list.remove(i);
                        flag=true;
                        break;
                    }
                }
                if(!flag)
                    System.out.println("Roll Number Does NOT exist !!");
                break;
            case 5:
                controller = new studentController(model,view);
                System.out.println("\nEnter Roll Number of Student :");
                roll1=sc.nextInt();
                flag=false;
                for(int i=0;i<list.size();i++){
                    controller=list.get(i);
                    if(controller.compare_roll(roll1)){
                        controller.change_marks();
                        flag=true;
                        break;
                    }
                }
                if(!flag)
                    System.out.println("Roll Number Does NOT exist !!");
                break;
            case 6:
                System.out.println("Exiting !!!");
                System.exit(0);
                break;
            default:
                System.out.println("Wrong Input !!");
                break;
            }
        }
    }
}

And the structure of studentController.java is :

 package student;

 import java.util.*;
 import java.io.*;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;

 public class studentController {
           private studentModel model;
           private studentView view ;

 public studentController(studentModel model, studentView view){
  this.model = model;
  this.view = view;
 }

 public void show(){
  this.view.display(this.model);
 }
 public void show_for_all(){
    this.view.display_for_all(this.model);
 }
 public void input(){
        String name;//name of student
        int roll;   //roll of student
        String date;        
        int marks[]=new int [6];
        String sub[]=new String[6];
        Scanner s = new Scanner(System.in);
        int m,j;String s1;int fg=0;
        System.out.println("\t\t ENTER DETAILS OF STUDENT");
        System.out.print("Enter name:");
        name=s.nextLine();

        /*to check if a string contains digits or not*/
        for(int h=0;h<name.length();h++){
        if(name.charAt(h)=='1' || name.charAt(h)=='2'||name.charAt(h)=='3'||name.charAt(h)=='4'||name.charAt(h)=='5'||name.charAt(h)=='6'||name.charAt(h)=='7'||name.charAt(h)=='8'||name.charAt(h)=='9'){
            fg=1;
            System.out.println("name contains digits enter correctlty");
            break;
        }
    }
    if(fg!=1){
        System.out.print("Enter roll no:");
        roll=s.nextInt();
        System.out.println();
        /*system generated date*/
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy
            Date now = new Date();
            date = sdfDate.format(now);
        System.out.println("Admission Date : "+date);
        System.out.println("Enter Subject and marks :");
        /*entering the marks of the subjects*/
        for(j=0;j<5;j++){
            s1=s.next();
            m= s.nextInt();
            if(m<0 || m>100){         //marks should not > 100 or <0
                System.out.println("wrong marks entered");break;}
            sub[j]=s1;
            marks[j]=m;
            //total=total+m;
        }
        model.set_name(name);
        model.set_roll(roll);
        model.set_date(date);
        model.set_sub_marks(marks,sub);
        model.set_total();
        model.set_cgpa();
    }

  } 
       public boolean compare_roll(int rol){
          if(this.model.get_roll()==rol)
                return true;
          else 
                return false;
          }
        /*function to change the marks of any of the subjects of a  particular student*/
     public void change_marks(){
       int f,sub_code,new_marks;
      System.out.println("enter no of subjects whose marks needs to be changed");
       Scanner s = new Scanner(System.in);
       f=s.nextInt();
     while(f!=0){
        System.out.println("Enter subject codes : ");       
        sub_code=s.nextInt();   
        if(sub_code>=1 && sub_code<=5){
             System.out.println("Enter new marks");
            new_marks=s.nextInt();
            if(new_marks>=0&&new_marks<101)
            model.change_marks(sub_code,new_marks);
            else
            System.out.println("Marks OUT of Range of 0 to 100");
            }
        else
            System.out.println("Wrong subject code entered");
        f--;
      }
  }


 };

While entering new entries for the data,I am facing problems.While entering the First entry for student there is no problem.When I am entering the details of the student for 2nd entry, the details of the student for first entry is getting deleted.And there is a redundancy as details for the second student is getting copied into first student.

For better explanation I am including the screenshots:

Details of the First Student Printed

This means that the 1st Entry was properly entered and stored.

Printing the details of all the students

If you see the Last Screenshot, you will see that the details of the 1st student has been replaced by details of the second.And thus there is redundancy.

Aneek Roy
  • 11
  • 3
  • 1
    Please try to create a minimal example to demonstrate your problem. – Paul Boddington Apr 10 '16 at 17:43
  • In this case, MVC has greatly complicated your logic. You are using controllers improperly. There should only be one controller here. In this case `main` is your controller. – 4castle Apr 10 '16 at 17:46
  • @PaulBoddington Basically if you go through the `studentMain.java`, you will find that I have tried to copy using an object of `studentController class` into an Arraylist with specified type of ``.While doing so the first entry works alright,but while entering the second entry,the first entry gets deleted and second entry is copied into the arraylist twice. – Aneek Roy Apr 10 '16 at 18:08
  • @4castle I am using the `studentMain` class to display the `Main Menu` and to take input from the user, rest all the manipulation is done through the object of`studentController` class through the methods defined in side the class `studentController` like `input( ) , show( ) ,show_for_all( ) ` and `change_marks( )` – Aneek Roy Apr 10 '16 at 18:12
  • This is a bit all over the place. `main` is your view then. – 4castle Apr 10 '16 at 20:32

1 Answers1

1

Before describing the problems you are interested about, let me describe the problems you are not (yet) interested about.

You seem to be very confused about MVC in general. MVC, as model-view-controller is based on the idea that models should be separated from views and from controllers. The Model is responsible for data modelling, the View is responsible for interaction with the user and the Controller is responsible for backend logic. So I would advise you to refactor your code. Believe me, at the and you will be grateful for this idea.

Now, let's see the problems you are interested about.

You use an object, which is called controller. This is created outside the infinite cycle. This is not a good idea, since you create an object without knowing that you will actually need it. What if the user enters 6? In that case you allocated memory in vain. So, change this:

studentController controller = new studentController(model,view);

to this:

studentController controller;

Now, let's see the commands. At command 1, you use the reference to controller and add it to the list. If controller is referenced to the first element in the list due to previous iterations and then you press 1, then you add the very same object to the list. This means, that you duplicate the reference to the object and hence the problem you were complaining about. So, instead of this:

    case 1 :controller.input();
        list.add(controller);
        break;

you need this:

        case 1 : {
            list.add((new studentController(model,view)).input());
        } break;

You might have other problems as well (I did not test the code with my suggestions), but I believe this answer is good-enough to help you to resolve the problems. Anyway, refactor the code.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175