Java图书管理系统(JavaSE)

此项目,适合在Java入门阶段的小白学习,整个项目运用了java基础阶段所有的知识点。

1、项目功能大纲

        项目技术点:面向对象,集合,IO,线程等技术

        author:冯宝宝

        项目功能:如图所示

2、功能模块

2.1、登陆(管理员登陆,普通用户登陆)

2.2、管理员登陆

        首页展示

        显示所有图书

        整理图书

        查询图书

        添加图书

        修改图书

        下架图书

        退出系统

2.3、普通用户登陆

        首页展示

        显示所有图书

        查询图书

        借阅图书

        下载电子图书

        退出系统

3、项目架构(三层架构思想)

3.1、什么是三层架构

三层架构来源于后端开发的一种分层的思想

三层架构就是为了符合“高内聚,低耦合”思想,把各个功能模块划分为

表示层(UI):主要对用户的请求接受,以及数据的返回,为客户端提供应用程序的访问。

业务逻辑层(BLL):主要负责对数据层的操作。也就是说把一些数据层的操作进行组合。

数据访问层(DAL):主要用于实现对数据库的访问和操作。

三层架构,各层之间采用接口相互访问,并通过对象模型的实体类(Model)作为数据传递的载体,不同的对象模型的实体类一般对应于数据库的不同表,实体类的属性与数据库表的字段名一致。

3.2、三层架构的好处

三层架构区分层次的目的是为了 “高内聚,低耦合”

开发人员分工更明确,将精力更专注于应用系统核心业务逻辑的分析、设计和开发,加快项目的进度,提高了开发效率,有利于项目的更新和维护工作。

3.3、三层架构图

 

 3.4、三层架构的优点

  1. 高内聚、低耦合,可以降低层与层之间的依赖。
  2. 各层互相独立,完成自己该完成的任务,项目可以多人同时开发,开发人员可以只关注整个结构中的其中某一层。
  3. 容易移植、维护,如 B / S 转 C / S、SQLServer 转 Oracle、添加、修改、删除等。
  4. 有利于标准化。
  5. 有利于各层逻辑的复用。
  6. 安全性高。用户端只能通过业务逻辑层来调用数据访问层,减少了入口点,把很多危险的系统功能都屏蔽了

3.5、三层架构开发原理

实体类-------数据访问层-------业务逻辑层-------表示层 -------(工具类)

pojo             dao                     service              controller         utils

4、项目开发

4.1、准备工作

admin.txt(用户文本)  

注意:这里在txt文本里面定义了   管理员登录和普通用户登录   分别为用户名和密码   可自定义!

admin1 123456 

admin2 66666 

admin3 123123 

-----------------------------------

张三 123 

李四 456 

王五 55555 

图书馆(书记)

 图书下载文档(用来存放下载的图书)

 序号  作者  图书名称   价格   图书路径   图书数量

4.2、登录

用户实体类(User)
public class User{
    private String username;
    private String password;
    private int level;          //0(管理员) 或 1(普通用户)

    public User() {
    }

    public User(String username, String password, int level) {
        this.username = username;
        this.password = password;
        this.level = level;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", level=" + level +
                '}';
    }

    //equals方法登录时比较输入的用户名和密码  比较输的用户名密码 
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return
                Objects.equals(username, user.username) &&
                Objects.equals(password, user.password);
    }

    @Override
    public int hashCode() {
        return Objects.hash(username, password, level);
    }
}

dao层(UserMapper接口)
public interface UserMapper {
    //登录
    User selectUser(User user);
}
UserMapperImpl实现类
public class UserMapperImpl implements UserMapper {

    //定义List集合    通过io流读取   admin.txt  内容,存到userList集合
    private static List<User> userList;

    static {
        userList = UserUtil.getUserList();
    }

    //登录
    @Override
    public User selectUser(User user) {
        for (int i = 0; i < userList.size(); i++) {
            if (user.equals(userList.get(i))){
                return userList.get(i);
            }
        }
        return null;
    }
    
}
UserUtil(工具类)
public class UserUtil {
    //定义List集合
    private static ArrayList<User> userList = new ArrayList<>();
    //将用户信息存在到list2集合中
    public static List<User> getUserList(){
        BufferedReader br1 = null;
        try {
            br1 = new BufferedReader(new FileReader("day26\\admin.txt"));
            String line1;
            while ((line1 = br1.readLine()) != null) {
                String[] split = line1.split(" ");
                userList.add(new User(split[0], split[1], Integer.parseInt(split[2])));
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (br1 != null){
                try {
                    br1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //返回一个集合
        return userList;
    }

}
service层(UserService接口)
public interface UserService {
    //登录(权限校验)
    User login();
}
UserServiceImpl实现类
public class UserServiceImpl implements UserService {

    //接口new实现类
    UserMapper userMapper = new UserMapperImpl();
    Scanner sc = new Scanner(System.in);


    //登录(权限校验) 
    @Override
    public User login() {
           String username = MyUtil.getInputString("请输入用户名",sc);
           String password = MyUtil.getInputString("请输入密码",sc);
           User user = new User();
           user.setUsername(username);
           user.setPassword(password);
           return userMapper.selectUser(user);
    }
}
MyUtil(键盘录入工具类)
//键盘录入工具类
public class MyUtil {

    /**
     * 得到指定区间的随机数
     */
    public static int getRandom(int min,int max){
        return (int)(Math.random()*(max-min)+min);
    }
    /**
     * 给用户提示的同时,请用户输入一个整数
     */
    public static int getInput(String msg, Scanner sc){
        System.out.println(msg);
        return sc.nextInt();
    }
    /**
     * 给用户提示的同时,请用户输入一个字符串
     */
    public static String getInputString(String msg,Scanner sc){
        System.out.println(msg);
        return sc.next();
    }
}
表示层(controller)
public class UserController {
    static UserService userService = new UserServiceImpl();
    static User user;

    public static void main(String[] args) {
        //登录
        while (true){
            user = userService.login();
            if (null != user){
                System.out.println("登录成功!");
                break;
            }else {
                System.out.println("登录失败!");
            }
        }

        //进入系统
        PrintUtil.welcome();
        PrintUtil.showMenu(user.getLevel());
    }
}
PrintUtil(工具类)
public class PrintUtil {
    static Scanner sc = new Scanner(System.in);
    //static BookService bs = new BookServiceImpl();

    public static void welcome(){
        System.out.println("*********欢迎来到乐学优课图书管理系统*********");
        System.out.println("系统初始化成功");
        System.out.println("个性化显示菜单");
    }
    public static void exit(){
        System.out.println("*********谢谢使用乐学优课图书管理系统*********");
    }

    public static void showMenu(int level){
        //系统显示(管理员)
        if (level == 1){
            System.out.println("\n操作菜单内容:");
            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("0.退出系统");
        }else {
            //系统显示(普通用户)
            System.out.println("\n操作菜单内容:");
            System.out.println("1.显示所有图书");
            System.out.println("2.查询图书");
            System.out.println("3.借阅图书");
            System.out.println("4.下载电子图书");
            System.out.println("0.退出系统");
        }
    }
}

4.3、管理员登陆账&普通用户登录

显示所有图书(普通用户和管理员共有功能)
实体类(book)
public class Book{
    private int id;         //编号
    private String name;    //图书名称
    private String author;  //图书作者
    private double price;   //图书价格
    private String path;    //图书路劲
    private int count;      //图书的库存

    public Book() {
    }

    public Book(int id,  String author,String name, double price,int count) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.price = price;
        this.count = count;
    }

    public Book(int id,  String author,String name, double price, String path,int count) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.price = price;
        this.path = path;
        this.count = count;
    }


    public int getId() {
        return id;
    }

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

    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 getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", path='" + path + '\'' +
                ", count=" + count +
                '}';
    }

    public String toString(Book book){
        return
                "" + id +
                " " + author +
                " " + name +
                " " + price +
                " " + path +
                " " + count;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return id == book.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, author, price, path, count);
    }
}
BookUtil工具类
public class BookUtil {
    //定义List集合    全局List  我们针对于图书的  增删改查都会在这个list进行
    private static List<Book> list;

    static {
        List<Book> bookList = new ArrayList<>();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("day26\\book.txt"));
            String line;
            while ((line = br.readLine()) != null) {
                String[] split = line.split(" ");
                Book book = new Book(Integer.parseInt(split[0]), split[1], split[2], Double.valueOf(split[3]),split[4],Integer.parseInt(split[5]));
                bookList.add(book);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        list = bookList;
    }

    public static List<Book> getList(){
        return list;
    }
}
dao层(BookMapper接口)
 //查询
 List<Book> selectAll();
BookMapperImpl
public class BookMapperImpl implements BookMapper {

    private static List<Book> bookList;

    static {
        bookList = BookUtil.getList();
    }
    //查询
      @Override
    public List<Book> selectAll() {
        return bookList;
    }
}
service层(BookService)
public interface BookService {
    //查询图书
    List<Book> findAllBook();
}
BookServiceImpl
public class BookServiceImpl implements BookService {

    BookMapper bookMapper = new BookMapperImpl();
    
    @Override
    public List<Book> findAllBook() {
        return bookMapper.selectAll();
    }
}
表示层(controller)更新
public class UserController {
    static UserService userService = new UserServiceImpl();
    static User user;

    public static void main(String[] args) {
        //登录
        while (true){
            user = userService.login();
            if (null != user){
                System.out.println("登录成功!");
                break;
            }else {
                System.out.println("登录失败!");
            }
        }

        //进入系统
        PrintUtil.welcome();
        PrintUtil.showMenu(user.getLevel());
        PrintUtil.choice(user);
    }
}
PrintUtil(工具类)更新
public static void choice(User user) {
    while (true){
        int input = MyUtil.getInput("请输入你的选择", sc);
        switch (input) {
            case 1:
                //显示所有书籍
                print( bs.findAllBook());
                break;
            case 2:

            case 3:

            case 4:

            case 5:

            case 6:

            case 0:

            default:
                System.out.println("你输入的数字有误!");
        }
    }
}

//打印方法
public static void print(List<Book> list){
    System.out.println(" 图书作者 " + "  图书名称 " + " 图书价格  " + " 图书库存 ");
    //获取集合中的数据
    if (list != null && list.size()>0){
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i)!=null){
                System.out.println(" " + list.get(i).getAuthor() + "  " + list.get(i).getName() + "  " + list.get(i).getPrice() + " " +list.get(i).getCount());
            }else {
                System.out.println("    无   " + "    无    " + "   无    " + "   无   ");
            }
        }
    }
}

4.4、管理员(整理图书)—— 普通用户(查询图书)

dao层(BookMapper)更新
public interface BookMapper {
   
    //根据名称查询
    Book selectByName(String name);
}
BookMapperImpl 更新
public class BookMapperImpl implements BookMapper {

    private static List<Book> bookList;

    static {
        bookList = BookUtil.getList();
    }
 
    //查询
    @Override
    public List<Book> selectAll() {
        return bookList;
    }
    //根据名称查询
    @Override
    public Book selectByName(String name) {
        Book book = BookUtil.isExist(name);
        return book;
    }
}
BookUtil(工具类)更新

  //根据书名判断该图书是否存在
public static Book isExist(String name) {
    //创建Book对象
    Book book = new Book();       //此时的book对象是空的
    for (Book b : list) {
        if (b.getName().equals("《" + name + "》")) {
            //将系统中的图书对象给book对象
            book = b;
            return book;
        }
    }
    return null;
}

//整理书籍
public static void sort() {
    TreeSet set = new TreeSet((o1, o2) ->{
        //按照价格从小到大排序
        if (o1 instanceof Book && o2 instanceof Book){
            Book book1 = (Book)o1;
            Book book2 = (Book)o2;
            int price = Double.compare(book1.getPrice(), book2.getPrice());
            int name = book1.getName().compareTo(book2.getName());
            return price == 0 ? name : price;
        }else {
            throw new RuntimeException("输入类型不匹配");
        }
    });

    for (int i = 0; i < list.size(); i++) {
        set.add(list.get(i));
    }
    //调用排序之后的查询
    show(set);
}

//排序之后的查询
public static void show(Set set){
    System.out.println(" 图书名称 " + " 图书作者 " + " 图书价格  " + " 图书库存 ");
    Iterator<Book> iterator = set.iterator();
    while (iterator.hasNext()){
        Book book = iterator.next();
        System.out.println(book.getName() + "  " + book.getAuthor() + "  " + book.getPrice()+ " " + book.getCount());
    }
}
PrintUtil(工具类)更新
private static void case2(User user) {
    //整理书籍
    if (user.getLevel() == 1){
        BookUtil.sort();
    }else {
        //按照名字查询书籍
        String bookName = MyUtil.getInputString("请输入查询的书籍名称", sc);
        List<Book> list = new ArrayList();
        list.add(bs.findBookByName(bookName));
        print(list);
    }
}
service层(BookService)更新
public interface BookService {
  

    //按名字查询图书
    Book findBookByName(String name);
}
BookServiceImpl 更新
public class BookServiceImpl implements BookService {
    BookMapper bookMapper = new BookMapperImpl();

    @Override
    public List<Book> findAllBook() {
        return bookMapper.selectAll();
    }

    @Override
    public Book findBookByName(String name) {
        return bookMapper.selectByName(name);
    }
}

4.5、管理员(查询图书)—— 普通用户(借阅图书)

dao层(BookMapper)更新
public interface BookMapper {
    //修改
    int updateOne(Book book);
   
}
BookMapperImpl 更新
public class BookMapperImpl implements BookMapper {

    private static List<Book> bookList;

    static {
        bookList = BookUtil.getList();
    }

    //修改
    @Override
    public int updateOne(Book book) {
        //根据名称返回一个book对象
        Book tempBook = BookUtil.isExist(book.getName());
        if (tempBook != null){
            tempBook.setPrice(book.getPrice());
            tempBook.setCount(book.getCount());
            //修改成功  返回1
            return 1;
        }else {
            System.out.println("该书名不存在!");
            //修改失败  返回0
            return 0;
        }
    }

   
}
service层(BookService)更新
public interface BookService {
    //修改图书价格,数量
    int borrowBook(Book book);
    
}
BookServiceImpl 更新
public class BookServiceImpl implements BookService {

    BookMapper bookMapper = new BookMapperImpl();

    //修改
    @Override
    public int borrowBook(Book book) {
        return bookMapper.updateOne(book);
    }

   
}
PrintUtil(工具类)更新
public static void case3(User user){
    //按照名字查询书籍
    if (user.getLevel() == 1){
        String bookName = MyUtil.getInputString("请输入查询的书籍名称", sc);
        List<Book> list = new ArrayList();
        list.add(bs.findBookByName(bookName));
        print(list);
    }else {
        //借阅书籍
        String bookName = MyUtil.getInputString("请输入借阅的书籍名称", sc);
        int count = MyUtil.getInput("请输入借阅的书籍数量", sc);
        Book book = BookUtil.isExist(bookName);
        Book tempBook = new Book();
        if (null != book){
            tempBook.setName(bookName);
            tempBook.setCount(book.getCount()-count);
            tempBook.setPrice(book.getPrice());
            bs.borrowBook(tempBook);
            System.out.println("还剩余:" + (book.getCount() + "本"));
        }else {
            System.out.println("该书籍不存在!!!");
        }
    }
}

4.6、管理员(添加图书)—— 普通用户(下载电子书)

dao层(BookMapper)更新
public interface BookMapper {
  
    //新增
    int insertOne(Book book);
}
BookMapperImpl 更新
public class BookMapperImpl implements BookMapper {

    private static List<Book> bookList;

    static {
        bookList = BookUtil.getList();
    }

    //新增
    @Override
    public int insertOne(Book book) {
        boolean flag = bookList.add(book);
        if (flag == false){
            return 0;
        }
        return 1;
    }


   
}
service层(BookService)更新
public interface BookService {

    //新增图书
    int addBook(Book book);

  
}
BookServiceImpl 更新
public class BookServiceImpl implements BookService {

    BookMapper bookMapper = new BookMapperImpl();

    //新增
    @Override
    public int addBook(Book book) {
        return bookMapper.insertOne(book);
    }

    
}
BookUtil工具类 更新
 //获得最大编号
public static int getMaxId(List<Book> bookList){
    //mapToInt()返回一个IntStream,其中包括将给定函数应用于此流的元素的结果
    // OptionalInt帮助我们创建一个可能包含也可能不包含int值的对象。
    // getAsInt()方法返回值如果OptionalInt对象中存在一个值,否则抛出NoSuchElementException。
    return bookList.stream().mapToInt(Book::getId).max().getAsInt();
}
DownloadUtil(工具类)
//下载工具类
public class DownloadUtil {
    //下载图书
    public static void downloadIO(String path,String downloadPath){
        FileInputStream fis = null;     //输入
        FileOutputStream fos = null;  //输出
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //实现非文本文件的复制
            //1,造文件File
            File srcFile = new File(path);
            File descFile = new File(downloadPath);
            //2,造流
            //2.1 : 造节点流
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(descFile);
            //2.2 : 造缓存流(处理流)
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            //3,复制的细节  读取,写入
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }
            System.out.println("下载成功!!!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //资源关闭
            //关闭要求  先关闭内层的流   在关闭外层的流
            //例如:穿衣服  先穿内衣  在穿外衣    脱衣服  先脱外衣   在脱内衣
            if (bos !=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis !=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //说明:关闭外层流同时,内层流也会自动关闭,关于内层流的关闭,我们可以省略
           /* fos.close();
            fis.close();*/
        }
    }

    //创建一本图书
    public static void uploadIO(String path){
        File file = new File(path);
        try {
            //创建文件
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
DownloadBook(线程类)
//线程类
public class DownloadBook implements Runnable {

    private String path;  //路劲
    private String downloadPath = "D:\\java\\one\\javaSE\\Demo\\day26\\library\\";   //下载目录
    private int count;

    public DownloadBook() {
    }

    public DownloadBook(String path,int count) {
        this.path = path;
        this.count = count;
    }

    @Override
    public void run() {
        while (true){
            if (count > 0){
                show();
            }else {
                break;
            }
        }
    }

    private synchronized void show(){
        if (count > 0){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String[] split = path.split("\\\\");
            downloadPath += split[split.length-1];
            DownloadUtil.downloadIO(path,downloadPath);
            count --;
        }
    }

}
PrintUtil类 更新

    public static void case4(User user){
        //添加书籍
        if (user.getLevel() == 1){
            String book_name = MyUtil.getInputString("请输入你要添加的图书名称", sc);
            //调用方法
            Book book = BookUtil.isExist(book_name);
            //新书添加方法
            if (book == null) {
                //系统中没有该图书
                String book_author = MyUtil.getInputString("请输入图书的作者", sc);
                String book_price = MyUtil.getInputString("请输入图书的价格", sc);
                int book_count = MyUtil.getInput("请输入图书的数量", sc);

                Book tempBook = new Book();
                tempBook.setName("《"+ book_name + "》");
                tempBook.setAuthor(book_author);
                tempBook.setPrice(Double.valueOf(book_price));
                tempBook.setCount(book_count);
                tempBook.setId(BookUtil.getMaxId(BookUtil.getList())+1);
                String path = "C:\\Users\\EDZ\\Desktop\\图书管理系统\\图书馆\\"+book_name+".txt";
                tempBook.setPath(path);
                DownloadUtil.uploadIO(path);
                bs.addBook(tempBook);
                System.out.println("新书添加成功");

            }else {
                           /* int count = MyUtil.getInput("已存在该书,请输入新增图书的数量", sc);
                            book.setCount(book.getCount()+count);
                            String subName = book.getName().substring(1, book.getName().length() - 1);
                            book.setName(subName);
                            bs.borrowBook(book);
                            System.out.println("当前此书籍数量:" + (book.getCount() + "本"));
                            System.out.println("添加成功");*/
                System.out.println("该图书已存在!!!!");
            }
        }else {
            //下载电子书
            downloadBook();
        }
    }
    
    
    //下载电子图书(使用多线程完成)
    public static void downloadBook() {
        while (true) {
            String name = MyUtil.getInputString("请输入你要下载的图书名称,可以下载一个图书或多个图书,图书之间《英文逗号》隔开", sc);
            String[] split = new String[0];
            int count = 0;
            if (name.length()>0){
                split = name.split(",");
                count++;
            }

            //如果输入一本书,重新把name赋值给split数组
            if (split.length == 0){
                split[0] = name;
            }
            //如果输入多本书,循环下载
            for (String bookName : split) {
                Book book = BookUtil.isExist(bookName);
                if (bookName != null && book.getCount() > 0) {

                    DownloadBook task1 = new DownloadBook(book.getPath(),count);
                    Thread thread = new Thread(task1);
                    thread.start();
                } else {
                    System.out.println("该书名不存在");
                }
            }
            break;
        }
    }

4.7、管理员(修改图书)

PrintUtil类 更新
public static void case5(){
    //修改图书
    String book_name = MyUtil.getInputString("请输入你要修改的图书名称", sc);
    String book_price = MyUtil.getInputString("请输入你要修改的图书的价格", sc);
    int book_count = MyUtil.getInput("请输入你要修改的图书的数量", sc);
    Book tempBook = new Book();
    tempBook.setName(book_name);
    tempBook.setPrice(Double.valueOf(book_price));
    tempBook.setCount(book_count);
    bs.borrowBook(tempBook);
    System.out.println("修改成功");
}

4.8、管理员(下架图书)

dao层(BookMapper)更新
public interface BookMapper {
  
    //删除
   
    int deleteByName(String name);
}
BookMapperImpl 更新
public class BookMapperImpl implements BookMapper {

    private static List<Book> bookList;

    static {
        bookList = BookUtil.getList();
    }

   

    //删除
    @Override
    public int deleteByName(String name) {
        //根据名称返回一个book对象
        Book tempBook = BookUtil.isExist(name);
        if (tempBook != null) {
            Iterator<Book> iterator = bookList.iterator();
            while (iterator.hasNext()){
                Book next = iterator.next();
                if(next.getName().equals("《" + name + "》")){
                    iterator.remove();
                    return 1;
                }
            }
        }else {
            System.out.println("该图书不存在!");
        }
        //删除失败,返回0
        return 0;
    }

    
}
service层(BookService)更新
public interface BookService {

   

    //下架图书
    int removeBook(String name);
}
BookServiceImpl 更新
public class BookServiceImpl implements BookService {

    BookMapper bookMapper = new BookMapperImpl();

   

    //下架
    @Override
    public int removeBook(String name) {
        return bookMapper.deleteByName(name);
    }
}
PrintUtil类 更新
public static void case6(){
    //下架图书
    String name = MyUtil.getInputString("请输入你要下架的图书名称", sc);
    bs.removeBook(name);
    System.out.println("下架成功!!!");
}

4.9、0.退出系统(管理员和普通用户共有功能)

BookUtil工具类 更新
//io流最终写入到文本文件
    public static void mapIO(){
        Map<Integer,Book> map = new HashMap<>();

        TreeSet set = new TreeSet((o1, o2) ->{
            //按照价格从小到大排序
            if (o1 instanceof Book && o2 instanceof Book){
                Book book1 = (Book)o1;
                Book book2 = (Book)o2;
                return Integer.compare(book1.getId(),book2.getId());
            }else {
                throw new RuntimeException("输入类型不匹配");
            }
        });

        List<Book> bookList = getList();

        for (int i = 0; i < bookList.size(); i++) {
            set.add(bookList.get(i));
        }


        Iterator<Book> iterator = set.iterator();
        while (iterator.hasNext()){
            Book book = iterator.next();
            map.put(book.getId(),book);
        }

        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("book.txt"));
            for (Map.Entry<Integer, Book> entry : map.entrySet()) {
                Book value = entry.getValue();
                bw.write(value.toString(value));
                bw.newLine();  //提供换行操作
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
PrintUtil 更新
 public static void choice(User user){
        while (true){
            int input = MyUtil.getInput("请输入你的选择", sc);
            switch (input) {
                case 1:
                    //显示所有书籍
                    print( bs.findAllBook());
                    break;
                case 2:
                    case2(user);
                    break;
                case 3:
                    case3(user);
                    break;
                case 4:
                    case4(user);
                    break;
                case 5:
                    case5();
                    break;
                case 6:
                    case6();
                    break;
                case 0:
                    BookUtil.mapIO();
                    exit();
                    System.exit(0);
                default:
                    System.out.println("你输入的数字有误!");
            }
        }
    }

5、项目总结

图书管理系统,涉及到了面向对象的核心思想,面向接口编程,归纳了javaSE的技术栈,体现了程序的高内聚,低耦合。运用到了三层架构,为后期框架阶段做了很好的铺垫。

  • 19
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值