关于数组的图书管理系统
1:代码实现
1:先创建一个Book类
1)里面封装了Book类的属性,代码为:
package com.atguigu.bms.domain;
/** Book
String name
int price
String author//作家
int stock //库存
int sales //销售
*javabean封装对象的属性
* */
public class Book {
private String name;
private int price;
private String author;
private int stock;
private int sales;
public Book(String name, int price, String author, int stock, int sales) {
this.name = name;
this.price = price;
this.author = author;
this.stock = stock;
this.sales = sales;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public int getSales() {
return sales;
}
public void setSales(int sales) {
this.sales = sales;
}
public String say(){
return " \t"+name+"\t \t \t"+price+" \t\t \t"+author+"\t \t \t"+stock+" \t\t \t"+sales;
}
}2:再创建一个BookService类:实现数组的功能实现.
package com.atguigu.bms.service;
import com.atguigu.bms.domain.Book;
/** BookService
boolean addBook(Book book);
Book[] getAllBooks();
Book getBook(int index);
boolean removeBook(int index)
* */
public class BookService {
private Book[] books;//声明一个Book对象数组
private int realCount;//声明一个计数器.作用是用来作为下标存入数据
//需要声明一个构造器,不直接写入多少个数组,而是用一个形参totalCount来存入
public BookService(int totalCount){
this.books= new Book[totalCount];//把totalCount这个下标的Book对象写入数组
}
//增加书的数量***************************************************************************
public boolean addBook(Book book){
if(realCount>=books.length){
System.out.println("不可继续添加,书库已满");
return false;
}
books[realCount]=book;//将book书类添加到realCount位置的数组
realCount++;
return true;
}
//查找所有书对象的信息***************************************************************************
public Book[] getAllBooks() {
//创建新数组,来放入加入的数据
Book [] allBook=new Book[realCount];
for(int i=0;i<realCount;i++){
allBook[i]=books[i];
}
return allBook;
}
//获得当前输入索引数组对象的信息***************************************************************************
public Book getBook(int index){
if(index>realCount||index<0){
System.out.println("输入的编号错误,");
return null;
}
return books[index-1];
}
//删除当前输入书索引数组对象***************************************************************************
public boolean removeBook(int index){
if(index>realCount||index<0){
System.out.println("输入的索引编号错误");
return false;
}
books[index]=null;
for( int i=index;index<realCount-1;index++){
books[i]=books[i+1];
}
books[realCount-1]=null;
realCount-=1;
return true;
}
}3:创建一个BookView类来实现与BookService类的交互:
package com.atguigu.bms.view;
import com.atguigu.bms.domain.Book;
import com.atguigu.bms.service.BookService;
public class BookView {
private BookService bookService=new BookService(10);
public void enterMainMenu(){
Boolean flag=true;//做一个循环,如果为flase则跳出循环
int realCount;
while(flag){
/***
* boolean addBook(Book book);
* Book[] getAllBooks();
* Book getBook(int index);
* boolean removeBook(int index)
*/
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.print("请输入1-5进行操作");
char num=EMSUtility.readChar();
switch (num){
case '1':{
addnewBook();
break;
}case '2':{
arrayList();
break;
}case '3':{
arrayOneList();
break;
}case '4':{
removeBook();
break;
}case '5':{
System.out.println("请选择是否退出Y/N");
char choose=EMSUtility.readChar();
if(choose=='Y'){
flag=false;
}
break;
}
}
}
}
private void addnewBook(){
System.out.println("----------------添加员工信息---------------");
/** this.name = name;
this.price = price;
this.author = author;
this.stock = stock;
this.sales = sales;*/
System.out.println("请输入添加的员工姓名:");
String name=EMSUtility.readString(20);
System.out.println("请输入添加的价钱:");
int price=EMSUtility.readInt();
System.out.println("请输入作者名称:");
String author =EMSUtility.readString(50);
System.out.println("请输入添加的数量");
int stock=EMSUtility.readInt();
System.out.println("请输入卖出的数量");
int sales=EMSUtility.readInt();
Book book=new Book(name,price,author,stock,sales);
boolean flag= bookService.addBook(book);
if(flag==true){
System.out.println("-------------添加完成-----------");
arrayList();
}else{
System.out.println("-------------添加失败-----------");
}
}
public void arrayList(){
System.out.println("----------列出所有书籍信息----------");
System.out.println("书的名字\t书的价格\t书的作者\t书的库存\t书的销量");
Book[] massage =bookService.getAllBooks();
for(int i=0;i<massage.length;i++){
System.out.println(massage[i].say());
}
}
public void arrayOneList(){
System.out.println("--------列出查询的书籍信息---------");
int index=EMSUtility.readInt();
if(index==-1){
return;
}else
bookService.getBook(index);
System.out.println( bookService.getBook(index).say());
}
public void removeBook(){
System.out.println("--------删除书籍的书籍信息---------");
int r=EMSUtility.readInt();
boolean bloo1=bookService.removeBook(r-1);
if(bloo1==true){
System.out.println("删除成功");
arrayList();
}else
System.out.println("删除失败");
}
}---------------------------------------------------------可以从命令行获取参数的类:
package com.atguigu.bms.view;
import java.util.*;
public class EMSUtility {
private static Scanner scanner = new Scanner(System.in);
public static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4' && c != '5') {
System.out.print("选择错误,请重新输入:");
} else break;
}
return c;
}
public static char readChar() {
String str = readKeyBoard(1, false);
return str.charAt(0);
}
public static char readChar(char defaultValue) {
String str = readKeyBoard(1, true);
return (str.length() == 0) ? defaultValue : str.charAt(0);
}
public static int readInt() {
int n;
for (; ; ) {
String str = readKeyBoard(2, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static int readInt(int defaultValue) {
int n;
for (; ; ) {
String str = readKeyBoard(2, true);
if (str.equals("")) {
return defaultValue;
}
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static String readString(int limit) {
return readKeyBoard(limit, false);
}
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}
public static char readConfirmSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
private static String readKeyBoard(int limit, boolean blankReturn) {
String line = "";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.length() == 0) {
if (blankReturn) return line;
else continue;
}
if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}4:定义一个main方法类,来作为入口,调用视图类.
package com.atguigu.bms.main;
import com.atguigu.bms.domain.Book;
import com.atguigu.bms.view.BookView;
public class BookMain {
public static void main(String[] args) {
BookView bookView=new BookView();
bookView.enterMainMenu();
}}
2:功能演示
1):主菜单和插入功能
2)查询所有书信息
3)查询输入的编号书的信息
4)删除输入的编号的书
5)退出
今天不学习,明天变垃圾。






浙公网安备 33010602011771号