Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package preparationuap;
- import java.util.*;
- import java.security.SecureRandom;
- /**
- *
- * @author wilsonsentanoe
- */
- public class PreparationUAP {
- /**
- * @param args the command line arguments
- */
- public static Scanner scan = new Scanner(System.in);
- public static ArrayList<String> arrTitle = new ArrayList<String>();
- public static ArrayList<String> arrAuthor = new ArrayList<String>();
- public static ArrayList<String> arrPublisher = new ArrayList<String>();
- public static ArrayList<String> arrYear = new ArrayList<String>();
- public static ArrayList<String> arrISBN = new ArrayList<String>();
- public static ArrayList<String> arrShelf = new ArrayList<String>();
- public static ArrayList<String> arrBookID = new ArrayList<String>();
- public static void clearScreen(){
- for(int i = 0; i < 30; i++){
- System.out.println("");
- }
- }
- public static String getNumeric(int len) {
- char[] ch = "0123456789".toCharArray();
- char[] c = new char[len];
- SecureRandom random = new SecureRandom();
- for (int i = 0; i < len; i++) {
- c[i] = ch[random.nextInt(ch.length)];
- }
- return new String(c);
- }
- public static String getAlpha(int len) {
- char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
- char[] c = new char[len];
- SecureRandom random = new SecureRandom();
- for (int i = 0; i < len; i++) {
- c[i] = ch[random.nextInt(ch.length)];
- }
- return new String(c);
- }
- public static String getAlphaNum(){
- String random = getNumeric(2) + getAlpha(3);
- return random;
- }
- public static void insert(){
- String title = "", author = "", publisher = "";
- int year = 0;
- long isbn = 0;
- String isbnString = "", yearString = "";
- String shelf = "";
- clearScreen();
- do{
- System.out.print("Input Title [not more than 35 characters]: ");
- title = scan.nextLine();
- }while(title.length() > 35);
- do{
- System.out.print("Input Author [lastname, firstname][<30]: ");
- author = scan.nextLine();
- }while((author.length() > 30) || !(author.contains(", ")));
- do{
- System.out.print("Input Publisher [not more than 20 characters]: ");
- publisher = scan.nextLine();
- }while(publisher.length() > 20);
- do{
- System.out.print("Input Year Published [must be newer than 1970]: ");
- try{
- year = scan.nextInt();
- }catch(Exception e){}
- scan.nextLine();
- }while(year < 1970 || year > 2018);
- yearString = Integer.toString(year);
- do{
- System.out.print("Input ISBN: ");
- try{
- isbn = scan.nextLong();
- }catch(Exception e){}
- scan.nextLine();
- isbnString = Long.toString(isbn);
- }while(isbnString.length() != 13);
- do{
- System.out.print("Input Shelf [1-9][A-G]: ");
- shelf = scan.next();
- } while(!shelf.matches("[1-9]+[A-Ga-g]"));
- arrTitle.add(title);
- arrAuthor.add(author);
- arrPublisher.add(publisher);
- arrYear.add(yearString);
- arrISBN.add(isbnString);
- //new
- arrShelf.add(shelf.toUpperCase());
- //endnew
- String bookID = getAlphaNum();
- System.out.println("Book ID: " + bookID);
- arrBookID.add(bookID);
- System.out.println("Data has been successfully inserted!");
- System.out.print("Press enter to continue...");
- scan.nextLine();
- scan.nextLine();
- }
- public static void view(){
- clearScreen();
- if(arrTitle.isEmpty()){
- System.out.println("No Data Available!");
- }else{
- //%-36s --> %-30s at Title
- System.out.printf("%-3s | %-30s | %-31s | %-21s | %-5s | %-14s | %-3s | %-6s", "No", "Title", "Author", "Publisher", "Year", "ISBN", "Shelf Location", "ID");
- for(int i = 0; i < arrTitle.size(); i++){
- 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));
- }
- }
- }
- public static void searchGeneral(ArrayList<String> a, String search){
- boolean _isEmpty = true;
- clearScreen();
- System.out.println("Showing search result for keyword: " + search);
- //%-36s --> %-30s at Title
- System.out.printf("\n%-3s | %-30s | %-31s | %-21s | %-5s | %-14s | %-3s | %-6s", "No", "Title", "Author", "Publisher", "Year", "ISBN", "Shelf Location", "ID");
- for(int i = 0; i < arrTitle.size(); i++){
- for(int j = 0; j < a.get(i).length(); j++){
- if(a.get(i).regionMatches(true, j, search, 0, search.length())){
- 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));
- _isEmpty = false;
- break;
- }
- }
- }
- if(_isEmpty){
- System.out.println("\nNo Data Found");
- }
- System.out.print("\nPress enter to continue...");
- scan.nextLine();
- }
- public static void search(){
- int choose = 0;
- long isbn = 0;
- String search = "";
- clearScreen();
- do{
- System.out.println("Search By ___");
- System.out.println("1. Title");
- System.out.println("2. Author");
- System.out.println("3. Publisher");
- System.out.println("4. ISBN");
- System.out.println("5. Shelf Location");
- System.out.println("6. Cancel Search");
- try{
- System.out.print("Choose: ");
- choose = scan.nextInt();
- }catch(Exception e){}
- scan.nextLine();
- switch(choose){
- case 1:
- do{
- System.out.print("Input Title [not more than 35 characters]: ");
- search = scan.nextLine();
- }while(search.length() > 35);
- searchGeneral(arrTitle, search);
- break;
- case 2:
- do{
- System.out.print("Input Author Name [not more than 30 characters]: ");
- search = scan.nextLine();
- }while((search.length() > 30));
- searchGeneral(arrAuthor, search);
- break;
- case 3:
- do{
- System.out.print("Input Publisher [not more than 20 characters]: ");
- search = scan.nextLine();
- }while(search.length() > 20);
- searchGeneral(arrPublisher, search);
- break;
- case 4:
- do{
- System.out.println("Input ISBN: ");
- try{
- isbn = scan.nextLong();
- }catch(Exception e){}
- scan.nextLine();
- search = Long.toString(isbn);
- }while(search.length() != 13);
- searchGeneral(arrISBN, search);
- break;
- case 5:
- do{
- System.out.print("Input Shelf Location: ");
- search = scan.next();
- } while(!search.matches("[1-9]+[A-Ga-g]"));
- searchGeneral(arrShelf, search);
- break;
- }break;
- }while(choose != 6);
- }
- //string c for shelf
- public static void SelectionSort(ArrayList<String> x, ArrayList<String> y, ArrayList<String> z, ArrayList<String> a, ArrayList<String> b, ArrayList<String> c){
- for (int i=0; i<x.size()-1; i++) {
- for (int j=i+1; j<x.size(); j++) {
- if (x.get(i).compareTo(x.get(j)) > 0) {
- //... Exchange elements in first array
- String temp = x.get(i);
- x.set(i, x.get(j));
- x.set(j, temp);
- //... Exchange elements in second array
- temp = y.get(i);
- y.set(i, y.get(j));
- y.set(j, temp);
- //... Exchange elements in third array
- temp = z.get(i);
- z.set(i, z.get(j));
- z.set(j, temp);
- //... Exchange elements in forth array
- temp = a.get(i);
- a.set(i, a.get(j));
- a.set(j, temp);
- //... Exchange elements in fifth array
- temp = b.get(i);
- b.set(i, b.get(j));
- b.set(j, temp);
- // Exchange 6th element
- temp = c.get(i);
- c.set(i, c.get(j));
- c.set(j, temp);
- }
- }
- }
- }
- public static void sort(){
- int choose = 0;
- clearScreen();
- do{
- System.out.println("Sort by ____");
- System.out.println("1. Title");
- System.out.println("2. Author");
- System.out.println("3. Publisher");
- System.out.println("4. Year Published");
- System.out.println("5. Cancel Sort");
- try{
- System.out.print("Choose: ");
- choose = scan.nextInt();
- }catch(Exception e){}
- scan.nextLine();
- switch(choose){
- case 1:
- SelectionSort(arrTitle, arrAuthor, arrPublisher, arrYear, arrISBN, arrShelf);
- view();
- break;
- case 2:
- SelectionSort(arrAuthor, arrTitle, arrPublisher, arrYear, arrISBN, arrShelf);
- view();
- break;
- case 3:
- SelectionSort(arrPublisher, arrAuthor, arrTitle, arrYear, arrISBN, arrShelf);
- view();
- break;
- case 4:
- SelectionSort(arrYear, arrAuthor, arrPublisher, arrTitle, arrISBN, arrShelf);
- view();
- break;
- }break;
- }while(choose != 5);
- System.out.print("\nPress enter to continue...");
- scan.nextLine();
- }
- public static void delete(){
- int index = 0;
- clearScreen();
- view();
- do{
- System.out.print("\n\nInput number to be deleted [1-" + arrTitle.size() + "][0 for Cancel]: ");
- try{
- index = scan.nextInt();
- }catch(Exception e){}
- scan.nextLine();
- if(index == 0)
- break;
- else{
- arrTitle.remove(index - 1);
- arrAuthor.remove(index - 1);
- arrPublisher.remove(index - 1);
- arrYear.remove(index - 1);
- arrISBN.remove(index - 1);
- arrShelf.remove(index - 1);
- System.out.println("Data has been successfully deleted!");
- System.out.print("Press enter continue...");
- scan.nextLine();
- break;
- }
- }while(index >= arrTitle.size());
- }
- public static void main(String[] args) {
- int choose = 0;
- do{
- clearScreen();
- System.out.println("=== Library Book Management ===");
- System.out.println("1. Insert New Book");
- System.out.println("2. View Book List");
- System.out.println("3. Search Book");
- System.out.println("4. Sort Book List");
- System.out.println("5. Delete a Book");
- System.out.println("6. Exit");
- try{
- System.out.print("Choose: ");
- choose = scan.nextInt();
- }catch(Exception e){}
- scan.nextLine();
- switch(choose){
- case 1:
- insert();
- break;
- case 2:
- view();
- System.out.print("\nPress enter continue...");
- scan.nextLine();
- break;
- case 3:
- search();
- break;
- case 4:
- sort();
- break;
- case 5:
- delete();
- break;
- case 6:
- System.out.println("Thank you for using our app.");
- break;
- }
- }while(choose != 6);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment