您的位置:首页 > 编程语言 > Java开发

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

2020-07-13 06:13 591 查看

书籍管理系统功能:
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: