Java 图书管理系统(实现CRUD)

Book 类

public class Book implements Comparable<Book>{
    
    
 
	private int id;
	private String bookName;
	private String author;
	private double price;
	
	public Book() {
    
     
		super();
	}
	
	public Book(int id, String bookName, String author, double price) {
    
    
		super();
		this.id = id;
		this.bookName = bookName;
		this.author = author;
		this.price = price;
	}
	
	public int getId() {
    
     
		return id;
	}
	
	@Override
	public int hashCode() {
    
    
		final int prime = 31;
		int result = 1;
		result = prime * result + ((bookName == null) ? 0 : bookName.hashCode());
		result = prime * result + id;
		return result;
	}
 
 
	@Override
	public boolean equals(Object obj) {
    
    
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (bookName == null) {
    
    
			if (other.bookName != null)
				return false;
		} else if (!bookName.equals(other.bookName))
			return false;
		if (id != other.id)
			return false;
		return true;
	}
 
	@Override
	public String toString(){
    
    
        return id +"\t"+  bookName + "\t"+  author +"\t"+  price ;
    }
 
	@Override
	public int compareTo(Book o) {
    
    
		// TODO Auto-generated method stub
		return this.getId()-o.getId();
	}

	public String getBookName() {
    
    
		return bookName;
	}

	public void setBookName(String bookName) {
    
    
		this.bookName = bookName;
	}

	public String getAuthor() {
    
    
		return author;
	}

	public void setAuthor(String author) {
    
    
		this.author = author;
	}

	public double getPrice() {
    
    
		return price;
	}

	public void setPrice(double price) {
    
    
		this.price = price;
	}

	public void setId(int id) {
    
    
		this.id = id;
	}
	
}

Library 类

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

public class Library {
    
    
    public static void main(String[] args) {
    
    
        HashSet<Book> hashSet=new HashSet<>();
        select(hashSet);
    }

    //功能选择   
    private static void select(HashSet<Book> hashSet) {
    
    
        Scanner s=new Scanner(System.in);
        boolean flag = true;
        while (flag) {
    
    
           System.out.println("***********功能列表************\n\t1.插入图书\n\t"
           		+ "2.删除图书\n\t3.修改图书\n\t4.查看图书\n\t5.退出\n请输入对应id(1-4)选择功能:");
           String selection = s.next();
    	   switch (selection){
    
    
           case "1":
               hashSet=addBook(hashSet);
               break;
           case "2":
               deleteBook(hashSet);
               break;
           case "3":
               changeBook(hashSet);
               break; 
           case "4":
               checkBook(hashSet);
               break;
           case "5":
               flag = false;
               break;
           default: 
               System.out.println("输入错误,请重新输入!");
               break;
    	    }
         }
      }

    //增
    private static HashSet<Book> addBook(HashSet<Book> hashSet) {
    
    
        Scanner s = new Scanner(System.in);
        boolean flag = true;
        do {
    
    
        	System.out.println("插入图书:");
            System.out.print("input bookId:");
            int id=s.nextInt();
            System.out.print("input bookName:");
            String bookName=s.next();
            System.out.print("input bookAuthor:");
            String author=s.next();
            System.out.print("input bookPrice");
            double price=s.nextDouble();
            hashSet.add(new Book(id, bookName, author, price));
            System.out.println("是否继续插入Y/N?");
            String isNext=s.next();
            if (isNext.equalsIgnoreCase("n")){
    
    
                System.out.println("插入完毕!");
                flag = false;
                checkBook(hashSet); 
            } else if (isNext.equalsIgnoreCase("y")){
    
    
            	System.out.println("继续插入");
    		}else{
    
    
    			System.out.println("输入错误,默认已退出!!!");
    			checkBook(hashSet);
    			flag = false;
            }
		} while (flag);
        
        return hashSet;
    }

    //删
    private static HashSet<Book> deleteBook(HashSet<Book> hashSet) {
    
    
        System.out.println("删除图书");
        System.out.println("请输入要删除书籍的ID:");
        Scanner s = new Scanner(System.in);
        int deleteId=s.nextInt();
        ArrayList<Book> arrayList=new ArrayList<>(hashSet);
        Collections.sort(arrayList);
        Iterator<Book> iterator=arrayList.iterator();
        while (iterator.hasNext()){
    
    
            Book b = (Book) iterator.next();
            if(b.getId()==deleteId){
    
    
                iterator.remove();
            }
        }
        //这里稍微需要注意一下,因为CRUD返回的都是HashSet,所以把删除后的arrayList里面的值重新倒给hashSet,进行输出和返回
        hashSet=new HashSet<Book>(arrayList);
        checkBook(hashSet);
        select(hashSet);
        return hashSet;
    }
 
    //改
    private static void changeBook(HashSet<Book> hashSet) {
    
    
        System.out.println("修改图书");
        System.out.println("请输入要修改书籍的ID:");
        Scanner s = new Scanner(System.in);
        int alterId=s.nextInt();
        ArrayList<Book> arrayList=new ArrayList<>(hashSet);
        Collections.sort(arrayList);
        Iterator<Book> iterator=arrayList.iterator();
        while (iterator.hasNext()){
    
    
            Book b=(Book) iterator.next();
            if(b.getId()==alterId){
    
    
            	System.out.print("请输入要修改的书籍的名字:");
            	String bookName = s.next();
            	b.setBookName(bookName);
            	System.out.print("请输入要修改的书籍作者的名字:");
            	String author = s.next();
            	b.setAuthor(author);
            	System.out.print("请输入要修改的书籍的价格:");
            	Double price = s.nextDouble();
            	b.setPrice(price);
            }
        }
        select(hashSet);
    }

    //查看图书
    private static void checkBook(HashSet<Book> hashSet) {
    
    
        System.out.println("=============查看图书==================");
        System.out.println("id\tName\tauthor\tprice");
        ArrayList<Book> arrayList=new ArrayList<>(hashSet);
        Collections.sort(arrayList);
        Iterator<Book> iterator=arrayList.iterator();
        while (iterator.hasNext()){
    
    
            Book book = (Book) iterator.next();
            System.out.println(book);
        }
        System.out.println("=====================================");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44322234/article/details/109538707