分层情况
实体类Book代码
//实体类
public class Book {
private int id;
private String name;
private int bsum;
public Book() {
}
public Book(int id, String name, int bsum) {
this.id = id;
this.name = name;
this.bsum = bsum;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBsum() {
return bsum;
}
public void setBsum(int bsum) {
this.bsum = bsum;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", bsum=" + bsum +
'}';
}
}
控制层BookController代码
import com.xszx.beans.Book;
import com.xszx.service.BookService;
import com.xszx.service.impl.BookServiceImpl;
import java.util.List;
import java.util.Scanner;
//控制层 控制请求转发
public class BookController {
Scanner sc=new Scanner(System.in);
BookService bookService = new BookServiceImpl();
//主方法
public void menu(){
System.out.println();
System.out.println(" ---小张图书管理系统--- ");
System.out.println("***【1】查看图书列表 【2】添加图书 【3】修改图书 【4】删除图书 【0】退出系统***");
System.out.println("请选择您要进行的操作:");
int n = sc.nextInt();
if(n==1){
checkBook();
menu();
}else if (n==2){
addBook();
menu();
}else if(n==3){
alterBook();
menu();
}else if (n==4){
deleteBook();
menu();
}else if(n==0){
System.out.println("系统已退出,欢迎下次使用!!!");
System.exit(0);
}
}
//删除图书
private void deleteBook() {
Book book = new Book();
System.out.println("请输入您要删除的图书名称:");
book.setName(sc.next());
//判断名字是否存在 存在则删除 不存在则提示不存在
boolean b=bookService.getbookname(book);
if(b){
bookService.deleteBook(book);
System.out.println("删除成功!");
} else{
System.out.println("您要删除的图书不存在!");
menu();
}
}
//修改图书
private void alterBook() {
Book book = new Book();
System.out.println("请输入您要修改的图书名称:");
book.setName(sc.next());
//判断名字是否存在 存在则继续修改 不存在则提示不存在
boolean b=bookService.getbookname(book);
if(b){
Book book1 = new Book();
System.out.println("请输入修改后的图书名称:");
book1.setName(sc.next());
System.out.println("请输入修改后的图书库存:");
book1.setBsum(sc.nextInt());
bookService.alterBook(book,book1);
System.out.println("修改成功!");
} else{
System.out.println("您要修改的图书不存在!");
menu();
}
}
//增加图书
private void addBook() {
Book book = new Book();
System.out.println("请输入您要添加的图书名称:");
book.setName(sc.next());
//判断名字是否存在 存在则提示名字冲突 不存在则继续执行
boolean b=bookService.getbookname(book);
if(b){
System.out.println("添加的图书名字冲突");
menu();
}else{
System.out.println("请输入您要添加的图书库存:");
book.setBsum(sc.nextInt());
//后台添加完成,是否成功
int i = bookService.addbook(book);
if (i==1){
System.out.println("添加成功!!!");
menu();
}else{
System.out.println("添加失败!!!");
menu();
}
}
}
//查看图书
private void checkBook() {
List<Book> list = bookService.getbook();
for (Book book : list){
System.out.println(book);
}
menu();
}
}
数据层BookDao代码
import com.xszx.beans.Book;
import java.util.ArrayList;
import java.util.List;
//数据层 连接数据库 处理数据 写sql 后期为接口
public class BookDao {
static List<Book> blist=new ArrayList<>();
static{
blist.add(new Book(1,"三国",666));
blist.add(new Book(2,"水浒",888));
blist.add(new Book(3,"红楼梦",999));
blist.add(new Book(4,"西游记",222));
blist.add(new Book(5,"宝莲灯",333));
}
public List<Book> getbooks() {
return blist;
}
public int addbook(Book book) {
int id = blist.get(0).getId();
//查找最大值
for (Book bk : blist){
if (id<bk.getId()){
id=bk.getId();
}
}
book.setId(id+1);
blist.add(book);
return 1;
}
//判断名字是否存在
public boolean getbookname(Book book) {
for (Book bk : blist){
if(bk.getName().equals(book.getName())){
return true;
}
}
return false;
}
public void alterBook(Book book, Book book1) {
for (Book bk : blist){
//修改参数
if(bk.getName().equals(book.getName())){
bk.setName(book1.getName());
bk.setBsum(book1.getBsum());
}
}
}
public void deleteBook(Book book) {
for (Book bk : blist){
//删除元素
if(bk.getName().equals(book.getName())){
blist.remove(bk);
break;
}
}
}
}
业务层接口BookService代码
import com.xszx.beans.Book;
import java.util.List;
//业务层 处理业务
public interface BookService {
List<Book> getbook();
int addbook(Book book);
boolean getbookname(Book book);
void alterBook(Book book, Book book1);
void deleteBook(Book book);
}
业务层实现类BookServiceImpl代码
import com.xszx.beans.Book;
import com.xszx.dao.BookDao;
import com.xszx.service.BookService;
import java.util.List;
public class BookServiceImpl implements BookService {
BookDao bookDao = new BookDao();
@Override
public List<Book> getbook() {
return bookDao.getbooks();
}
@Override
public int addbook(Book book) {
return bookDao.addbook(book);
}
@Override
public boolean getbookname(Book book) {
return bookDao.getbookname(book);
}
@Override
public void alterBook(Book book, Book book1) {
bookDao.alterBook(book, book1);
}
@Override
public void deleteBook(Book book) {
bookDao.deleteBook(book);
}
}
测试类Test代码
import com.xszx.controller.UserController;
//测试类
public class Test {
public static void main(String[] args) {
System.out.println("欢迎来到小张图书管理系统!!!");
new UserController().menu();
}
}