比较适合新手练手的一个简易项目

直接上源码

Book

package code;
/**
 * @author ztr
 * @version 创建时间:2021年3月4日 下午8:21:40
 * 类说明
 */
public class Book {
    private String name;
    private String author;
    private double price;
    private String type;
//    有参构造器
    public Book(String name, String author, double price, String type) {
        super();
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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 String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    
}

Cuser

package code;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * @author ztr
 * @version 创建时间:2021年3月4日 下午8:32:15 类说明
 */
public class Cuser extends User {
    Scanner sc = new Scanner(System.in);
    public Cuser(String name, String sex, int age) {
        super(name, sex, age);
    }
//    查找书籍
    @Override
    public void serach(List<Book> LibraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要查询的书籍名称");
        String name = sc.next();
        for (int i = 0; i < LibraryBookDate.size(); i++) {
            if (name != null) {
                if (LibraryBookDate.get(i).getName().equals(name)) {
                    System.out.println("查询成功");
                    System.out.println(LibraryBookDate.get(i).getName() + " "
                            + LibraryBookDate.get(i).getAuthor() + " "
                            + LibraryBookDate.get(i).getPrice() + " "
                            + LibraryBookDate.get(i).getType());
                }
            } else {
                System.out.println("没有此书");
            }
        }
        

    }
//遍历图书
    @Override
    public void display(List<Book> LibraryBookDate) {
        // 使用lambda表达式
        Iterator<Book> it = LibraryBookDate.iterator();
        while (it.hasNext()) {
            // 因为it.next()返回的是object类型所以需要进行类型强转
            Book book = (Book) it.next();
            System.out.println(book.getName() + " " + book.getAuthor() + " "
                    + book.getPrice() + " " + book.getType());
        }
    }
//借阅书籍
    @Override
    public void borrow(List<Book> LibraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要借阅的书籍书名:");
        String name = sc.next();
        // 获取集合的迭代器
        Iterator<Book> it = LibraryBookDate.iterator();
        while (it.hasNext()) {
            Book book = (Book) it.next();
            if (book.getName().equals(name)) {
                System.out.println(book.getName() + " " + book.getAuthor()
                        + " " + book.getPrice() + " " + book.getType());
                UserBookDate.userdateBooks.add(book);
                it.remove();
            } else {
                System.out.println("没有该书籍");
            }
        }
        
    }
//    归还书籍
    public void revert(List<Book> LibraryBookDate){
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要归还的书籍:");
        String name = sc.next();
        Iterator<Book> it = UserBookDate.userdateBooks.iterator();
        while(it.hasNext()){
            Book book = (Book)it.next();
            if(book.getName().equals(name)){
                it.remove();
                LibraryBookDate.add(book);
            }            
        }
        
    }
}

LibraryBookDate

package code;

import java.util.ArrayList;
import java.util.List;



/**
 * @author ztr
 * @version 创建时间:2021年3月4日 下午9:21:13
 * 类说明
 */
public class LibraryBookDate {
    static List<Book> LibraryBookDate = new ArrayList<Book>();
    
}

Menu

ackage code;

import java.util.Scanner;

/**
 * @author ztr
 * @version 创建时间:2021年3月6日 下午4:28:07 类说明
 */
public class Menu {
    public void menuRuser() {
        Ruser ruser = new Ruser("ztr", "man", 20);
        System.out.println("1.查阅书籍" + "/n" + "2.遍历书籍" + "/n" + "3.删除书籍"
                + "4.添加书籍" + "5.返回上一层");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        switch (n) {

        case 1:
            ruser.serach(LibraryBookDate.LibraryBookDate);
            menuRuser();
            break;
        case 2:
            ruser.display(LibraryBookDate.LibraryBookDate);
            menuRuser();
            break;
        case 3:
            ruser.borrow(LibraryBookDate.LibraryBookDate);
        
            menuRuser();
            break;
        case 4:
            ruser.insert(LibraryBookDate.LibraryBookDate);        
            menuRuser();
            break;
        case 5:
            menu();
            break;
        default:
            System.out.println("请重新输入:");
            menuRuser();
            break;
        }
        sc.close();
    }

    public void menuCuser() {
        Cuser cuser = new Cuser("x", "man", 10);
        System.out.println("1.查阅书籍" + "/n" + "2.遍历书籍" + "/n" + "3.借阅书籍"
                + "4.归还书籍" + "5.返回上一层");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        switch (n) {

        case 1:
            cuser.serach(LibraryBookDate.LibraryBookDate);
            menuCuser();
            break;
        case 2:
            cuser.display(LibraryBookDate.LibraryBookDate);
            menuCuser();
            break;
        case 3:
            cuser.borrow(LibraryBookDate.LibraryBookDate);
            menuCuser();
            break;
        case 4:
            cuser.revert(LibraryBookDate.LibraryBookDate);
            menuCuser();
            break;
        case 5:
            menu();
            break;
        default:
            System.out.println("请重新输入:");
            menuCuser();
            break;
        }
        sc.close();
    }

    public void menu() {
        System.out.println("====图书馆管理系统===");
        System.out.println("1.用户  2.管理员  3.退出");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        switch (n) {
        case 1:
            menuCuser();
            break;
        case 2:
            menuRuser();
        case 3:
            break;
        default:
            System.out.println("请重新输入");
            break;
        }
        sc.close();
    }

}

Operate

package code;

import java.util.List;

/**
 * @author ztr
 * @version 创建时间:2021年3月4日 下午8:25:01
 * 类说明
 */
public interface Operate {
    public void serach(List<Book> LibraryBookDate);
    public void display(List<Book> LibraryBookDate);
    public void borrow(List<Book> LibraryBookDate);
}

Ruser

package code;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * @author ztr
 * @version 创建时间:2021年3月6日 下午3:56:25 类说明
 */
public class Ruser extends User {
    Scanner sc = new Scanner(System.in);
    public Ruser(String name, String sex, int age) {
        super(name, sex, age);
    }

    // 查找书籍
    @Override
    public void serach(List<Book> LibraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要查找的书籍书名:");
        String name = sc.next();
        // 获取集合的迭代器
        Iterator<Book> it = LibraryBookDate.iterator();
        while (it.hasNext()) {
            Book book = (Book) it.next();
            if (book.getName().equals(name)) {
                System.out.println(book.getName() + " " + book.getAuthor()
                        + " " + book.getPrice() + " " + book.getType());
                UserBookDate.userdateBooks.add(book);
            } else {
                System.out.println("没有该书籍");
            }
        }
    

    }

    // 遍历图书
    @Override
    public void display(List<Book> LibraryBookDate) {
        // 使用lambda表达式
        Iterator<Book> it = LibraryBookDate.iterator();
        while (it.hasNext()) {
            // 因为it.next()返回的是object类型所以需要进行类型强转
            Book book = (Book) it.next();
            System.out.println(book.getName() + " " + book.getAuthor() + " "
                    + book.getPrice() + " " + book.getType());
        }
    }

    // 删除图书
    @Override
    public void borrow(List<Book> LibraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除书籍的书名:");
        String name = sc.next();
        // 获取集合的迭代器
        Iterator<Book> it = LibraryBookDate.iterator();
        while (it.hasNext()) {
            Book book = (Book) it.next();
            if (book.getName().equals(name)) {
                System.out.println(book.getName() + " " + book.getAuthor()
                        + " " + book.getPrice() + " " + book.getType());
                it.remove();
            } else {
                System.out.println("没有该书籍");
            }
        }
    }

    // 添加图书
    public void insert(List<Book> libraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请依次输入锁添加书籍的信息:1.书名2.作者3价格4类型");
        String name = sc.next();
        String author = sc.next();
        double price = sc.nextDouble();
        String type = sc.next();
        Book book = new Book(name, author, price, type);
        libraryBookDate.add(book);
        
    }
}

User

package code;
/**
 * @author ztr
 * @version 创建时间:2021年3月4日 下午8:24:46
 * 类说明
 */
public abstract class User implements Operate{
    private String name;
    private String sex;
    private int age;
    
    public User(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}

UserBookDate

package code;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ztr
 * @version 创建时间:2021年3月6日 下午2:07:20
 * 类说明
 */
public class UserBookDate {
    static List<Book> userdateBooks = new ArrayList<Book>();
}

Test

package code;
/**
 * @author ztr
 * @version 创建时间:2021年3月6日 下午4:47:25
 * 类说明
 */
public class Test {
    public static void main(String[] args){
        Menu menuone = new Menu();
        menuone.menu();
    }
}

Fileio

package FileIo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import code.Book;
import code.LibraryBookDate;

/**
 * @author ztr
 * @version 创建时间:2021年3月6日 下午9:29:18 类说明
 */
public class Fileio {
    public void fileread() {
        // 创建字符缓冲流对象
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("src/book.txt"));

            String line;
            while ((line = br.readLine()) != null) {
                // 把读取到的字符串数据存储到集合中
                String[] strArray = line.split(" ");
                String name = strArray[0];
                String author = strArray[1];
                double price = Double.parseDouble(strArray[2]);
                String type = strArray[3];
                // 创建书本对象
                Book book = new Book(name, author, price, type);
                LibraryBookDate.LibraryBookDate.add(book);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {// 写文件是会有IO异常
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            // 为了保障close一定执行
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void filewrite() {
        // 创建输出缓冲流对象
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("src/book.txt"));
            // 遍历集合
            Iterator<Book> iterator = LibraryBookDate.LibraryBookDate
                    .iterator();
            while (iterator.hasNext()) {
                Book book = (Book) iterator.next();
                bw.write(book.getName() + " ");
                bw.write(book.getAuthor() + " ");
                bw.write(String.valueOf(book.getPrice()) + " ");
                bw.write(book.getType() + " ");
                bw.newLine();
                // 强制数据输出
                bw.flush();
            }
        } catch (FileNotFoundException e) {
            // TODO: handle exception
        } catch (IOException e) {
            e.printStackTrace();
            // TODO: handle exception
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
            }
        }
    }
}

 

功能非常简单,但可以自己进行功能的扩展

笨鸟先飞