Book类:图书的基本信息:id,书名 …
public class Book {
private String bookid;
private String bookname;
private String isbn;
private double price;
private int count;
public Book() {
}
public Book(String bookid, String bookname, String isbn, double price, int count) {
this.bookid = bookid;
this.bookname = bookname;
this.isbn = isbn;
this.price = price;
this.count = count;
}
public Book(String bookid, String bookname, int count) {
this.bookid = bookid;
this.bookname = bookname;
this.count = count;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
//测试
// public static void main(String[] args) {
// new BookView();
// }
public String getBookid() {
return bookid;
}
public void setBookid(String bookid) {
this.bookid = bookid;
}
}
BookBiz类(实现增删改查的基本功能):
/*
* 基本功能
* 增加图书,出库,入库,查询
* */
public class BookBiz {
//获取仓库中Datas.bookStore总数
public int getBookCount(){
int count = 0;
for (Book book : Datas.BookStore) {
if(book == null)
break;
count++;
}
return count;
}
//重载getBookCount
public int getBookCount(Book ...books){
int count = 0;
for (Book book : Datas.BookStore) {
if(book == null)
break;
count++;
}
return count;
}
//添加图书
public boolean addBook(Book book){
int bookCount = getBookCount();
if(Datas.BookStore.length == bookCount){
return false;
}
//如果仓库不满,将图书对象放在数组最后一个位置
Datas.BookStore[bookCount] = book;
return true;
}
//删除图书
public boolean delBook(Book book){
int bookCount = getBookCount();
int delIndex = -1;//要删除元素的下标
for (int i = 0; i < bookCount; i++) {
if(book.getBookid().equals(Datas.BookStore[i].getBookid())){
delIndex = i;
System.out.println("111");
break;
}
}
if(delIndex == -1){
return false;//没有找到要删除的元素时,直接结束方法,返回false
}
//执行删除图书的套路
for (int i = delIndex; i < bookCount -1 ; i++) {
Datas.BookStore[i] = Datas.BookStore[i + 1];
}
//将最后一个元素设置为null
Datas.BookStore[bookCount - 1] = null;
return true;
}
//通过图书id查找图书
public Book findByid(String bookid){
int bookCount = getBookCount();
for (int i = 0; i < bookCount; i++) {
if(bookid.equals(Datas.BookStore[i].getBookid())){
return Datas.BookStore[i];
}
}
return null;
}
//销售
//bookID 要入库的图书id
//count 入库数量
public boolean outStore(String bookId,int count){
Book book = findByid(bookId);
if(book == null){
return false;
}
if(book.getCount() < count){
return false;
}
book.setCount(book.getCount() - count);
return true;
}
//改价
public boolean updata(String bookid,double price){
Book book = findByid(bookid);
if (book == null){
return false;
}
book.setPrice(price);
return true;
}
}
BookView类:界面层
import java.util.Scanner;
public class BookView {
private Scanner input = null;
private BookBiz bookBiz = null;//将图书的业务类当作属性
public BookView() {
input = new Scanner(System.in);
bookBiz = new BookBiz();
System.out.println("----------------------------------------------------");
System.out.println("\t欢迎使用kyy的图书店管理系统");
System.out.println("\t 1.登录 2.退出系统");
System.out.println("----------------------------------------------------");
System.out.println("请选择:");
String choic = input.next();
if("1".equals(choic)){
//执行登录界面
//显示主菜单
System.out.println("登录成功");
while (showMainView()){ }
}
System.out.println("kyy图书管理系统已成功退出,欢迎再次使用!");
}
public boolean showMainView(){
System.out.println("\nkyy图书管理系统>>主菜单");
System.out.println("1.新增图书\t2.删除\t3.查看图书\t4.按编号查询\t5.销售\t6.改价\t7.退出");
System.out.println("请选择:");
String choice = input.next();
switch (choice){
case "1" :
showAddBookView();//新增图书界面
showBooks(Datas.BookStore);
break;
case "2":
showDelBookView();//删除页面
break;
case "3":
showBooks(null);
break;
case "4":
findByid();
break;
case "5":
showOutStore();
break;
case "6":
showupdata();
break;
case "7":
//System.exit(0);
return false;
default:
break;
}
return true;
}
//改价
public void showupdata(){
System.out.println("\nkyy图书店管理系统>> 改价");
System.out.println("请输入改价的图书编号:");
String id = input.next();
System.out.println("改价为:");
double price = input.nextInt();
if(bookBiz.updata(id,price)){
System.out.println("改价成功!");
showBooks(null);
}else {
System.out.println("改价失败,请检查输入的图书编号!");
}
}
//销售
public void showOutStore(){
System.out.println("\nkyy图书店管理系统>> 销售");
System.out.println("请输入出售的图书编号:");
String id = input.next();
System.out.println("销售数量:");
int bookCount = input.nextInt();
if(bookBiz.outStore(id,bookCount)){
System.out.println("销售成功");
showBooks(null);
}else {
System.out.println("销售失败,请检查输入的图书编号!");
}
}
//按编号查找
public void findByid(){
try {
System.out.println("\nkyy图书店管理系统>> 按编号查询");
System.out.println("请输入要查找的图书编号:");
String id = input.next();
Book book = bookBiz.findByid(id);
showBooks(book);
}catch (Exception e){
}
}
//删除图书界面
public void showDelBookView(){
System.out.println("\nkyy图书店管理系统>> 删除图书");
System.out.println("请输入要删除的图书id:");
String delId = input.next();
Book delbook = new Book();
delbook.setBookid(delId);
if(bookBiz.delBook(delbook)){//删除成功
System.out.println("删除成功!");
showBooks(null); //打印默认的图书仓库
}else {
//删除失败
System.out.println("删除失败!");
}
}
//新增图书界面
public Book showAddBookView(){
System.out.println("\nkyy图书店管理系统>> 新增图书");
System.out.println("编号:");
String bookId = input.next();
//验证是否存在
Book newBook = bookBiz.findByid(bookId);
if(newBook == null){
newBook = new Book();
newBook.setBookid(bookId);
System.out.println("名称:");
newBook.setBookname(input.next());
System.out.println("库存:");
newBook.setCount(input.nextInt());
bookBiz.addBook(newBook);//调用,新增图书
}else {//图书已存在
System.out.println("此编号已存在");
newBook = showAddBookView();//重新调用,让用户再次输入
}
return newBook;
}
//显示所有的图书信息
public void showBooks(Book ...bookArray){
System.out.println("编号\t图书名称\t\t\t库存\t\t\t价格");
if(bookArray == null){//要么传送有内容的数组,打印。要么传递空,打印仓库
bookArray = Datas.BookStore;
}
int bookCount = bookBiz.getBookCount(bookArray);
for (int i = 0; i < bookCount; i++) {
System.out.printf("%s\t\t%s\t\t%d\t\t%f\n",bookArray[i].getBookid(),bookArray[i].getBookname(),bookArray[i].getCount(),bookArray[i].getPrice());
}
}
}
Datas:仓库
public class Datas {
//仓库
public static Book[] BookStore = new Book[999];
//用来测试
static {
Book one = new Book("1000","李易达外传","100-101",99.9,10);
Book two = new Book("1001","马超外传","100-102",9.9,20);
Book three= new Book("1002","战神外传","100-103",999.9,1);
BookStore[0] = one;
BookStore[1] = two;
BookStore[2] = three;
}
}
Main
public class Main {
public static void main(String[] args) {
new BookView();
}
}