package view;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
import controller.BookController;
import controller.UserController;
import entry.Book;
public class BookManageView {
private Scanner sc = new Scanner(System.in);
private BookController bController = new BookController();
private UserController uController = new UserController();
private int key = 0;// 用来记录用户的登录状态,0表示未登录,1表示已登录
public void printMain() {
System.out.println("------欢迎使用图书管理系统------");
System.out.println("1.查询所有图书 2.按类别查询图书 3.添加图书 4.更新图书 5.删除图书");
System.out.println("6.借书 7.还书 ");
System.out.println("8.退出登录 0.退出");
}
public void printloginAndReg() {
System.out.println("1.注册 2.登录");
System.out.println("0.返回");
}
public void selectAll() {
List<Book> books = bController.selectAll();
System.out.println("编号" + "\t" + "书名" + "\t\t" + "价格" + "\t" + "添加时间" + "\t\t\t" + "类型");
for (Book book : books) {
System.out.println(book);
}
}
public void selectByType() {
System.out.println("请输入您要查询的类别:");
String type = sc.next();
List<Book> books = bController.selectAll(type);
if (books.size() == 0)
System.out.println("该类别暂时没有图书");
for (Book book : books) {
System.out.println(book);
}
}
public void addBook() {
System.out.println("请输入图书名字:");
String bname = sc.nextLine();
System.out.println("请输入图书价格:");
Double bprice = sc.nextDouble();
sc.nextLine();
System.out.println("请输入图书类别:");
String type = sc.nextLine();
String addtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
Book book = new Book(0, bname, bprice, addtime, "0", 0, type);
boolean flag = bController.addBook(book);
if (flag) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
}
public void updateBook() {
selectAll();
System.out.println("请输入您想要修改的图书名字:");
String name = sc.nextLine();
System.out.println("请输入新的图书名字:");
String bname = sc.nextLine();
System.out.println("请输入新的价格:");
Double bprice = sc.nextDouble();
sc.nextLine();
System.out.println("请输入图书类型:");
String type = sc.nextLine();
String updatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
boolean flag = bController.updateBook(name, bname, bprice, updatetime, type);
if (flag) {
System.out.println("更新成功");
} else {
System.out.println("更新失败");
}
}
public void deleteBook() {
selectAll();
System.out.println("请输入您想要删除的图书的名字:");
String bname = sc.nextLine();
boolean flag = bController.deleteBook(bname);
if (flag)
System.out.println("删除成功");
System.out.println("删除失败");
}
public void login() {
System.out.println("----登录----");
System.out.println("请输入您的用户名:");
String username = sc.nextLine();
System.out.println("请输入您的密码:");
String password = sc.nextLine();
boolean flag = uController.login(username, password);
if (flag) {
System.out.println("登录成功!");
key = 1;
} else {
System.out.println("登录失败!");
}
}
public void reg() {
//也可以使用递归,不用while循环
while (true) {
System.out.println("----注册----");
System.out.println("请输入你要使用的用户名:");
String username = sc.nextLine();
System.out.println("请输入你要使用的密码:");
String password = sc.nextLine();
System.out.println("请再次输入密码:");
String psd = sc.nextLine();
boolean flag = uController.verify(username);
if (flag) {
if (password.equals(psd)) {
uController.register(username, password);
System.out.println("注册成功");
login();
break;
} else {
System.out.println("前后两次输入的密码不一致,请重新进行注册");
continue;
}
} else {
System.out.println("该用户名已被占用,请重新进行注册");
}
}
}
public void borrowBook() {
if (key == 0) {
System.out.println("您还没有登录,登录后才可以借书.");
while (true) {
printloginAndReg();
int choice = choice();
if(choice == 0)
break;
switch (choice) {
case 1:
reg();
borrowBook();
break;
case 2:
login();
borrowBook();
break;
default:
System.out.println("您的选择有误,请重新选择:");
break;
}
}
} else {
selectAll();
System.out.println("请输入你想要借的书的名字:");
String bname = sc.nextLine();
// state表示书本的状态,0表示未借出,1则表示已借出
boolean flag = bController.updateState(1, bname);
if (flag) {
System.out.println("借书成功!");
} else {
System.out.println("借书失败!");
}
}
}
public List<Book> onLoanBook(){
return bController.onLoanBook();
}
public void returnBook() {
List<Book> books = onLoanBook();
if(books.size() == 0){
System.out.println("您暂时没有借的书本.");
}else{
System.out.println("您借的书本有:");
for (Book book : books) {
System.out.println(book);
}
System.out.println("请输入你要还的书的名字:");
String bname = sc.nextLine();
boolean flag = bController.updateState(0, bname);
if (flag) {
System.out.println("还书成功!");
} else {
System.out.println("还书失败!您输入的书名可能不正确!");
}
}
}
public void exit() {
if (key == 0) {
System.out.println("您还没有登录呢!");
} else {
key = 0;
System.out.println("已退出登录!");
}
}
//选择操作
public int choice() {
System.out.println("请选择您要进行的操作:");
try {
int choice = sc.nextInt();
sc.nextLine();
return choice;
} catch (InputMismatchException ie) {
sc.nextLine();
return 10010;
}
}
public void show() {
while (true) {
printMain();
int choice = choice();
switch (choice) {
case 1:
selectAll();
break;
case 2:
selectByType();
break;
case 3:
addBook();
break;
case 4:
updateBook();
break;
case 5:
deleteBook();
break;
case 6:
borrowBook();
break;
case 7:
returnBook();
break;
case 8:
exit();
break;
case 0:
System.out.println("bye");
System.exit(0);
break;
default:
System.out.println("您的选择有误,请重新选择:");
break;
}
}
}
}