欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java单链表实现书籍管理系统

程序员文章站 2022-06-18 16:14:15
本文实例为大家分享了java单链表实现书籍管理系统的具体代码,供大家参考,具体内容如下书籍管理系统功能:1).添加图书2).删除图书3).查看图书4).修改书籍5).修改排序方式6).模糊查询7).退...

本文实例为大家分享了java单链表实现书籍管理系统的具体代码,供大家参考,具体内容如下

书籍管理系统功能:

1).添加图书
2).删除图书
3).查看图书
4).修改书籍
5).修改排序方式
6).模糊查询
7).退出程序

代码实现:

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;//把头节点赋值给一个辅助类
  boolean falg = false;
  while(true) {
   if(temp.next == null) {//判断链表是否到最后
    break;
   }
   if(test.stroing %2 == 1) {//判断是否修改了显示顺序
    if(temp.next.no.comparetoignorecase(bo.no)<0) {//寻找适合的位置插入节点//跳过头节点
     break;
    }else if(temp.next.no.comparetoignorecase(bo.no)==0){
     falg = true;
     break;
    }
   }else {
    if(temp.next.no.comparetoignorecase(bo.no)>0) {//寻找适合的位置插入节点//跳过头节点
     break;
    }else if(temp.next.no.comparetoignorecase(bo.no)==0){
     falg = true;
     break;
    }
    
    
   }
   //节点后移
   temp = temp.next;
  }
  if(falg) {//判断是否输入相同的编号
   system.out.println("插入"+bo.no+"的数据编号已存在");
  }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("没有书籍数据");
   return;
  }
  book temp = test.head.next;//输出头节点下一个节点
  int sum=0;
  while(true) {
   if(temp == null) {
    break;
   }
   system.out.println(temp);
   sum++;
   temp = temp.next;//temp位移
  }
  system.out.println("书籍总数为:"+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);
    //重新调用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;//把有数据的赋值给辅助类
  book next = null;
  if(temp.next == null) {//链表只有一个数据不需要排序
   system.out.println("链表只有一个数据不需要逆序");
   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("输入需要查找的数据: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("-----欢迎进入图书管理系统-----");
  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();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。