Program
import java.io.*;
class Student {
String name;
int age;
String address;
int rollno;
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
/*
public Student(int size) {
Student[] s=new Student[size];
}
*/
public void addStudent() throws Exception {
System.out.print("Name : ");
this.name=br.readLine();
System.out.print("Age : ");
this.age=Integer.parseInt(br.readLine());
System.out.print("Address : ");
this.address=br.readLine();
System.out.print("Rollno : ");
this.rollno=Integer.parseInt(br.readLine());
}
public void listStudent() {
System.out.println("Name : " + this.name);
System.out.println("Age : " + this.age);
System.out.println("Address : " + this.address);
System.out.println("Rollno : " + this.rollno);
System.out.println("");
}
}
class StudentManagement {
public static void main(String args[]) throws Exception {
if(args.length!=1) {
System.out.println("\nOne command line argument should be needed as no of students !!!");
System.exit(0);
}
int ch,n,j=0;
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
n=Integer.parseInt(args[0]);
Student[] s=new Student[n];
for(int i=0;i<n;i++) {
s[i] = new Student();
}
do{
System.out.println("");
System.out.println("1 Add");
System.out.println("2 List");
System.out.println("0 Exit ");
System.out.print("Enter your choice : ");
ch=Integer.parseInt(br.readLine());
System.out.println("");
switch(ch) {
case 1:
if(j<n) {
s[j++].addStudent();
System.out.println("");
break;
}
else {
System.out.println("Maximum size reached !!");
break;
}
case 2:
System.out.println("\n—————\nStudent Details " + "\n—————");
for(int i=0;i<j;i++) {
s[i].listStudent();
}
System.out.println("");
break;
default:
break;
}
}while(ch!=0);
}
}
Output
D:\a5518\day4>javac StudentManagement.java
D:\a5518\day4>java StudentManagement
One command line argument should be needed as no of students !!!
D:\a5518\day4>java StudentManagement 2
1 Add
2 List
0 Exit
Enter your choice : 1
Name : jijo
Age : 23
Address : kanjirapally
Rollno : 14
1 Add
2 List
0 Exit
Enter your choice : 1
Name : John
Age : 30
Address : Kottayam
Rollno : 07
1 Add
2 List
0 Exit
Enter your choice : 1
Maximum size reached !!
1 Add
2 List
0 Exit
Enter your choice : 2
—————
Student Details
—————
Name : jijo
Age : 23
Address : kanjirapally
Rollno : 14
Name : John
Age : 30
Address : Kottayam
Rollno : 7
1 Add
2 List
0 Exit
Enter your choice : 0
D:\a5518\day4>