java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng)
本文實(shí)例為大家分享了java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
書籍管理系統(tǒng)功能:
1).添加圖書
2).刪除圖書
3).查看圖書
4).修改書籍
5).修改排序方式
6).模糊查詢
7).退出程序
代碼實(shí)現(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("請輸入書價(jià)格:"); 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é)點(diǎn)賦值給一個(gè)輔助類 boolean falg = false; while(true) { if(temp.next == null) {//判斷鏈表是否到最后 break; } if(Test.stroing %2 == 1) {//判斷是否修改了顯示順序 if(temp.next.no.compareToIgnoreCase(bo.no)<0) {//尋找適合的位置插入節(jié)點(diǎn)//跳過頭節(jié)點(diǎn) break; }else if(temp.next.no.compareToIgnoreCase(bo.no)==0){ falg = true; break; } }else { if(temp.next.no.compareToIgnoreCase(bo.no)>0) {//尋找適合的位置插入節(jié)點(diǎn)//跳過頭節(jié)點(diǎn) break; }else if(temp.next.no.compareToIgnoreCase(bo.no)==0){ falg = true; break; } } //節(jié)點(diǎn)后移 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域指向刪除的編號讓下一個(gè)next覆蓋 //如果需要刪除的編號下一個(gè)next域指向的是null則temp.next域則下一個(gè)指向?yàn)榭? 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é)點(diǎn)下一個(gè)節(jié)點(diǎn) 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.價(jià)格 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("請輸入需要修改的價(jià)格:"); 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) {//鏈表只有一個(gè)數(shù)據(jù)不需要排序 System.out.println("鏈表只有一個(gè)數(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.價(jià)格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("輸入價(jià)格:"); 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(); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
nacos在liunx系統(tǒng)中啟動成功瀏覽器卻訪問不了的解決方法
在linux下搭建nacos,現(xiàn)在想要啟動,訪問nacos頁面,訪問不了,所以本文小編將給大家介紹nacos在liunx系統(tǒng)中啟動成功,瀏覽器卻訪問不了?全面的解決辦法,需要的朋友可以參考下2023-09-09處理Log4j2不能打印行號的問題(AsyncLogger)
這篇文章主要介紹了處理Log4j2不能打印行號的問題(AsyncLogger),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12在Spring Boot中使用swagger-bootstrap-ui的方法
這篇文章主要介紹了在Spring Boot中使用swagger-bootstrap-ui的方法,需要的朋友可以參考下2018-01-01詳解hashCode()和equals()的本質(zhì)區(qū)別和聯(lián)系
這篇文章主要介紹了詳解hashCode()和equals()的本質(zhì)區(qū)別和聯(lián)系,本文先對兩種方法作了介紹,然后對二者聯(lián)系進(jìn)行分析,具有一定參考價(jià)值,需要的朋友可以了解下。2017-09-09