21xrx.com
2024-11-05 20:36:26 Tuesday
登录
文章检索 我的文章 写文章
Java实现图书删除操作:完整代码示例
2023-06-15 17:58:05 深夜i     --     --
Java 删除 图书管理

图书管理系统中,删除图书是一项必备功能。本文将给大家介绍如何使用Java编写一个简单的删除图书的代码,并附上完整的代码示例。

代码实现

首先需要创建一个图书类Book,包含以下属性:


private int id; // 图书ID

private String title; // 图书名称

private String author; // 作者

private String publisher; // 出版社

private double price; // 价格

接着,需要创建一个图书管理类BookManager,包含以下方法:


public void addBook(Book book); // 添加图书

public void removeBook(int id); // 删除图书

public void updateBook(int id, Book book); // 更新图书信息

public Book findBook(int id); // 查找图书信息

其中,removeBook方法是删除图书的核心方法,其实现如下:


public void removeBook(int id) {

  Iterator iterator = bookList.iterator();

  while (iterator.hasNext()) {

    Book book = iterator.next();

    if (book.getId() == id) {

      iterator.remove();

    }

  }

}

首先获取图书列表的迭代器,遍历图书列表,当找到与传入id相同的图书时,使用迭代器的remove方法将该图书从列表中删除。

完整代码如下:


import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class Book {

  private int id; // 图书ID

  private String title; // 图书名称

  private String author; // 作者

  private String publisher; // 出版社

  private double price; // 价格

  public Book(int id, String title, String author, String publisher, double price)

    this.id = id;

    this.title = title;

    this.author = author;

    this.publisher = publisher;

    this.price = price;

  

  public int getId()

    return id;

  

  public void setId(int id)

    this.id = id;

  

  public String getTitle()

    return title;

  

  public void setTitle(String title)

    this.title = title;

  

  public String getAuthor()

    return author;

  

  public void setAuthor(String author)

    this.author = author;

  

  public String getPublisher()

    return publisher;

  

  public void setPublisher(String publisher)

    this.publisher = publisher;

  

  public double getPrice()

    return price;

  

  public void setPrice(double price)

    this.price = price;

  

  @Override

  public String toString() {

    return "Book{" +

        "id=" + id +

        ", title='" + title + '\'' +

        ", author='" + author + '\'' +

        ", publisher='" + publisher + '\'' +

        ", price=" + price +

        '}';

  }

}

class BookManager {

  private List bookList;

  public BookManager() {

    bookList = new ArrayList<>();

  }

  // 添加图书

  public void addBook(Book book) {

    bookList.add(book);

  }

  // 删除图书

  public void removeBook(int id) {

    Iterator iterator = bookList.iterator();

    while (iterator.hasNext()) {

      Book book = iterator.next();

      if (book.getId() == id) {

        iterator.remove();

      }

    }

  }

  // 更新图书

  public void updateBook(int id, Book book) {

    for(Book b : bookList) {

      if(b.getId() == id) {

        b.setTitle(book.getTitle());

        b.setAuthor(book.getAuthor());

        b.setPublisher(book.getPublisher());

        b.setPrice(book.getPrice());

      }

    }

  }

  // 查找图书

  public Book findBook(int id) {

    for(Book book : bookList) {

      if(book.getId() == id)

        return book;

      

    }

    return null;

  }

  public List getBookList()

    return bookList;

  

}

public class Main {

  public static void main(String[] args) {

    Book book1 = new Book(1, "Java程序设计", "张三", "清华大学出版社", 78.5);

    Book book2 = new Book(2, "Python入门到实战", "李四", "人民邮电出版社", 68.8);

    Book book3 = new Book(3, "C++编程思想", "王五", "机械工业出版社", 98.0);

    BookManager manager = new BookManager();

    manager.addBook(book1);

    manager.addBook(book2);

    manager.addBook(book3);

    // 删除图书

    manager.removeBook(2);

    // 查找图书

    Book book = manager.findBook(3);

    if(book != null) {

      System.out.println(book);

    }

    // 更新图书信息

    Book book4 = new Book(4, "Java编程思想", "Bruce Eckel", "机械工业出版社", 118.0);

    manager.updateBook(1, book4);

    // 打印所有图书信息

    for(Book b : manager.getBookList()) {

      System.out.println(b);

    }

  }

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复