Realization of Book Management System by java Single Linked List

  • 2021-12-13 08:02:23
  • OfStack

This article example for everyone to share the java single linked list to achieve book management system specific code, for your reference, the specific content is as follows

Book management system functions:

1). Add a book
2). Delete books
3). Check books
4). Revise books
5). Modify the sort
6). Fuzzy query
7). Exit the procedure

Code implementation:

Class Book


package com.bookmanagement.book;

public class Book {// Book category 
 public String no;
 public String name;
 public int price;
 public String type;
 public Book next;
 
 public Book(String Bno,String Bname,int Bprive,String Btype) {
  this.no=Bno;
  this.name=Bname;
  this.price=Bprive;
  this.type=Btype;
 }
 public Book() {
  
 }
 
 //toString Method 
 @Override
 public String toString() {
  return "  Bookno=" + no + ",  Bookname=" + name + ",  Bookprice=" + price + ", Booktype=" + type;
 }
 
 
}

1). Add a book


package com.bookmanagement.function;
import java.util.Scanner;
import com.bookmanagement.book.*;
public class AddBook {
 static Scanner input = new Scanner(System.in);
 public static void addbook() {
  
  System.out.println(" Please enter the book number :");
  String no = input.next();
  System.out.println(" Please enter the name of the book :");
  String name = input.next();
  System.out.println(" Please enter the book price :");
  int price = input.nextInt();
  System.out.println(" Please enter a book type :");
  String type = input.next();
  Book bo = new Book(no,name,price,type);
  add(bo);
 }
 public static void add(Book bo) {
  Book temp = Test.head;// Assign the header node to 1 Auxiliary classes 
  boolean falg = false;
  while(true) {
   if(temp.next == null) {// Determine whether the linked list is at the end 
    break;
   }
   if(Test.stroing %2 == 1) {// Determining whether the display order has been modified 
    if(temp.next.no.compareToIgnoreCase(bo.no)<0) {// Find a suitable position to insert a node // Head hopping node 
     break;
    }else if(temp.next.no.compareToIgnoreCase(bo.no)==0){
     falg = true;
     break;
    }
   }else {
    if(temp.next.no.compareToIgnoreCase(bo.no)>0) {// Find a suitable position to insert a node // Head hopping node 
     break;
    }else if(temp.next.no.compareToIgnoreCase(bo.no)==0){
     falg = true;
     break;
    }
    
    
   }
   // Node moving backward 
   temp = temp.next;
  }
  if(falg) {// Determine whether to enter the same number 
   System.out.println(" Insert "+bo.no+" The data number of is already present ");
  }else {
   bo.next = temp.next;
   temp.next = bo;
  }
  
 }
 
}

2). Delete books


package com.bookmanagement.function;
import java.util.Scanner;
import com.bookmanagement.book.*;
public class DropBook {
 static Scanner input = new Scanner(System.in);
 public static void dropbook() {
  System.out.println(" Please enter the number of the book to be deleted :");
  String no = input.next();
  Book temp = Test.head;
  boolean falg = false;
  while(true) {
   if(temp.next == null) {// Determine whether the linked list is at the end 
    break;
   }
   if(temp.next.no.compareToIgnoreCase(no)==0) {
    falg = true;
    break;
   }
   temp = temp.next;//temp Displacement 
  }
  if(falg) {
    temp.next=temp.next.next;// Find temp.next The field points to the deleted number 1 A next Covering 
    // If you need to delete the number under 1 A next The domain points to the null Then temp.next Domain rule 1 Point to null 
    System.out.println(" Delete succeeded ");
  }else {
   System.out.println(" The book was not found ");
  }
  
   
 }
}

3). Check books


package com.bookmanagement.function;
import com.bookmanagement.book.*;
public class ShowBook {
 public static void showbook() {
  if(Test.head.next == null) {
   System.out.println(" There is no book data ");
   return;
  }
  Book temp = Test.head.next;// Output head node 1 Nodes 
  int sum=0;
  while(true) {
   if(temp == null) {
    break;
   }
   System.out.println(temp);
   sum++;
   temp = temp.next;//temp Displacement 
  }
  System.out.println(" The total number of books is :"+sum);
  
 }
}

4). Revise books


package com.bookmanagement.function;
import java.util.Scanner;
import com.bookmanagement.book.*;
public class Modify {
 static Scanner input = new Scanner(System.in);
 public static void modidy() {
  System.out.println(" Please enter the number of the book to be modified :");
  String no = input.next();
  Book temp = Test.head;
  boolean ts = false;
  while(true) {
   if(temp.next == null) {
    break;
   }
   if(temp.next.no.compareToIgnoreCase(no)==0) {
    ts = true;
    break;
   }
   temp = temp.next;
  }
  if(ts) {
   System.out.println(" Modify :1. Name  2. Numbering  3. Price  4. Type ");
   int falg = input.nextInt();
   switch (falg) {
   case 1:
    System.out.println(" Please enter the name you want to modify :");
    String name = input.next();
    temp.next.name = name;
    break;
   case 2:
    System.out.println(" Please enter the number to be modified :");
    String Mno = input.next();
    temp.next.no = Mno;
    Book change = temp.next;
    temp.next=temp.next.next;
    AddBook.add(change);
    // Recall add Method 
    break;
   case 3:
    System.out.println(" Please enter the price to be modified :");
    int prive = input.nextInt();
    temp.next.price = prive;
    break;
   case 4:
    System.out.println(" Please enter the type to modify :");
    String type= input.next();
    temp.next.type = type;
    break;
   default:System.out.println(" Input error ");
    break;
   }
  }else{
   System.out.println(" The book was not found ");
  } 
 }
}

5). Modify the sort


package com.bookmanagement.function;
import java.util.Scanner;
import com.bookmanagement.book.*;
public class Flash {
 static Scanner input = new Scanner(System.in);
 public static void flashbook() {
  Book everList = new Book("","",0,"");
  Book temp = Test.head.next;// Assign values with data to auxiliary classes 
  Book next = null;
  if(temp.next == null) {// Linked list only 1 Data does not need to be sorted 
   System.out.println(" Linked list only 1 Data does not need to be reversed ");
   return;
  }
  while(temp != null) {
   next = temp.next;
   temp.next = everList.next;
   everList.next = temp;
   temp = next;
  }
  Test.head.next = everList.next;
  if(Test.stroing%2==1) {
   System.out.println(" Modify to descending order ");
  }else {
   System.out.println(" Modify to ascending order ");
  }
 }
}

6). Fuzzy query


package com.bookmanagement.function;
import com.bookmanagement.book.*;
import java.util.Scanner;
public class Detailed {
 static Scanner input = new Scanner(System.in);
 public static void detailed() {
  System.out.println(" Function : Fuzzy query ");
   detalied1();
 }
 public static void detalied1() {
  System.out.println(" Enter the data to find :1. Book title 2. Numbering 3. Price 4. Type ");
  int falg = input.nextInt();
  switch (falg) {
  case 1:
   DetaBookName();
   break;
  case 2:
   DetaBookNo();
   break;
  case 3:
   DetaBookPrice();
   break;
  case 4:
   DetaBookType();
   break;
  default:
   break;
  }
 }
 public static void DetaBookName() {
  System.out.println(" Please enter a fuzzy title :");
  String name = input.next();
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println(" No book information ");
   return;
  }
  while(true) {
   if(temp.next == null) {
    break;
   }
   if(temp.next.name.indexOf(name)==0) {
    System.out.println(temp.next);
    falg = true;
   }
   temp = temp.next;
  }
  if(!falg) {
   System.out.println(" The book information was not found ");
  }
 }
 public static void DetaBookNo() {
  System.out.println(" Please enter a fuzzy number :");
  String no = input.next();
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println(" No book information ");
   return;
  }
  while(true) {
   if(temp.next == null) {
    break;
   }
   if(temp.next.no.indexOf(no)==0) {
    System.out.println(temp.next);
    falg = true;
   }
   temp = temp.next;
  }
  if(!falg) {
   System.out.println(" The book information was not found ");
  }
 }
 static int price;
 public static void DetaBookPrice() {
  System.out.print(" Input symbol :(>,<,=,>=,<=,!=):");
  String symbol = input.next();
  System.out.print(" Enter price :");
  price = input.nextInt();
  System.out.println();
  switch (symbol) {
  case ">":
   GreaterPrice();
   break;
  case "<":
   LessPrice();
   break;
  case "=":
   EqualPrice();
   break;
  case ">=":
   GreaterEqualePrice();
   break;
  case "<=":
   LessEqualePrice();
   break;
  case "!=":
   NotEquale();
   break;
  default:System.out.println(" Input error ");
   break;
  }
 }
 public static void GreaterPrice() {
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println(" No book information ");
  }
  while(true) {
   if(temp.next == null) {
    break;
   }
   if(temp.next.price>price) {
    System.out.println(temp.next);
    falg = true;
   }
   temp = temp.next;
  }
  if(!falg) {
   System.out.println(" No books match the information ");
  }
 }
 public static void LessPrice() {
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println(" No book information ");
  }
  while(true) {
   if(temp.next == null) {
    break;
   }
   if(temp.next.price<price) {
    System.out.println(temp.next);
    falg = true;
   }
   temp = temp.next;
  }
  if(!falg) {
   System.out.println(" No books match the information ");
  }
 }
 public static void EqualPrice() {
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println(" No book information ");
  }
  while(true) {
   if(temp.next == null) {
    break;
   }
   if(temp.next.price==price) {
    System.out.println(temp.next);
    falg = true;
   }
   temp = temp.next;
  }
  if(!falg) {
   System.out.println(" No books match the information ");
  }
 }
 public static void GreaterEqualePrice() {
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println(" No book information ");
  }
  while(true) {
   if(temp.next == null) {
    break;
   }
   if(temp.next.price>=price) {
    System.out.println(temp.next);
    falg = true;
   }
   temp = temp.next;
  }
  if(!falg) {
   System.out.println(" No books match the information ");
  }
 }
 public static void LessEqualePrice() {
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println(" No book information ");
  }
  while(true) {
   if(temp.next == null) {
    break;
   }
   if(temp.next.price<=price) {
    System.out.println(temp.next);
    falg = true;
   }
   temp = temp.next;
  }
  if(!falg) {
   System.out.println(" No books match the information ");
  }
 }
 public static void NotEquale() {
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println(" No book information ");
  }
  while(true) {
   if(temp.next == null) {
    break;
   }
   if(temp.next.price!=price) {
    System.out.println(temp.next);
    falg = true;
   }
   temp = temp.next;
  }
  if(!falg) {
   System.out.println(" No books match the information ");
  }
 }
 public static void DetaBookType() {
  System.out.println(" Please enter a fuzzy type :");
  String type = input.next();
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println(" No book information ");
   return;
  }
  while(true) {
   if(temp.next == null) {
    break;
   }
   if(temp.next.type.indexOf(type)==0) {
    System.out.println(temp.next);
    falg = true;
   }
   temp = temp.next;
  }
  if(!falg) {
   System.out.println(" The book information was not found ");
  }
 }
 
}

7). Test classes


package com.bookmanagement.function;

import java.util.Scanner;
import com.bookmanagement.book.*;
public class Test {
 static int stroing=0;
 public static Book head = new Book("","",0,"");// Establish linked list header 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.println("----- Welcome to the library management system -----");
  boolean temp = true;
  while(temp) {
   System.out.println("1). Add Books ");
   System.out.println("2). Delete a book ");
   System.out.println("3). Check books ");
   System.out.println("4). Revise a book ");
   System.out.println("5). Modify the sorting method ");
   System.out.println("6). Fuzzy query ");
   System.out.println("7). Exit program ");
   int choose = input.nextInt();
   switch (choose) {
   case 1:
    AddBook.addbook();// Add a book 
    break;
   case 2:
    DropBook.dropbook();// Delete a book 
    break;
   case 3:
    ShowBook.showbook();// Check books 
    break;
   case 4:
    Modify.modidy();// Revise a book 
    break;
   case 5:
    stroing++;
    Flash.flashbook();// Modify the sorting method 
    break;
   case 6:
    Detailed.detailed();// Fuzzy query 
    break;
   case 7:
    temp = false;// Exit program 
    break;
   default:System.out.println(" Input error ");
    break;
   }
  }
  System.out.println(" Program exit , Welcome to use it next time ");
  input.close();
 }
}

Related articles: