package man;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import control.LoginAction;
import control.ExitAction;
import Dao.*;
import view.*;
public class bookmanagermain {
private static BufferedReader strin;
//图书登录界面
public static Boolean login(){
System.out.println(" 图书管理系统登录界面");
strin=new BufferedReader(new InputStreamReader(System.in));
try{
System.out.print("用户名:");
String name = strin.readLine();
System.out.print("密 码:");
String password = strin.readLine();
Boolean isAdmin = Dao.check(name, password);
while(!isAdmin){
System.out.println("用户名或密码错误,请重新输入用户名(或输入5退出):");
name = strin.readLine();
System.out.println("name:"+name);
if(name.equals("5"))
return false;
System.out.print("密 码:");
password = strin.readLine();
isAdmin = Dao.check(name, password);
}
}catch(IOException e){
e.printStackTrace();
return false;
}
return true;
}
//图书信息添加界面
public static Boolean addbook(){
System.out.println("------------------\n 图书信息添加界面");
try{
Book book = new Book();
System.out.print("编号:");
book.setid(strin.readLine());
System.out.print("名称:");
book.setname(strin.readLine());
System.out.print("作者:");
book.setautor(strin.readLine());
System.out.print("类别:");
book.settype(strin.readLine());
System.out.print("价钱:");
book.setprice(strin.readLine());
int insertresult = Dao.insert(book);
if(insertresult==0){
return false;
}
}catch(IOException e){
e.printStackTrace();
return false;
}
return true;
}
//图书信息添加界面
public static Boolean query(){
System.out.println("------------------\n 查看所有图书信息界面");
System.out.println("图书编号 类别 价格 图书作者 图书名称");
List qlist = Dao.query();
for(Iterator i = qlist.iterator();i.hasNext();){
Map map = (Map)i.next();
System.out.println(map.get("bookid")+" "+
map.get("booktype")+" "+
map.get("bookprice")+" "+
map.get("bookautor")+" "+
map.get("bookname"));
}
return true;
}
//修改界面
public static Boolean update(){
System.out.println("------------------\n 修改图书信息");
System.out.print("请输入所要修改图书的编号:");
Book book = new Book();
try{
book.setid(strin.readLine());
System.out.print("请填写新的图书信息\n图书名称:");
book.setname(strin.readLine());
System.out.print("图书作者:");
book.setautor(strin.readLine());
System.out.print("图书类型:");
book.settype(strin.readLine());
System.out.print("图书价钱:");
book.setprice(strin.readLine());
}catch(IOException e){
e.printStackTrace();
return false;
}
Dao.update(book);
return true;
}
//删除界面
public static Boolean delete(){
System.out.println("------------------\n 删除图书信息");
System.out.print("请输入所要删除的图书名称:");
String name = "";
try{
name = strin.readLine();
}catch(IOException e){
e.printStackTrace();
return false;
}
Dao.delete(name);
return true;
}
//主界面
public static void bookmanageUI(){
System.out.println("------------------\n 图书管理系统主界面");
System.out.println("1、添加图书");
System.out.println("2、查看所有图书信息");
System.out.println("3、删除图书");
System.out.println("4、修改图书信息");
System.out.println("5、退出系统");
System.out.println("请选择(1-5)");
try{
String item = strin.readLine();
switch(item.charAt(0)){
case '1':
if(!addbook()){
System.out.println("添加图书信息失败!");
}
bookmanageUI();
break;
case '2':
query();
bookmanageUI();
break;
case '3':
delete();
bookmanageUI();
break;
case '4':
update();
bookmanageUI();
break;
case '5':
System.exit(0);
break;
default:
break;
}
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
//--------控制台版------------------
//图书管理系统登录界面
//if(!login()){
// return;
//}
//主界面
//bookmanageUI();
//--------SWING版-----------------
LoginFrame login = new LoginFrame();
LoginAction la = new LoginAction(login);
login.getJb_login().addActionListener(la);
ExitAction ea = new ExitAction();
login.getJb_exit().addActionListener(ea);
}
}