需求:
利用面向对象的思想设计一个图书管理系统。图书的属性有:编号,书名,作者,价格。要求提供如下功能:
1、提供操作菜单,可以选择要进行的操作。
2、可以添加图书,添加图书时,编号需要唯一,添加成功,返回到菜单。
3、可以查询图书,显示所有图书信息,然后返回到菜单。
4、可以根据书名,查询单本图书信息,显示信息后,返回到菜单。
5、可以删除图书,通过编号删除,删除成功后,返回到菜单。
6、可以修改图书的信息,但编号不可以修改,修改成功后,返回到菜单。
7、可以退出系统,结束程序运行。
运行展示
书类实现:
//利用面向对象的思想设计一个图书管理系统。
// 图书的属性有:编号,书名,作者,价格。要求提供如下功能:
public class Books {
private int serNum;
private String title;
private String author;
private double price;
public Books() {
}
public Books(int serNum, String title, String author, double price) {
this.serNum = serNum;
this.title = title;
this.author = author;
this.price = price;
}
public int getSerNum() {
return serNum;
}
public void setSerNum(int serNum) {
this.serNum = serNum;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void show(){
System.out.println("编号:"+serNum+" 书名:《" + title + "》 作者:" + author + " 价格:" + price);
}
}
测试类实现:
import java.util.ArrayList;
import java.util.Scanner;
/*
* 1、提供操作菜单,可以选择要进行的操作。
2、可以添加图书,添加图书时,编号需要唯一,添加成功,返回到菜单。
3、可以查询图书,显示所有图书信息,然后返回到菜单。
4、可以根据书名,查询单本图书信息,显示信息后,返回到菜单。
5、可以删除图书,通过编号删除,删除成功后,返回到菜单。
6、可以修改图书的信息,但编号不可以修改,修改成功后,返回到菜单。
7、可以退出系统,结束程序运行。
*
*
* */
public class DemoBooks {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Books> list = new ArrayList<>();
while (true) {
System.out.println("===========图书馆里系统============");
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("请输入序号:(1-6)");
int choice = sc.nextInt();
boolean aNull = isNull(list);
switch (choice) {
case 1:
// System.out.println("添加图书");
addBoo(sc,list);
break;
case 2:
// System.out.println("查询所有图书");
if(aNull){
lookBooks(list);
}else {
System.out.println("请先添加图书!");
}
break;
case 3:
// System.out.println("查询单本图书");
if(aNull){
lookDanBook(list,sc);
}else {
System.out.println("请先添加图书!");
}
break;
case 4:
// System.out.println("删除图书");
if(aNull){
reBook(list,sc);
}else {
System.out.println("请先添加图书!");
}
break;
case 5:
// System.out.println("修改图书");
if(aNull){
changBook(list,sc);
}else {
System.out.println("请先添加图书!");
}
break;
case 6:
System.out.println("欢迎使用");
System.exit(0);
default:
System.out.println("输入有误,请输入(1-6)");
break;
}
}
}
// 判断集合是否 有值
public static boolean isNull(ArrayList<Books> list){
if(list.size() == 0 || list == null){
// 没有值
return false;
}
return true;
}
// 判断是否存在
public static int isHave(ArrayList<Books> list,int id){
for (int i = 0; i < list.size(); i++) {
// 如果存在
if (id == list.get(i).getSerNum()){
return i;
}
}
// 不存在返回 -1
return -1;
}
// 添加图书
public static void addBoo(Scanner sc, ArrayList<Books> list) {
int id = 0;
while (true) {
System.out.println("请输入图书编号:");
id = sc.nextInt();
int index = isHave(list, id);
if(index == -1){
break;
}else {
System.out.println("该图书编号已经存在,请重新输入!");
}
}
System.out.println("请输入图书的书名:");
String name = sc.next();
System.out.println("请输入图书的作者:");
String author = sc.next();
System.out.println("请输入图书的价格:");
double price = sc.nextDouble();
Books b1 = new Books(id,name,author,price);
list.add(b1);
System.out.println("添加成功!");
}
// 查看所有图书
public static void lookBooks(ArrayList<Books> list){
// System.out.println("================================");
// System.out.println(" 编号 书名 作者 价格");
for (int i = 0; i < list.size(); i++) {
list.get(i).show();
}
}
// 查看单个图书 根据编号查找
public static void lookDanBook(ArrayList<Books> list,Scanner sc){
System.out.println("请输入要查看的图书编号:");
int lookBookId = sc.nextInt();
// 查找是否存在 存在 返回索引 不存在 -1
int index = isHave(list, lookBookId);
if(index != -1){
// System.out.println(" 编号 书名 作者 价格");
list.get(index).show();
}else {
System.out.println("您要查找的图书不存在!");
}
}
// 删除图书
public static void reBook(ArrayList<Books> list,Scanner sc){
System.out.println("请输入要删除的图书编号:");
int reId = sc.nextInt();
// 进行判断是否存在 存在 返回但前值 不存在 -1
int index = isHave(list, reId);
if(index != -1){
Books b1 = list.get(index);
list.remove(b1);
System.out.println("删除成功!!");
}else {
System.out.println("您要删除的图书不存在!");
}
}
// 修改图书
public static void changBook(ArrayList<Books> list, Scanner sc){
System.out.println("请输入要修改的图书编号:");
int changeId = sc.nextInt();
// 进行判断是否存在 存在 返回但前值 不存在 -1
int index = isHave(list, changeId);
if(index != -1){
System.out.println("请输入该图书新的图书名:");
String newName = sc.next();
System.out.println("请输入改图书新的作者:");
String newAuthor = sc.next();
System.out.println("请输入该图书新的价格:");
double newPrice = sc.nextDouble();
Books b1 = new Books(changeId,newName,newAuthor,newPrice);
list.set(index,b1);
System.out.println("修改成功!");
}else {
System.out.println("您输入的图书不存在!!");
}
}
}