Java simple CRUD


#1
package com.company;

import java.util.*;


class Book{
private
     String BookName;
     String ISBN;
     String Author;
public

Book(){
    BookName=null;
    ISBN=null;
    Author=null;
}
     void create(){
         Scanner s1 =new Scanner(System.in);
         System.out.println("Enter Book Name");
         BookName=s1.nextLine();
         System.out.println("Enter Book ISBN");
         ISBN=s1.nextLine();
         System.out.println("Enter Book Author");
         Author=s1.nextLine();
     }
    void read(){
        Scanner s1 =new Scanner(System.in);
        System.out.println("Book Name: "+BookName);
        System.out.println("\nBook ISBN: "+ISBN);
        System.out.println("\nBook Author: "+Author);

    }
    void update(){
        Scanner s1 =new Scanner(System.in);
        System.out.println("\n\nCurrent Book Name: "+BookName);
        System.out.println("\nUpdate Book Name");
        BookName=s1.nextLine();
        System.out.println("\nCurrent Book ISBN: "+ISBN);
        System.out.println("\nUpdate Book ISBN");
        ISBN=s1.nextLine();
        System.out.println("\nCurrent Book Author: "+Author);
        System.out.println("Update Book Author");
        Author=s1.nextLine();
    }
    void delete(){

    }

}

public class Main {

    public static void main(String[] args) {
	    int c=0,a=0;
        int ub=0,lb=0;
        Book[] b=new Book[20];
        Scanner s= new Scanner(System.in);
        char s1=0;
        do{
           System.out.println("\nBook Management System");
           System.out.println("\n1) Add Books");
           System.out.println("\n2) Show Books");
           System.out.println("\n3) Update Books");
           System.out.println("\n4) Delete Books");

           switch (a = s.nextInt()) {
               case 1:
                   do {
                       System.out.println("\nAdd Books!");
                       Book bx = new Book();
                       bx.create();
                       b[c] = bx;
                       System.out.println("Do you want to add more books! Y/N");
                       s1 = s.next().charAt(0);
                       c++;
                       lb = c - 1;
                   } while (s1 == 'y' || s1 == 'y');
                   break;

               case 2:
                   System.out.println("\nEnter book index you want to Read: from 0 to " + c);
                   a = s.nextInt();
                   if(a>=ub && a<=lb) {
                       b[a].read();
                   }
                   else
                     System.out.println("Invalid index");
                   break;
               case 3:
                   System.out.println("\nEnter book index you want to Update: from 0 to " + c);
                   a = s.nextInt();
                   b[a].update();
                   break;
               case 4:
                   System.out.println("\nEnter book index you want to Delete: from 0 to " + c);
                   a = s.nextInt();
                   for(int i=a;i<lb;i++) {
                       b[i]=b[i+1];
                   }
                   b[lb]=null;
                   c--;
                   lb--;
                   break;
               default:
                   System.out.println("\nInvalid Input");

           }
           System.out.println("View Main menu Y/N");
           s1 = s.next().charAt(0);
       }while(s1=='y'||s1=='y');





    }
}

#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.