Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.82 KB | None | 0 0
  1. package integratedlibraryclient;
  2.  
  3. import ejb.BookAuthorManagerBeanRemote;
  4. import ejb.BookLoanManagerBeanRemote;
  5. import ejb.LibMemberManagerBeanRemote;
  6. import java.io.BufferedReader;
  7. import java.io.InputStreamReader;
  8. import javax.ejb.EJB;
  9. import java.util.*;
  10.  
  11.  
  12. public class Main {
  13.  
  14.     @EJB
  15.     private static LibMemberManagerBeanRemote lmb;
  16.     @EJB
  17.     private static BookAuthorManagerBeanRemote ba;
  18.     @EJB
  19.     private static BookLoanManagerBeanRemote bl;
  20.  
  21.     public Main(){}
  22.    
  23.     public static void main(String[] args) {
  24.         Main admin = new Main();
  25.         admin.doILS(args);
  26.     }
  27.    
  28.     public void doILS(String[] args){
  29.         try{
  30.             String choice = "";
  31.             Scanner sc = new Scanner(System.in);
  32.             while(!choice.equals("0")){
  33.                 //main menu
  34.                 System.out.println("***************************************");
  35.                 System.out.println("Welcome to ILS Admin Portal!");
  36.                 System.out.println("***************************************");
  37.                 System.out.println("Please enter your choice:");
  38.                 System.out.println("1. Add User");
  39.                 System.out.println("2. Delete User");
  40.                 System.out.println("3. Add Author");
  41.                 System.out.println("4. Add Book");
  42.                 System.out.println("5. Update Book");
  43.                 System.out.println("6. Delete Book");
  44.                 System.out.println("7. Checkout Book");
  45.                 System.out.println("8. Return Book");
  46.                 System.out.println("9. View Reservations");
  47.                 System.out.println("10. View Checkouts");
  48.                 System.out.println("11. View Fines");
  49.                 System.out.println("12. Process Request");
  50.                 System.out.println("0: Exit");
  51.                
  52.                 System.out.print("Enter Choice: ");
  53.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  54.                 choice = br.readLine();
  55.                
  56.                 if(choice.equals("1")){
  57.                     addUser(sc);
  58.                 } else if(choice.equals("2")){
  59.                     deleteUser(sc);
  60.                 } else if(choice.equals("3")){
  61.                     addAuthor(sc);
  62.                 } else if(choice.equals("4")){
  63.                     addBook(sc);
  64.                 } else if(choice.equals("5")){
  65.                     updateBook(sc);
  66.                 } else if(choice.equals("6")){
  67.                     deleteBook(sc);
  68.                 } else if(choice.equals("7")){
  69.                     checkoutBook(sc);
  70.                 } else if(choice.equals("8")){
  71.                     returnBook(sc);
  72.                 } else if(choice.equals("9")){
  73.                     viewReservation(sc);
  74.                 } else if(choice.equals("10")){
  75.                     viewCheckout(sc);
  76.                 } else if(choice.equals("11")){
  77.                     viewFine(sc);
  78.                 } else if(choice.equals("12")){
  79.                     processRequest(sc);
  80.                 } else if(choice.equals("0")){
  81.                     return;
  82.                 } else{
  83.                     return;
  84.                 }
  85.             }
  86.            
  87.         }catch(Exception ex){
  88.             System.err.println("Caught an unexpected exception!");
  89.         }
  90.     }
  91.    
  92.     private void addUser(Scanner sc){
  93.         try{
  94.             System.out.println("---ADD USER---");
  95.             System.out.print("Enter User Name: ");
  96.             String inName = sc.nextLine();
  97.             System.out.print("Enter Default Password: ");
  98.             String inPass = sc.nextLine();
  99.             System.out.print("Enter Default Password Again: ");
  100.             String inPass2 = sc.nextLine();
  101.             System.out.print("Enter Contact Number: ");
  102.             String inContact = sc.nextLine();
  103.             System.out.print("Enter E-mail: ");
  104.             String inEmail = sc.nextLine();
  105.             System.out.print("Enter Address: ");
  106.             String inAddress = sc.nextLine();
  107.  
  108.             String result = lmb.createMember(inName, inPass, inContact, inEmail, inAddress, inPass2);
  109.            
  110.             System.out.println(result);
  111.         } catch(Exception ex) {
  112.             System.out.println("\nFailed to create member because " + ex.getMessage() + "\n");
  113.         }
  114.     }
  115.    
  116.     private void deleteUser(Scanner sc){
  117.         try{
  118.             System.out.println("---ADD USER---");
  119.             System.out.print("Enter User Name: ");
  120.             String username = sc.nextLine();
  121.             String result = lmb.deleteMember(username);
  122.             System.out.println(result);
  123.         } catch (Exception ex){
  124.             System.out.println("\n Failed to delete user becase " + ex.getMessage() + "\n");
  125.         }
  126.     }
  127.    
  128.     private void addAuthor(Scanner sc){
  129.         try{
  130.             System.out.println("---ADD AUTHOR---");
  131.             System.out.print("Enter Author Name: ");
  132.             String inAuthorName = sc.nextLine();
  133.             System.out.print("Enter Author's Description: ");
  134.             String inAuthorDesc = sc.nextLine();
  135.  
  136.             String authorID = ba.createAuthor(inAuthorName, inAuthorDesc);
  137.            
  138.             System.out.println("\nAuthor added successfully!\n");
  139.            
  140.             System.out.println("Author ID: " + authorID);
  141.         } catch (Exception ex){
  142.             System.out.println("\n Failed to create author becase " + ex.getMessage() + "\n");
  143.         }
  144.     }
  145.    
  146.     private void addBook(Scanner sc){
  147.         try{
  148.             System.out.println("---ADD BOOK---");
  149.             System.out.print("Enter Title: ");
  150.             String inTitle = sc.nextLine();
  151.            
  152.             System.out.print("Enter Number of Authors: ");
  153.             int numAuthors = sc.nextInt();
  154.  
  155.             String[] authorArr = new String[numAuthors];
  156.            
  157.             for(int i=0; i<numAuthors; i++){
  158.                 System.out.print("Enter ID of Author: ");
  159.                 authorArr[i] = sc.next();
  160.             }
  161.             sc.nextLine();
  162.             System.out.print("Enter Publisher: ");
  163.             String inPub = sc.nextLine();
  164.             System.out.print("Enter Publication Year: ");
  165.             int inPubYear = sc.nextInt();
  166.             sc.nextLine();
  167.             System.out.print("Enter ISBN: ");
  168.             String inISBN = sc.nextLine();
  169.             System.out.print("Enter Copy No: ");
  170.             int inCopyNo = sc.nextInt();
  171.            
  172.             sc.nextLine();
  173.            
  174.             String result = ba.createBook(inTitle, inPub, inPubYear, inISBN, inCopyNo, authorArr);
  175.             System.out.println(result);
  176.            
  177.         } catch(Exception ex) {
  178.             System.out.println("\nFailed to create book because " + ex.getMessage() + "\n");
  179.         }
  180.     }
  181.    
  182.     private void updateBook(Scanner sc){
  183.         try{
  184.             System.out.println("---UPDATE BOOK---");
  185.             System.out.print("Enter ISBN: ");
  186.             String oldISBN = sc.nextLine();
  187.             System.out.print("Enter Copy Number: ");
  188.             int oldCopyNo = sc.nextInt();
  189.             sc.nextLine();
  190.             System.out.println("---Enter Details of New Book---");
  191.             System.out.print("Enter New Title: ");
  192.             String newTitle = sc.nextLine();
  193.             System.out.print("Enter New Publisher: ");
  194.             String newPub = sc.nextLine();
  195.             System.out.print("Enter New Publication Year: ");
  196.             int newPubYear = sc.nextInt();            
  197.             sc.nextLine();
  198.             System.out.print("Enter New ISBN: ");
  199.             String newISBN = sc.nextLine();
  200.             System.out.print("Enter New Copy Number: ");
  201.             int newCopyNo = sc.nextInt();
  202.             System.out.print("Enter New Number of Authors: ");
  203.             int newNumAuthors = sc.nextInt();
  204.             sc.nextLine();
  205.             String[] authorArr = new String[newNumAuthors];
  206.            
  207.             for(int i=0; i<newNumAuthors; i++){
  208.                 System.out.print("Enter ID of Author " + (i+1) + ":");
  209.                 authorArr[i] = sc.next();
  210.             }
  211.             sc.nextLine();
  212.            
  213.             String result = ba.updateBook(oldISBN, oldCopyNo, newTitle, newPub, newPubYear, newISBN, newCopyNo, authorArr);
  214.             System.out.println(result);
  215.         }catch(Exception ex){
  216.             System.out.println("\nFailed to update book because " + ex.getMessage() + "\n");
  217.         }
  218.     }
  219.    
  220.     public void deleteBook(Scanner sc){
  221.         try {
  222.             System.out.println("---DELETE BOOK---");
  223.             System.out.print("Enter ISBN: ");
  224.             String isbn = sc.nextLine();
  225.             System.out.print("Enter Copy No: ");
  226.             int copyNo = sc.nextInt();
  227.             sc.nextLine();
  228.             String result = ba.deleteBook(isbn, copyNo);
  229.             System.out.println(result);
  230.         } catch (Exception ex) {
  231.             System.out.println("\nFailed to delete book because " + ex.getMessage() + "\n");
  232.  
  233.         }
  234.     }
  235.    
  236.     public void checkoutBook(Scanner sc){
  237.         try {
  238.             System.out.println("---CHECKOUT BOOK---");
  239.             System.out.print("Enter Username: ");
  240.             String username = sc.nextLine();
  241.             System.out.print("Enter ISBN: ");
  242.             String isbn = sc.nextLine();
  243.             System.out.print("Enter Copy No: ");
  244.             int copyNo = sc.nextInt();
  245.             sc.nextLine();
  246.             String dueDate = bl.checkoutBook(username, isbn, copyNo);
  247.             if(dueDate.equals("Book currently unavailable")){
  248.                 System.out.println(dueDate);
  249.             }
  250.             else {
  251.                 System.out.println("Book successfully checkout!");
  252.                 System.out.println("Due date: " + dueDate);
  253.             }
  254.         } catch (Exception ex) {
  255.             System.out.println("\nFailed to checkout book because " + ex.getMessage() + "\n");
  256.         }
  257.     }
  258.    
  259.     public void returnBook(Scanner sc){
  260.         try {
  261.             System.out.println("---RETURN BOOK---");
  262.             System.out.print("Enter ISBN: ");
  263.             String isbn = sc.nextLine();
  264.             System.out.print("Enter Copy No: ");
  265.             int copyNo = sc.nextInt();
  266.             sc.nextLine();
  267.             int days = bl.returnBook(isbn, copyNo);
  268.             if(days > 0)
  269.                 System.out.println("Book overdue. Fine successfully created");
  270.             else if(days == -1)
  271.                 System.out.println("Error, no checkout for the book");
  272.            
  273.             System.out.println("Book successfully returned!");  
  274.         } catch (Exception ex) {
  275.             System.out.println("\nFailed to checkout book because " + ex.getMessage() + "\n");
  276.         }
  277.     }
  278.    
  279.     public void viewReservation(Scanner sc){
  280.         try {
  281.             System.out.println("---VIEW RESERVATIONS---");
  282.             System.out.print("Enter ISBN: ");
  283.             String isbn = sc.nextLine();
  284.             System.out.print("Enter Copy No: ");
  285.             int copyNo = sc.nextInt();
  286.             sc.nextLine();
  287.             String returnMsg = (String) bl.viewReservations(isbn, copyNo).get(0);
  288.             ArrayList results = bl.viewReservations(isbn, copyNo);
  289.             int size =  bl.viewReservations(isbn, copyNo).size();
  290.            
  291.             if(returnMsg.equals("Error! Incorrect book details") || returnMsg.equals("No reservations found!")){
  292.                 System.out.println(returnMsg);
  293.             } else {
  294.                 System.out.println("BookTitle: " + returnMsg);
  295.                 for(int i=1; i<size; i++){
  296.                     Object o = results.get(i);
  297.                     String reservation = (String) o;
  298.                     StringTokenizer st = new StringTokenizer(reservation, " # ");
  299.                    
  300.                     System.out.println(st.nextToken());
  301.                     System.out.println("Username: " + st.nextToken());
  302.                     System.out.println("Email: " + st.nextToken());
  303.                     System.out.println("Reservation Time: " + st.nextToken() + " " + st.nextToken());
  304.                     System.out.println("Note: " + st.nextToken());
  305.                 }
  306.             }
  307.         } catch (Exception ex) {
  308.             System.out.println("\nFailed to checkout book because " + ex.getMessage() + "\n");
  309.         }
  310.     }
  311.    
  312.     public void viewCheckout(Scanner sc){
  313.         try {
  314.             System.out.println("---VIEW CURRENT CHECKOUTS---\n");
  315.            
  316.             if(bl.viewCheckouts().get(0).equals("No checkout found!")){
  317.                 System.out.println("No checkout found!");
  318.             }
  319.             else {
  320.                 for(Object o: bl.viewCheckouts()){
  321.                     String checkout = (String) o;
  322.                     StringTokenizer st = new StringTokenizer(checkout, " # ");
  323.                         System.out.println("ISBN: " + st.nextToken());
  324.                         System.out.println("Copy No: " + st.nextToken());
  325.                         System.out.println("Title: " + st.nextToken());
  326.                         System.out.println("Username: " + st.nextToken());
  327.                         System.out.println("E-mail Address: " + st.nextToken());
  328.                         System.out.println("Loan Date: " + st.nextToken());
  329.                         System.out.println("Due Date: " + st.nextToken() + "\n\n");
  330.                 }
  331.             }
  332.         } catch (Exception ex) {
  333.             System.out.println("\nFailed to get checkout books because " + ex.getMessage() + "\n");
  334.         }
  335.     }
  336.    
  337.     public void viewFine(Scanner sc){
  338.         try {
  339.             System.out.println("---VIEW FINES---\n");
  340.            
  341.             if(bl.viewFines().get(0).equals("No fine found!")){
  342.                 System.out.println("No fine found!");
  343.             }
  344.             else {
  345.                 for(Object o: bl.viewFines()){
  346.                     String fine = (String) o;
  347.                     String[] fineS = fine.split("#");
  348.                         System.out.println("FineID: " + fineS[0]);
  349.                         System.out.println("Fine Date: " + fineS[1]);
  350.                         System.out.println("Fine Amount: " + fineS[2]);
  351.                         System.out.println("Username: " + fineS[3]);
  352.                         System.out.println("E-mail Address: " + fineS[4] + "\n\n");
  353.                 }
  354.             }
  355.            
  356.         } catch (Exception ex) {
  357.             System.out.println("\nFailed to view fine because " + ex.getMessage() + "\n");
  358.         }
  359.     }
  360.    
  361.     public void processRequest(Scanner sc){
  362.         try {
  363.             System.out.println("---VIEW REQUESTS---\n");
  364.            
  365.             if(bl.viewRequest().get(0).equals("No request found!")){
  366.                 System.out.println("No request found!");
  367.             }
  368.             else {
  369.                 for(Object o: bl.viewRequest()){
  370.                     String req = (String) o;
  371.                     StringTokenizer st = new StringTokenizer(req, " # ");
  372.                     System.out.println("Request ID: " + st.nextToken());
  373.                     System.out.println("Username: " + st.nextToken());
  374.                     System.out.println("Time: " + st.nextToken());
  375.                     System.out.println("Message: " + st.nextToken() + "\n");
  376.                 }
  377.                
  378.                 System.out.print("Enter the request to be processed: ");
  379.                 String requestID = sc.nextLine();
  380.                 System.out.print("Change the status to: ");
  381.                 String status = sc.nextLine();
  382.                 System.out.print("Comment: ");
  383.                 String comment = sc.nextLine();
  384.                 String result = bl.processRequest(requestID, status, comment);
  385.                
  386.                 System.out.println(result);
  387.             }
  388.         } catch (Exception ex) {
  389.             System.out.println("\nFailed to get request because " + ex.getMessage() + "\n");
  390.         }
  391.     }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement