Java数组-图书管理系统

该程序创建了一个图书类(Book)用于存储图书信息,包括ID、名称、价格、作者和库存。在功能类(BookTest)中,实现了查看图书、新增图书、修改图书和删除图书的功能,提供了一个简单的命令行交互式图书管理系统。
摘要由CSDN通过智能技术生成

项目要求:

  1. 存储图书相关信息
  2. 初始化两本书
  3. 输出欢迎以及对图书的操作
  4. 查看图书
  5. 新增图书
  6. 修改图书
  7. 删除图书
  8. 退出本系统

图书类(Book)

package com.xszx.ts;

/**
 * @Author  杨 
 * @CreateDate 2023-03-19 9:52
 **/
public class Book {
    private int id;
    private String name;
    private double price;
    private String author;
    private int snum;

    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 double getPrice() {
        return price;
    }

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

    public String getAuthor() {
        return author;
    }

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

    public int getSnum() {
        return snum;
    }

    public void setSnum(int snum) {
        this.snum = snum;
    }

    public Book() {
    }

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

功能类(BookTest)

package com.xszx.ts;
import java.util.Scanner;

/**
 * @Author 杨 
 * @CreateDate 2023-03-19 9:56
 **/
public class BookTest {
    static Scanner sc = new Scanner(System.in);
    static  Book[] books= new Book[6];
    public static void main(String[] args) {
        books[0]= new Book(1,"呐喊",30,"鲁迅",8);
        books[1]= new Book(2,"野草",20.5,"鲁迅",5);

        while (true){
            System.out.println("欢迎进入图书管理系统!");
            System.out.println("【1】查看图书【2】新增图书【3】修改图书【4】删除图书【5】退出系统");
            System.out.println("请选择要进行的操作:");
            int m = sc.nextInt();
            if (m == 1) {
                selBook();
            }else if (m == 2) {
                addBook();
            }else if (m == 3) {
                updateBook();
            }else if (m == 4) {
                delBook();
            }else if (m == 5) {
                System.out.println("本次服务到此结束!");
                break;
            }
        }

    }

    //查看图书
    private static void selBook() {
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                System.out.println("第"+(i+1)+"本书编号:"+books[i].getId()+"\t"+
                        "书名:"+books[i].getName()+"\t"+
                        "价格:"+books[i].getPrice()+"\t"+
                        "作者:"+books[i].getAuthor()+"\t"+
                        "数量:"+books[i].getSnum());
            }

        }
    }

    //增加图书
    private static void addBook() {
        System.out.println("请输入要添加的位置:");
        int n= sc.nextInt();
        System.out.println("请输入要添加的图书编号:");
        int id =sc.nextInt();
        System.out.println("请输入要添加的图书名称:");
        String name = sc.next();
        System.out.println("请输入要添加的图书价格:");
        double price = sc.nextDouble();
        System.out.println("请输入要添加的图书作者:");
        String author = sc.next();
        System.out.println("请输入要添加的图书数量:");
        int snu= sc.nextInt();
        books[n-1]= new Book(id,name,price,author,snu);
    }

    //修改图书
    private static void updateBook() {
        System.out.println("请输入要修改的图书位置:");
        int n= sc.nextInt();
        System.out.println("本书编号:"+books[n-1].getId()+"\t"+
                "书名:"+books[n-1].getName()+"\t"+
                "价格:"+books[n-1].getPrice()+"\t"+
                "作者:"+books[n-1].getAuthor()+"\t"+
                "数量:"+books[n-1].getSnum());
        System.out.println("请输入要添加的图书编号:");
        int id =sc.nextInt();
        System.out.println("请输入要添加的图书名称:");
        String name = sc.next();
        System.out.println("请输入要添加的图书价格:");
        double price = sc.nextDouble();
        System.out.println("请输入要添加的图书作者:");
        String author = sc.next();
        System.out.println("请输入要添加的图书数量:");
        int snu= sc.nextInt();
        books[n-1]= new Book(id,name,price,author,snu);
    }

    //删除图书
    private static void delBook() {
        System.out.println("请输入要删除的图书名称:");
        String name = sc.next();
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null && books[i].getName().equals(name)) {
                books[i] = new Book();
            }
        }
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值