项目要求:
- 存储图书相关信息
- 初始化两本书
- 输出欢迎以及对图书的操作
- 查看图书
- 新增图书
- 修改图书
- 删除图书
- 退出本系统
图书类(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();
}
}
}
}