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:
This means that the 1st Entry was properly entered and stored.
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.