本文主要采用MVC的编程方式
1,编写视图
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package book;
import java.io.PrintStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class View {
Scanner input;
public View() {
this.input = new Scanner(System.in);
}
public void welcome() {
System.out.println("欢迎使用图书系统,请按提示操作");
}
public void bye() {
System.out.println("欢迎下次使用");
}
public int main_Menu() {
System.out.println("进入图书系统,请按提示操作");
System.out.println("1:进入管理员系统");
System.out.println("2:进入用户系统");
System.out.println("0:退出");
String text = this.input.nextLine();
int num = -1;
try {
num = Integer.parseInt(text);
} catch (NumberFormatException var4) {
}
if (num >= 0 && num <= 2) {
return num;
} else {
System.out.println("输入的指令不正确,请重新输入");
return this.main_Menu();
}
}
public int a_Menu() {
System.out.println("进入管理员系统,请按提示操作");
System.out.println("1:图书新增");
System.out.println("2:图书修改");
System.out.println("3:图书删除");
System.out.println("4:查看所有图书");
System.out.println("0:返回上级目录");
String text = this.input.nextLine();
int num = -1;
try {
num = Integer.parseInt(text);
} catch (NumberFormatException var4) {
}
if (num >= 0 && num <= 4) {
return num;
} else {
System.out.println("输入的指令不正确,请重新输入");
return this.a_Menu();
}
}
public String u_Menu() {
System.out.println("欢迎进入用户系统,请按提示操作");
System.out.println("请输入想要查找的图书书名");
String bookName = this.input.nextLine();
return bookName;
}
public Book addBook() {
System.out.println("请输入你想要添加的图书名字");
String bookName = this.input.nextLine();
System.out.println("请输入你想要添加的图书价格");
String strBookCost = this.input.nextLine();
System.out.println("请输入你想要添加的图书的出版日期, 日期格式:yyyy-MM-dd");
String strBookDate = this.input.nextLine();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date bookDate;
try {
bookDate = dateFormat.parse(strBookDate);
} catch (ParseException var9) {
System.out.println("输入的日期格式不对,请重新输入");
return this.addBook();
}
int bookCost = -1;
try {
bookCost = Integer.parseInt(strBookCost);
} catch (NumberFormatException var8) {
}
if (bookCost <= 0) {
System.out.println("您的输入有误,请重新输入");
return this.addBook();
} else {
Book book = new Book();
book.setBookName(bookName);
book.setBookCost(bookCost);
book.setBookDate(bookDate);
return book;
}
}
public String findByBookName() {
System.out.println