Guest User

Untitled

a guest
Jan 12th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.00 KB | None | 0 0
  1. package preparationuap;
  2. import java.util.*;
  3. import java.security.SecureRandom;
  4.  
  5. /**
  6.  *
  7.  * @author wilsonsentanoe
  8.  */
  9. public class PreparationUAP {
  10.  
  11.     /**
  12.      * @param args the command line arguments
  13.      */
  14.     public static Scanner scan = new Scanner(System.in);
  15.  
  16.     public static ArrayList<String> arrTitle = new ArrayList<String>();
  17.     public static ArrayList<String> arrAuthor = new ArrayList<String>();
  18.     public static ArrayList<String> arrPublisher = new ArrayList<String>();
  19.     public static ArrayList<String> arrYear = new ArrayList<String>();
  20.     public static ArrayList<String> arrISBN = new ArrayList<String>();
  21.     public static ArrayList<String> arrShelf = new ArrayList<String>();
  22.     public static ArrayList<String> arrBookID = new ArrayList<String>();
  23.  
  24.     public static void clearScreen(){
  25.         for(int i = 0; i < 30; i++){
  26.             System.out.println("");
  27.         }
  28.     }
  29.    
  30.     public static String getNumeric(int len) {
  31.  
  32.         char[] ch = "0123456789".toCharArray();
  33.  
  34.         char[] c = new char[len];
  35.         SecureRandom random = new SecureRandom();
  36.         for (int i = 0; i < len; i++) {
  37.             c[i] = ch[random.nextInt(ch.length)];
  38.         }
  39.  
  40.         return new String(c);
  41.     }
  42.    
  43.     public static String getAlpha(int len) {
  44.  
  45.         char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
  46.  
  47.         char[] c = new char[len];
  48.         SecureRandom random = new SecureRandom();
  49.         for (int i = 0; i < len; i++) {
  50.             c[i] = ch[random.nextInt(ch.length)];
  51.         }
  52.  
  53.         return new String(c);
  54.     }
  55.    
  56.     public static String getAlphaNum(){
  57.         String random = getNumeric(2) + getAlpha(3);
  58.        
  59.         return random;
  60.     }
  61.  
  62.     public static void insert(){
  63.         String title = "", author = "", publisher = "";
  64.         int year = 0;
  65.         long isbn = 0;
  66.         String isbnString = "", yearString = "";
  67.         String shelf = "";
  68.  
  69.         clearScreen();
  70.  
  71.         do{
  72.             System.out.print("Input Title [not more than 35 characters]: ");
  73.             title = scan.nextLine();
  74.         }while(title.length() > 35);
  75.  
  76.         do{
  77.             System.out.print("Input Author [lastname, firstname][<30]: ");
  78.             author = scan.nextLine();
  79.         }while((author.length() > 30) || !(author.contains(", ")));
  80.  
  81.         do{
  82.             System.out.print("Input Publisher [not more than 20 characters]: ");
  83.             publisher = scan.nextLine();
  84.         }while(publisher.length() > 20);
  85.  
  86.         do{
  87.             System.out.print("Input Year Published [must be newer than 1970]: ");
  88.             try{
  89.                 year = scan.nextInt();
  90.             }catch(Exception e){}
  91.             scan.nextLine();
  92.         }while(year < 1970 || year > 2018);
  93.  
  94.         yearString = Integer.toString(year);
  95.  
  96.         do{
  97.             System.out.print("Input ISBN: ");
  98.             try{
  99.                 isbn = scan.nextLong();
  100.             }catch(Exception e){}
  101.             scan.nextLine();
  102.  
  103.             isbnString = Long.toString(isbn);
  104.         }while(isbnString.length() != 13);
  105.  
  106.         do{
  107.             System.out.print("Input Shelf [1-9][A-G]: ");
  108.             shelf = scan.next();
  109.         } while(!shelf.matches("[1-9]+[A-Ga-g]"));
  110.  
  111.         arrTitle.add(title);
  112.         arrAuthor.add(author);
  113.         arrPublisher.add(publisher);
  114.         arrYear.add(yearString);
  115.         arrISBN.add(isbnString);
  116.         //new
  117.         arrShelf.add(shelf.toUpperCase());
  118.         //endnew
  119.        
  120.         String bookID = getAlphaNum();
  121.        
  122.         System.out.println("Book ID: " + bookID);
  123.        
  124.         arrBookID.add(bookID);
  125.  
  126.         System.out.println("Data has been successfully inserted!");
  127.         System.out.print("Press enter to continue...");
  128.         scan.nextLine();
  129.         scan.nextLine();
  130.     }
  131.  
  132.     public static void view(){
  133.         clearScreen();
  134.  
  135.         if(arrTitle.isEmpty()){
  136.             System.out.println("No Data Available!");
  137.         }else{
  138.             //%-36s --> %-30s at Title
  139.             System.out.printf("%-3s | %-30s | %-31s | %-21s | %-5s | %-14s | %-3s | %-6s", "No", "Title", "Author", "Publisher", "Year", "ISBN", "Shelf Location", "ID");
  140.             for(int i = 0; i < arrTitle.size(); i++){
  141.                 System.out.printf("\n%-3d | %-30s | %-31s | %-21s | %-5s | %-14s | %-3s | %-6s",(i+1), arrTitle.get(i), arrAuthor.get(i), arrPublisher.get(i), arrYear.get(i), arrISBN.get(i), arrShelf.get(i), arrBookID.get(i));
  142.             }
  143.         }
  144.     }
  145.  
  146.     public static void searchGeneral(ArrayList<String> a, String search){
  147.         boolean _isEmpty = true;
  148.  
  149.         clearScreen();
  150.             System.out.println("Showing search result for keyword: " + search);
  151.             //%-36s --> %-30s at Title
  152.             System.out.printf("\n%-3s | %-30s | %-31s | %-21s | %-5s | %-14s | %-3s | %-6s", "No", "Title", "Author", "Publisher", "Year", "ISBN", "Shelf Location", "ID");
  153.             for(int i = 0; i < arrTitle.size(); i++){
  154.                 for(int j = 0; j < a.get(i).length(); j++){
  155.                     if(a.get(i).regionMatches(true, j, search, 0, search.length())){
  156.                         System.out.printf("\n%-3d | %-30s | %-31s | %-21s | %-5s | %-14s | %-3s | %-6s",(i+1), arrTitle.get(i), arrAuthor.get(i), arrPublisher.get(i), arrYear.get(i), arrISBN.get(i), arrShelf.get(i), arrBookID.get(i));
  157.                         _isEmpty = false;
  158.                         break;
  159.                     }
  160.                 }
  161.             }
  162.  
  163.         if(_isEmpty){
  164.             System.out.println("\nNo Data Found");
  165.         }
  166.         System.out.print("\nPress enter to continue...");
  167.             scan.nextLine();
  168.     }
  169.  
  170.     public static void search(){
  171.         int choose = 0;
  172.         long isbn = 0;
  173.         String search = "";
  174.  
  175.         clearScreen();
  176.  
  177.         do{
  178.             System.out.println("Search By ___");
  179.             System.out.println("1. Title");
  180.             System.out.println("2. Author");
  181.             System.out.println("3. Publisher");
  182.             System.out.println("4. ISBN");
  183.             System.out.println("5. Shelf Location");
  184.             System.out.println("6. Cancel Search");
  185.             try{
  186.                 System.out.print("Choose: ");
  187.                 choose = scan.nextInt();
  188.             }catch(Exception e){}
  189.             scan.nextLine();
  190.  
  191.             switch(choose){
  192.                 case 1:
  193.                     do{
  194.                         System.out.print("Input Title [not more than 35 characters]: ");
  195.                         search = scan.nextLine();
  196.                     }while(search.length() > 35);
  197.  
  198.                     searchGeneral(arrTitle, search);
  199.                     break;
  200.                 case 2:
  201.                     do{
  202.                         System.out.print("Input Author Name [not more than 30 characters]: ");
  203.                         search = scan.nextLine();
  204.                     }while((search.length() > 30));
  205.  
  206.                     searchGeneral(arrAuthor, search);
  207.                     break;
  208.                 case 3:
  209.                     do{
  210.                         System.out.print("Input Publisher [not more than 20 characters]: ");
  211.                         search = scan.nextLine();
  212.                     }while(search.length() > 20);
  213.  
  214.                     searchGeneral(arrPublisher, search);
  215.                     break;
  216.                 case 4:
  217.                     do{
  218.                         System.out.println("Input ISBN: ");
  219.                         try{
  220.                             isbn = scan.nextLong();
  221.                         }catch(Exception e){}
  222.                         scan.nextLine();
  223.                         search = Long.toString(isbn);
  224.                     }while(search.length() != 13);
  225.  
  226.                     searchGeneral(arrISBN, search);
  227.                     break;
  228.         case 5:
  229.                     do{
  230.                         System.out.print("Input Shelf Location: ");
  231.                         search = scan.next();
  232.                     } while(!search.matches("[1-9]+[A-Ga-g]"));
  233.                     searchGeneral(arrShelf, search);
  234.                     break;
  235.             }break;
  236.         }while(choose != 6);
  237.     }
  238.     //string c for shelf
  239.     public static void SelectionSort(ArrayList<String> x, ArrayList<String> y, ArrayList<String> z, ArrayList<String> a, ArrayList<String> b, ArrayList<String> c){
  240.         for (int i=0; i<x.size()-1; i++) {
  241.             for (int j=i+1; j<x.size(); j++) {
  242.                 if (x.get(i).compareTo(x.get(j)) > 0) {
  243.                     //... Exchange elements in first array
  244.                     String temp = x.get(i);
  245.                     x.set(i, x.get(j));
  246.                     x.set(j, temp);
  247.  
  248.                     //... Exchange elements in second array
  249.                     temp = y.get(i);
  250.                     y.set(i, y.get(j));
  251.                     y.set(j, temp);
  252.  
  253.                     //... Exchange elements in third array
  254.                     temp = z.get(i);
  255.                     z.set(i, z.get(j));
  256.                     z.set(j, temp);
  257.  
  258.                     //... Exchange elements in forth array
  259.                     temp = a.get(i);
  260.                     a.set(i, a.get(j));
  261.                     a.set(j, temp);
  262.  
  263.                     //... Exchange elements in fifth array
  264.                     temp = b.get(i);
  265.                     b.set(i, b.get(j));
  266.                     b.set(j, temp);
  267.  
  268.                     // Exchange 6th element
  269.                     temp = c.get(i);
  270.                     c.set(i, c.get(j));
  271.                     c.set(j, temp);
  272.                 }
  273.             }
  274.         }
  275.     }
  276.  
  277.     public static void sort(){
  278.         int choose = 0;
  279.  
  280.         clearScreen();
  281.  
  282.         do{
  283.             System.out.println("Sort by ____");
  284.             System.out.println("1. Title");
  285.             System.out.println("2. Author");
  286.             System.out.println("3. Publisher");
  287.             System.out.println("4. Year Published");
  288.             System.out.println("5. Cancel Sort");
  289.             try{
  290.                 System.out.print("Choose: ");
  291.                 choose = scan.nextInt();
  292.             }catch(Exception e){}
  293.             scan.nextLine();
  294.  
  295.             switch(choose){
  296.             case 1:
  297.                 SelectionSort(arrTitle, arrAuthor, arrPublisher, arrYear, arrISBN, arrShelf);
  298.                 view();
  299.                 break;
  300.             case 2:
  301.                 SelectionSort(arrAuthor, arrTitle, arrPublisher, arrYear, arrISBN, arrShelf);
  302.                 view();
  303.                 break;
  304.             case 3:
  305.                 SelectionSort(arrPublisher, arrAuthor, arrTitle, arrYear, arrISBN, arrShelf);
  306.                 view();
  307.                 break;
  308.             case 4:
  309.                 SelectionSort(arrYear, arrAuthor, arrPublisher, arrTitle, arrISBN, arrShelf);
  310.                 view();
  311.                 break;
  312.             }break;
  313.         }while(choose != 5);
  314.        
  315.         System.out.print("\nPress enter to continue...");
  316.         scan.nextLine();
  317.     }
  318.  
  319.     public static void delete(){
  320.         int index = 0;
  321.  
  322.         clearScreen();
  323.         view();
  324.  
  325.         do{
  326.             System.out.print("\n\nInput number to be deleted [1-" + arrTitle.size() + "][0 for Cancel]: ");
  327.             try{
  328.                 index = scan.nextInt();
  329.             }catch(Exception e){}
  330.             scan.nextLine();
  331.  
  332.             if(index == 0)
  333.                 break;
  334.             else{
  335.                 arrTitle.remove(index - 1);
  336.                 arrAuthor.remove(index - 1);
  337.                 arrPublisher.remove(index - 1);
  338.                 arrYear.remove(index - 1);
  339.                 arrISBN.remove(index - 1);
  340.                 arrShelf.remove(index - 1);
  341.  
  342.                 System.out.println("Data has been successfully deleted!");
  343.                 System.out.print("Press enter continue...");
  344.                 scan.nextLine();
  345.                 break;
  346.             }
  347.         }while(index >= arrTitle.size());
  348.     }
  349.  
  350.     public static void main(String[] args) {
  351.         int choose = 0;
  352.  
  353.         do{
  354.             clearScreen();
  355.  
  356.             System.out.println("=== Library Book Management ===");
  357.             System.out.println("1. Insert New Book");
  358.             System.out.println("2. View Book List");
  359.             System.out.println("3. Search Book");
  360.             System.out.println("4. Sort Book List");
  361.             System.out.println("5. Delete a Book");
  362.             System.out.println("6. Exit");
  363.             try{
  364.                 System.out.print("Choose: ");
  365.                 choose = scan.nextInt();
  366.             }catch(Exception e){}
  367.             scan.nextLine();
  368.  
  369.             switch(choose){
  370.                 case 1:
  371.                     insert();
  372.                     break;
  373.                 case 2:
  374.                     view();
  375.                     System.out.print("\nPress enter continue...");
  376.                     scan.nextLine();
  377.                     break;
  378.                 case 3:
  379.                     search();
  380.                     break;
  381.                 case 4:
  382.                     sort();
  383.                     break;
  384.                 case 5:
  385.                     delete();
  386.                     break;
  387.                 case 6:
  388.                     System.out.println("Thank you for using our app.");
  389.                     break;
  390.            }
  391.         }while(choose != 6);
  392.     }
  393.    
  394. }
Advertisement
Add Comment
Please, Sign In to add comment