溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java單鏈表怎么實現(xiàn)書籍管理系統(tǒng)

發(fā)布時間:2021-11-22 17:09:02 來源:億速云 閱讀:170 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“java單鏈表怎么實現(xiàn)書籍管理系統(tǒng)”,在日常操作中,相信很多人在java單鏈表怎么實現(xiàn)書籍管理系統(tǒng)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java單鏈表怎么實現(xiàn)書籍管理系統(tǒng)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

書籍管理系統(tǒng)功能:

1).添加圖書
2).刪除圖書
3).查看圖書
4).修改書籍
5).修改排序方式
6).模糊查詢
7).退出程序

代碼實現(xiàn):

Book類

package com.bookmanagement.book;

public class Book {//書類
 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方法
 @Override
 public String toString() {
  return "  Bookno=" + no + ",  Bookname=" + name + ",  Bookprice=" + price + ", Booktype=" + type;
 }
 
 
}

1).添加圖書

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("請輸入書編號:");
  String no = input.next();
  System.out.println("請輸入書名字:");
  String name = input.next();
  System.out.println("請輸入書價格:");
  int price = input.nextInt();
  System.out.println("請輸入書類型:");
  String type = input.next();
  Book bo = new Book(no,name,price,type);
  add(bo);
 }
 public static void add(Book bo) {
  Book temp = Test.head;//把頭節(jié)點賦值給一個輔助類
  boolean falg = false;
  while(true) {
   if(temp.next == null) {//判斷鏈表是否到最后
    break;
   }
   if(Test.stroing %2 == 1) {//判斷是否修改了顯示順序
    if(temp.next.no.compareToIgnoreCase(bo.no)<0) {//尋找適合的位置插入節(jié)點//跳過頭節(jié)點
     break;
    }else if(temp.next.no.compareToIgnoreCase(bo.no)==0){
     falg = true;
     break;
    }
   }else {
    if(temp.next.no.compareToIgnoreCase(bo.no)>0) {//尋找適合的位置插入節(jié)點//跳過頭節(jié)點
     break;
    }else if(temp.next.no.compareToIgnoreCase(bo.no)==0){
     falg = true;
     break;
    }
    
    
   }
   //節(jié)點后移
   temp = temp.next;
  }
  if(falg) {//判斷是否輸入相同的編號
   System.out.println("插入"+bo.no+"的數(shù)據(jù)編號已存在");
  }else {
   bo.next = temp.next;
   temp.next = bo;
  }
  
 }
 
}

2).刪除圖書

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("請輸入需要刪除圖書的編號:");
  String no = input.next();
  Book temp = Test.head;
  boolean falg = false;
  while(true) {
   if(temp.next == null) {//判斷鏈表是否到最后
    break;
   }
   if(temp.next.no.compareToIgnoreCase(no)==0) {
    falg = true;
    break;
   }
   temp = temp.next;//temp位移
  }
  if(falg) {
    temp.next=temp.next.next;//找到temp.next域指向刪除的編號讓下一個next覆蓋
    //如果需要刪除的編號下一個next域指向的是null則temp.next域則下一個指向為空
    System.out.println("刪除成功");
  }else {
   System.out.println("沒有找到該書籍");
  }
  
   
 }
}

3).查看圖書

package com.bookmanagement.function;
import com.bookmanagement.book.*;
public class ShowBook {
 public static void showbook() {
  if(Test.head.next == null) {
   System.out.println("沒有書籍?dāng)?shù)據(jù)");
   return;
  }
  Book temp = Test.head.next;//輸出頭節(jié)點下一個節(jié)點
  int sum=0;
  while(true) {
   if(temp == null) {
    break;
   }
   System.out.println(temp);
   sum++;
   temp = temp.next;//temp位移
  }
  System.out.println("書籍總數(shù)為:"+sum);
  
 }
}

4).修改書籍

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("請輸入需要修改的圖書的編號:");
  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("修改:1.名字 2.編號 3.價格 4.類型");
   int falg = input.nextInt();
   switch (falg) {
   case 1:
    System.out.println("請輸入需要修改的名字:");
    String name = input.next();
    temp.next.name = name;
    break;
   case 2:
    System.out.println("請輸入需要修改的編號:");
    String Mno = input.next();
    temp.next.no = Mno;
    Book change = temp.next;
    temp.next=temp.next.next;
    AddBook.add(change);
    //重新調(diào)用add方法
    break;
   case 3:
    System.out.println("請輸入需要修改的價格:");
    int prive = input.nextInt();
    temp.next.price = prive;
    break;
   case 4:
    System.out.println("請輸入需要修改的類型:");
    String type= input.next();
    temp.next.type = type;
    break;
   default:System.out.println("輸入有誤");
    break;
   }
  }else{
   System.out.println("沒有找到該書籍");
  } 
 }
}

5).修改排序方式

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;//把有數(shù)據(jù)的賦值給輔助類
  Book next = null;
  if(temp.next == null) {//鏈表只有一個數(shù)據(jù)不需要排序
   System.out.println("鏈表只有一個數(shù)據(jù)不需要逆序");
   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("修改為降序");
  }else {
   System.out.println("修改為升序");
  }
 }
}

6).模糊查詢

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("功能:模糊查詢");
   detalied1();
 }
 public static void detalied1() {
  System.out.println("輸入需要查找的數(shù)據(jù):1.書名2.編號3.價格4.類型");
  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("請輸入模糊書名:");
  String name = input.next();
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println("沒有書籍信息");
   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("沒有找到該書籍信息");
  }
 }
 public static void DetaBookNo() {
  System.out.println("請輸入模糊編號:");
  String no = input.next();
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println("沒有書籍信息");
   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("沒有找到該書籍信息");
  }
 }
 static int price;
 public static void DetaBookPrice() {
  System.out.print("輸入符號:(>,<,=,>=,<=,!=):");
  String symbol = input.next();
  System.out.print("輸入價格:");
  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("輸入錯誤");
   break;
  }
 }
 public static void GreaterPrice() {
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println("沒有書籍信息");
  }
  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("沒有書籍符合信息");
  }
 }
 public static void LessPrice() {
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println("沒有書籍信息");
  }
  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("沒有書籍符合信息");
  }
 }
 public static void EqualPrice() {
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println("沒有書籍信息");
  }
  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("沒有書籍符合信息");
  }
 }
 public static void GreaterEqualePrice() {
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println("沒有書籍信息");
  }
  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("沒有書籍符合信息");
  }
 }
 public static void LessEqualePrice() {
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println("沒有書籍信息");
  }
  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("沒有書籍符合信息");
  }
 }
 public static void NotEquale() {
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println("沒有書籍信息");
  }
  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("沒有書籍符合信息");
  }
 }
 public static void DetaBookType() {
  System.out.println("請輸入模糊類型:");
  String type = input.next();
  Book temp = Test.head;
  boolean falg = false;
  if(Test.head.next == null) {
   System.out.println("沒有書籍信息");
   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("沒有找到該書籍信息");
  }
 }
 
}

7).測試類

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,"");//建立鏈表頭
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.println("-----歡迎進(jìn)入圖書管理系統(tǒng)-----");
  boolean temp = true;
  while(temp) {
   System.out.println("1).添加圖書");
   System.out.println("2).刪除圖書");
   System.out.println("3).查看圖書");
   System.out.println("4).修改書籍");
   System.out.println("5).修改排序方式");
   System.out.println("6).模糊查詢");
   System.out.println("7).退出程序");
   int choose = input.nextInt();
   switch (choose) {
   case 1:
    AddBook.addbook();//添加書籍
    break;
   case 2:
    DropBook.dropbook();//刪除書籍
    break;
   case 3:
    ShowBook.showbook();//查看書籍
    break;
   case 4:
    Modify.modidy();//修改書籍
    break;
   case 5:
    stroing++;
    Flash.flashbook();//修改排序方式
    break;
   case 6:
    Detailed.detailed();//模糊查詢
    break;
   case 7:
    temp = false;//退出程序
    break;
   default:System.out.println("輸入錯誤");
    break;
   }
  }
  System.out.println("程序退出,歡迎下次使用");
  input.close();
 }
}

到此,關(guān)于“java單鏈表怎么實現(xiàn)書籍管理系統(tǒng)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI