类和对象实现简单的图书管理
package com.fnd.book.entity;
import com.fnd.book.biz.BookBiz;
import com.fnd.book.entity.ex.EX;
public class User {
public String name;
public String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/**
* 图书入库
* @param bookId
* @param num
*/
public void in(int bookId,int num){
BookBiz bookBiz=new BookBiz();
bookBiz.inBook(bookId, num);
}
/**
* 图书出库
* @param bookId
* @param num
*/
public void out(int bookId, int num) {
BookBiz bookBiz=new BookBiz();
bookBiz.outBook(bookId, num);
}
/**
* 新增图书
*/
public void save(Book book) {
BookBiz bookBiz=new BookBiz();
bookBiz.saveBook(book);
System.out.println("新增图书成功!");
}
/**
* 买赠品
*/
public EX buyEx(int exCode) {
BookBiz bookBiz=new BookBiz();
return bookBiz.buyEx(exCode);
}
/**
* 购买图书
*/
public Book buy(int bookId, int num) {
BookBiz bookBiz=new BookBiz();
return bookBiz.buyBook(bookId,num);
}
/**
* 结账
*/
public void checkout(Book book) {
BookBiz bookBiz=new BookBiz();
bookBiz.checkout(book);
}
/**
* 库存查询
*/
public void query(){
BookBiz bookBiz = new BookBiz();
bookBiz.query();
}
}
package com.fnd.book.entity;
import com.fnd.book.entity.ex.EX;
public class Book {
public int id;
public String name;
public String author;
public String pub_date;
public int store;
public double price;
public int num;
public EX ex;
//不添加构造方法:每一个属性的赋值由用户一项一项输入
/**
* 计算价钱
* @return 总价
*/
public double cost() {
//显示书名和单价及购买数量
System.out.println("购买的图书:"+name+"价格:"+price+"数量:"+num);
//先输出不购买赠品情况下的价格
System.out.println("不购卖赠品的总价格:"+(price*num));
//还要考虑购买赠品的情况:加赠品价格
return price*num;
}
public Book(int id, String name,String author, String pub_date, double price, int store) {
// TODO Auto-generated constructor stub
this.id=id;
this.name=name;
this.author=author;
this.pub_date=pub_date;
this.store=store;
this.price=price;
}
public Book() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public EX getEx() {
return ex;
}
public void setEx(EX ex) {
this.ex = ex;
}
//static final EX;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPub_date() {
return pub_date;
}
public void setPub_date(String pub_date) {
this.pub_date = pub_date;
}
public int getStore() {
return store;
}
public void setStore(int store) {
this.store = store;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
//CD,Pen与Bag相同
package com.fnd.book.entity.ex;
public class Bag extends EX {
public Bag() {
setEx_name("BAG");
setPrice(2.7);
}
}
//附赠品类
package com.fnd.book.entity.ex;
public abstract class EX {
private String ex_name;
private double price;
public String getEx_name() {
return ex_name;
}
public void setEx_name(String ex_name) {
this.ex_name = ex_name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
/**
* 附赠品价格计算
* @return
*/
public double cost() {
//输出附赠品的名称和价格,返回价格
System.out.println("购买的附赠品名称是:"+ex_name+"价格是:"+price);
return price;
}
}
package com.fnd.book.entity.ex;
public class ExFactory {
/**
* 创建附赠品
* @param id 用户选择赠品的编号
* @return 对应的赠品对象
*/
public static EX create(int id) {
switch (id) {
case 1:
return new CD();
case 2:
return new Bag();
case 3:
return new Pen();
default:
return null;
}
}
}
package com.fnd.book.biz;
import com.fnd.book.entity.Book;
import com.fnd.book.entity.ex.EX;
import com.fnd.book.entity.ex.ExFactory;
public class BookBiz {
// 对象数组
static Book[] books = new Book[30];
Book book = new Book();
/**
* 初始化5本圖書信息
/
public static void makeData() {
// 创建5本书对象,添加至books数组
//构造方法初始化
Book book1 = new Book(10001, "白夜行", "冬夜圭吾", "2010-12-12", 11.0, 11);
Book book2 = new Book(10002, "白夜行", "冬夜圭吾", "2010-12-12", 11.0, 11);
Book book3 = new Book(10003, "白夜行", "冬夜圭吾", "2010-12-12", 11.0, 11);
Book book4 = new Book(10004, "白夜行", "冬夜圭吾", "2010-12-12", 11.0, 11);
Book book5 = new Book(10005, "白夜行", "冬夜圭吾", "2010-12-12", 11.0, 11);
//setter初始化
/ book1.setId(10001);
book1.setName("白夜行");
book1.setAuthor("张华");
book1.setPub_date("2010-12-12");
book1.setPrice(12.4);
book1.setStore(20);/
// 对象放入对象数组
books[0] = book1;
books[1] = book2;
books[2] = book3;
books[3] = book4;
books[4] = book5;
/// 遍历输出图书信息
for (int i = 0; i < books.length; i++) {
Book b = books[i];
// System.out.println(b.getId()+"\t"+b.getName()+"\t"+b.getAuther()+"\t"+b.getPub_date()+"\t"+b.getPrice()+"\t"+b.getStore());
}*/
}
/**
* 查询图书是否存在:不管是新增、入库、出库、结账都需先判断
* @param id 编号
* @return Book对象
/
public Book findBookById(int id){
for (int i = 0; i < books.length; i++) {
if (books[i]!=null) {
if(books[i].getId()==id){
return books[i];
}
}
}
return null;//for循环结束还未返回,表示未找到
}
/*
* 图书入库
*/
public void inBook(int bookId, int num) {
//先查找是否存在
Book book = findBookById(bookId);
//不存在就返回,存在则增加book[i]的库存量
if (book==null) {
System.out.println("要入库的图书不存在");
}else {
//存在增加库存
book.setStore(book.getStore()+num);
System.out.println("图书入库成功!");
}
}
/**
* 图书出库
*/
public void outBook(int bookId,int num) {
//先查找是否存在
Book book =findBookById(bookId);
//如找到,减少该book[i]的库存量
if (book==null) {
System.out.println("要出库的图书不存在");
}else{
//存在图书,可以出库,判断库存和出库数量
if (book.getStore()>=num) {
//可以出库
book.setStore(book.getStore()-num);
System.out.println("图书出库成功!");
}else{
//不合法
System.out.println("库存不足");
}
}
}
/**
* 新增图书
* @param book
*/
public void saveBook(Book book) {
//方法1
/*for (int i = 0; i < books.length; i++) {
if (books[i]==null) {
books[i+1]=book;
book.setId(books[i-1].getId()+1);
break;
}
}*/
//方法2
//为新增输入编号,为上一本书编号加1
int index=-1;
for (int i = 0; i < books.length; i++) {
if (books[i]==null) {
index=i;
break;
}
}
//取最后一本书的id
int id =books[index-1].getId();
book.setId(id+1);
//把新增对象添加到数组
books[index]=book;
}
/**
* 查询图书
*/
public void query() {
// 遍历输出图书信息
//方法1
/*System.out.println("书号\t" + "书名\t" + "作者\t" + "发布日期\t"
+ "价格\t" + "库存");
for (Book b : books) {
if (b != null) {
System.out.println(b.getId() + "\t" + b.getName() + "\t" + b.getAuthor() + "\t" + b.getPub_date() + "\t"
+ b.getPrice() + "\t" + b.getStore());
}
}*/
//方法2
System.out.println("序号\t书名\t作者\t出版日期\t\t价格\t库存");
//循环Books数组
for (int i = 0; i < books.length; i++) {
if (books[i]!=null) {
System.out.println(books[i].getId()+"\t"+books[i].getName()+"\t"+books[i].getAuthor()+"\t"+books[i].getPub_date()+"\t"+books[i].getPrice()+"\t"+books[i].getStore());
}
}
}
/**
* 购买附赠品
* @return 附赠品
*/
public EX buyEx(int exCode) {
return ExFactory.create(exCode);//调用工厂里的方法,根据用户选择的数字生成对应的附赠品
}
/**
* 结账
*/
public void checkout(Book book){
double totalPrice;
//减少库存数量
book.setStore(book.getStore()-book.getNum());
//输出总价格
if (book.getEx()!=null) {
//购买的有附赠品
totalPrice = book.cost()+book.getEx().getPrice();
System.out.println("购买附赠品后的总价格="+totalPrice);
}
}
/**
* 购买图书
* @return 被购买书的对象
*/
public Book buyBook(int bookId, int num) {
// 查找到图书
Book book=findBookById(bookId);
if (book==null) {
System.out.println("要购买的图书不存在");
}else {
//找到购买图书,蛇者购买book[i]的购买数量
if (num<book.getStore()) {
book.setNum(num);
}else{
System.out.println("库存不足");
}
}
return book;
}
}
package com.fnd.book;
import java.util.Scanner;
import com.fnd.book.biz.BookBiz;
import com.fnd.book.entity.*;
import com.fnd.book.entity.ex.EX;
public class TestBook {
Scanner input = new Scanner(System.in);
User user = new User();
public static void main(String[] args) {
BookBiz.makeData();
TestBook test = new TestBook();
System.out.println("欢迎使用图书书店");
//登陆
test.accreditRole();
}
private void accreditRole() {
// TODO Auto-generated method stub
System.out.print("请输入用户名:");
String username = input.next();
System.out.print("请输入密码:");
String pwd = input.next();
//给属性赋值
user.setName(username);
user.setPassword(pwd);
if (username.equals("admin")&&pwd.equals("admin")) {
doStoreMgr(user);//管理员
}else {
doCustomer(user);//用户
}
}
/**
* 顾客的操作
*/
private void doCustomer(User user) {
System.out.println("*****欢迎光临图书系统*****");
System.out.println("请选择进行的操作:1.查看图书 2.结账 3.退出");
int choice=input.nextInt();
switch (choice) {
case 1:
//查询
user.query();
doCustomer(user);
break;
case 2:
//购买
Book book = null;
System.out.println("请输入想要购买的图书信息:");
System.out.println("请输入图书ID:");
int id = input.nextInt();
System.out.println("请输入购买数量:");
int num = input.nextInt();
book = user.buy(id, num);
if (book != null) {
while (true) {
System.out.print("1.CD 27.50元 2.包装 2.70元 3.钢笔 10元 4.不需要\n请输入需要购买的附赠品:");
int exCode = input.nextInt();
if (exCode >= 1 && exCode <= 4) {
EX ex = user.buyEx(exCode);
book.setEx(ex);//将赠品对象传递为book对象,计算总价
user.checkout(book);
break;
}
}
}
doCustomer(user);
break;
case 3:
//退出
System.out.println("谢谢使用!");
return;
default:
break;
}
}
/**
* 库存管理员操作
*/
private void doStoreMgr(User user) {
System.out.println("欢迎登陆库存管理系统");
System.out.print("请选择进行的操作:");
//方法1
/*String inits[] = { "1.图书入库", "2.图书出库", "3.查询全部图书", "4.新增图书", "5.退出" };
for (String string : inits) {
System.out.print(string);
}
System.out.println();*/
//方法2
System.out.println("请选择进行的操作:1.图书入库 2.图书出库 3.查询全部图书 4.新增图书 5.退出");
int choice=0;
choice = input.nextInt();
int num;
switch(choice){
case 1:
//入库
System.out.println("输入入库的图书ID:");
int bookId=input.nextInt();
System.out.println("输入入库的数量");
num=input.nextInt();
user.in(bookId, num);
doStoreMgr(user); //这样可以实现循环
break;
case 2:
//出库
System.out.print("请输入ID:");
int id = input.nextInt();
System.out.print("请输入出库的数量:");
num = input.nextInt();
user.out(id, num);// 出库
doStoreMgr(user);
break;
case 3:
//查询
user.query();
doStoreMgr(user); //这样可以实现循环
break;
case 4:
//新增
Book book = new Book();
System.out.print("请输入图书信息");
System.out.print("请输入书名:");
String name = input.next();
System.out.print("请输入作者:");
String author = input.next();
System.out.print("请输入出版时间:");
String pub_date = input.next();
System.out.print("请输入价格:");
double price = input.nextDouble();
System.out.print("请输入数量:");
int store = input.nextInt();
book.setName(name);
book.setAuthor(author);
book.setPub_date(pub_date);
book.setPrice(price);
book.setStore(store);
user.save(book);
doStoreMgr(user);
break;
case 5:
//退出
System.out.println("谢谢使用");
return;
default:
System.out.println("输入有误");
break;
}
}
}