Wednesday, August 8, 2012

Menu Driven Program in Java

 A menu driven system which will perform following options for student data
 1) (Roll No,Name,Per)
 2) Insert, Update, Delete, Search  record
 3) Raise Exception if wrong data are entered like negative for roll no.

import java.io.*;
 import java.util.*;

 class NegativeNumberException extends Exception  { }


 class Slip5_9_13_2
 {
     public static void main(String agrs[]) throws IOException
     {
    
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       Student stud;
  &stud);
                       break;
                case 2:System.out.print("\nEnter Position where to insert record : ");
                        pos=Integer.parseInt(br.readLine());
         System.out.println("|------------------------------|");
         System.out.println("|\t [1] :Add \t\t|");
         System.out.println("|\t [2] :Insert \t\t|");
         System.out.println("|\t [3] :Update \t\t|");
         System.out.println("|\t [4] :Search \t\t|");
         System.out.println("|\t [5] :Delete \t\t|");
         System.out.println("|\t [6] :Print \t\t|");
         System.out.println("|\t [7] :Exit \t\t|");
          System.out.println("|------------------------------|");
         System.out.print("\nEnter Choice : ");
        
        
         try
          {
              ch=Integer.parseInt(br.readLine());
             
               switch(ch)
              {
                case 1:System.out.println("\nEnter Stud Details\n");
                       stud=new Student();
                       perfect=stud.read_data();   
                       if(perfect==1)
                           L.add(stud);
                       break;
                case 2:System.out.print("\nEnter Position where to insert record : ");
                        pos=Integer.parseInt(br.readLine());
                       if(pos>L.size())
                         {
                              System.out.println("\n\nPosition out of range\n\n");
                              break;
                         }   
                      
                       stud= new Student();
                       perfect=stud.read_data();
                       if(perfect==1)
                            L.add(pos,stud);
                       break;
                 case 3:System.out.print("\n Enter Roll no to update record : ");
                        no=Integer.parseInt(br.readLine());
                        found=0;
                        it=L.iterator();
                         while(it.hasNext())
                         {
                           stud=(Student)it.next();
                           if(stud.rno==no)
                              {
                                 found=1;   
                                  pos=L.indexOf(stud);
                                L.remove(pos);// remove old record from specified pos
                               
                                perfect=stud.read_data();
                               
                                if(perfect==1)
                                     L.add(pos,stud);
                            
                                break;
                               }
                         } 
                       if(found!=0)  //found==0
                           System.out.println("\nRoll_No not present so Record Not Updated ");
                                               
                       break;        
                              
                case 4:System.out.print("Enter Rollno to search record : ");
                       no=Integer.parseInt(br.readLine());
                       found=0;
                       it=L.iterator();
                      
                       while(it.hasNext())
                       {
                           stud=(Student)it.next();
                            if(stud.rno==no)
                              {
                                  found=1;
                                  pos=L.indexOf(stud);
                                  System.out.println("\n\nReord Found at "+pos+" Position\n");
                                  stud.show_data();
                                 
                                  break;
                                 
                              }
                       }
                       if(found!=1)
                             System.out.println("\nRoll_No not present so Record Not Found");
                         
                      break;
                     
                case 5:System.out.print("\n Enter Roll no to delete record : ");
                        no=Integer.parseInt(br.readLine());
                        found=0;
                        it=L.iterator();
                       
                        while(it.hasNext())
                         {
                          stud=(Student)it.next();
                              if(stud.rno==no)
                            {
                                 found=1;
                                 L.remove(stud);
                                 System.out.print("\nRecord deleted succesfully \n");
                                 break;   
                               }
                         }
                        if(found!=1)
                          System.out.println("\nRoll_No not present so Record Not Deleted\n");
                        
                       break;
            
                case 6:System.out.println("\n\n|------* Student Details *-----|");
                       System.out.println("|------------------------------|");
                      System.out.println("|ROLL NO\t NAME\t PERCENTAGE|");
                      System.out.println("|------------------------------|");

                      it=L.iterator();

                      while(it.hasNext())
                       {
                             stud=(Student)it.next();
                             stud.show_data();
                       }
                      System.out.println("|------------------------------|");
                      break;
                case 7:System.exit(0);           
               
            
              }//switch
           }
         catch(NumberFormatException n)
         {   System.out.println("Exception "+n); }
        
       }while(ch!=7);
     }//main
    
 }// class slip


 class Student
 {
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     int rno;    // Roll no
     String name; //name
     double per;//percentage
    
     int  read_data() throws IOException
     {
         try
         {
        
             System.out.print("Roll No : ");
             rno=Integer.parseInt(br.readLine());
                if(rno<0)
                   throw new NegativeNumberException();
           
             System.out.print("Name : ");
             name=br.readLine();
            
             System.out.print("Percentage : ");
             per=Double.parseDouble(br.readLine());
                 if(per<0)
                     throw new NegativeNumberException();
            
          }
         
         catch(NegativeNumberException n)
         {
          System.out.println("Exception: Number Must be Positive");
           return 0;
         } 
       return 1;    
       
      }// read_data()
     
      void show_data()
      {
          System.out.println("| "+rno+"\t | "+name+" | "+per+" |");
      }  

 }

2 comments:

  1. where is L in your program ,

    ReplyDelete
  2. i think some code missed can you put total code

    ReplyDelete

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...