java实现图书馆管理系统2.0
程序员文章站
2024-03-22 21:52:28
...
Book类
/**
*
*/
package com.gem.demo.book;
/**
*
* Description:
*
* @author HadwinLing
*
* @date 2020年1月16日上午11:49:23
*
* @version 0.0.1
*
*/
public class Book {
private String bno;
private String bname;
private String author;
private double price;
private String mark;
//构造方法
public Book() {
}
/**
* @param bno
* @param bname
* @param author
* @param price
* @param mark
*/
public Book(String bno, String bname, String author, double price, String mark) {
this.bno = bno;
this.bname = bname;
this.author = author;
this.price = price;
this.mark = mark;
}
/**
* @return the bno
*/
public String getBno() {
return bno;
}
/**
* @param bno the bno to set
*/
public void setBno(String bno) {
this.bno = bno;
}
/**
* @return the bname
*/
public String getBname() {
return bname;
}
/**
* @param bname the bname to set
*/
public void setBname(String bname) {
this.bname = bname;
}
/**
* @return the author
*/
public String getAuthor() {
return author;
}
/**
* @param author the author to set
*/
public void setAuthor(String author) {
this.author = author;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @return the mark
*/
public String getMark() {
return mark;
}
/**
* @param mark the mark to set
*/
public void setMark(String mark) {
this.mark = mark;
}
//sout();调用
public String toString() {
return this.bno+"\t"+this.bname+"\t"+this.author+"\t"+this.price+"\t"+this.mark;
}
}
Menu
/**
*
*/
package com.gem.demo.book;
import java.util.Scanner;
/**
*
* Description:
*
* @author HadwinLing
*
* @date 2020年1月16日上午11:48:54
*
* @version 0.0.1
*
*/
public class Menu {
private Book[] books;//null
private int size;//真实书籍的个数
private Scanner input=new Scanner(System.in);
public Menu() {
books=new Book[5];//容器大小
Book b1=new Book("b001", "红楼梦", "曹雪芹", 30, "四大名著");
Book b2=new Book("b002", "三国演义", "罗贯中", 30, "四大名著");
Book b3=new Book("b003", "水浒传", "施耐庵", 30, "四大名著");
Book b4=new Book("b004", "三体", "刘慈欣", 30, "名著");
Book b5=new Book("b005", "西游记", "吴承恩", 30, "名著");
books[0]=b1;
books[1]=b2;
books[2]=b3;
books[3]=b4;
books[4]=b5;
size=5;
}
public void printMenu() {
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("请选择...");
}
public void printTitle() {
System.out.println("书籍编号\t书籍名称\t作者\t价格\t简介");
}
/**
* 浏览功能
* 遍历数组 ,打印所有书籍信息
*/
public void findAll() {
printTitle();
for(int i=0;i<size;i++) {
System.out.println(books[i]);
}
}
/**
* 查找书籍
*/
public void findBook() {
System.out.println("请输入编号:");
String bno=input.next();
//根据输入的bno进行查询
Book book=getBook(bno);
if(book!=null) {
printTitle();
System.out.println(book);
}else {
System.out.println("改编号书籍不存在!");
}
}
/**
* 判断是否找到书籍
*/
private Book getBook(String bno) {
for(int i=0;i<size;i++) {
if(bno.equals(books[i].getBno())) {
//找到了
return books[i];//方法结束
}
}
return null;//没有找到书籍
}
/**
* 增加书籍
*/
public void addBook() {
System.out.println("请输入编号");
String bno=input.next();
System.out.println("请输入书名");
String bname=input.next();
System.out.println("请输入作者");
String author=input.next();
System.out.println("请输入价格");
double price=input.nextDouble();
System.out.println("请输入简介");
String mark=input.next();
//新书籍对象创建
Book book=new Book(bno, bname, author, price, mark);
//扩容
extend();
//放在数组容器中
books[size++]=book;
System.out.println("上架成功");
}
private void extend() {
if(size>=books.length) {
//需要扩容
Book[] newBooks=new Book[books.length*2];//新的书籍数组 null
for(int i=0;i<books.length;i++) {
newBooks[i]=books[i];
}
books=newBooks;
}
}
/**
* 更新书籍
*/
public void updateBook() {
System.out.println("请输入书籍编号");
String bno=input.next();
Book book=getBook(bno);
if(book!=null) {
//可以编辑
System.out.println("请输入书名");
String bname=input.next();
System.out.println("请输入作者");
String author=input.next();
System.out.println("请输入价格");
double price=input.nextDouble();
System.out.println("请输入简介");
String mark=input.next();
//更新
book.setAuthor(author);
book.setBname(bname);
book.setMark(mark);
book.setPrice(price);
System.out.println("更新成功");
}else {
System.out.println("改编号书籍不存在,不能编辑!");
}
}
/**
*删除书籍
*/
public void deleteBook() {
System.out.println("请要删除的书籍编号:");
String bno=input.next();
for(int i=0;i<size;i++) {
if(bno.equals(books[i].getBno())) {
//要删除的书籍找到了
//i位置开始,j==i,books[j]=books[j+1];
for(int j=i;j<size-1;j++) {
books[j]=books[j+1];
}
//size=3
//book[size-2]==books[size-1] books[size]
books[--size]=null;
System.out.println("删除成功!");
return;//结束方法
}
}
System.out.println("输入编号有误,删除失败");
}
}
Test测试类
/**
*
*/
package com.gem.demo.book;
import java.util.Scanner;
/**
*
* Description:
*
* @author HadwinLing
*
* @date 2020年1月16日上午11:50:07
*
* @version 0.0.1
*
*/
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Menu menu=new Menu();
//接收键盘输入数据
Scanner input=new Scanner(System.in);
boolean flag=true;
while(flag) {
//打印主菜单
menu.printMenu();
int op=input.nextInt();
switch (op) {//key 数据类型 String char int short byte 枚举类
case 1:
menu.findAll();
break;
case 2:
menu.findBook();
break;
case 3:
menu.addBook();
break;
case 4:
menu.updateBook();
break;
case 5:
menu.deleteBook();
break;
case 6:
System.out.println("确认退出?Y/N");
String result=input.next();
//equalsIgnoreCase 忽略大小写 比较值
if("y".equalsIgnoreCase(result)) {
flag=false;
}
break;
default:
System.out.println("输入有误,重新选择");
break;
}
}
System.out.println("*******谢谢使用********");
}
}
数组扩容
/**
* 数组扩容
*/
private void extend() {
if(size>=books.length) {
//需要扩容
Book[] newBooks=new Book[books.length*2];//新的书籍数组 null
for(int i=0;i<books.length;i++) {
newBooks[i]=books[i];
}
books=newBooks;
}
}
上一篇: ElasticSearch集群环境搭建
下一篇: MacBook连接小米等安卓手机