-图书管理训练任务
作者:互联网
还记得之前的图书管理吗?
学习了集合以后,使用集合来进行图书信息的 存储吧。。
1.管理员登陆
2.图书管理
2.1 图书新增
2.2 图书修改
2.3 图书删除
2.4 根据图书名称模糊查找图书
2.5 查看所有图书 (三种排序)
2.6(新增)价格从高到低排序
2.7(新增)价格从低到高排序
2.8(新增)新旧排序(出版日期排序)
模块图
bean.Book
public class Book implements Comparable<Book>{
private String name;
private double price;
private int year;
private int month;
public Book() {
}
public Book(String name, double price, int year, int month) {
this.name = name;
this.price = price;
this.year = year;
this.month = month;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return year == book.year && month == book.month && Objects.equals(name, book.name);
}
@Override
public int hashCode() {
return Objects.hash(name, price, year, month);
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
", year=" + year +
", month=" + month +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
@Override
public int compareTo(Book o) {
if(this.price ==o.price) {
return 0;
}else if(this.price -o.price >0){
return 1;
}else{
return -1;
}
}
public static class BookComparator implements Comparator<Book>{
@Override
public int compare(Book o1, Book o2) {
if((o1.getYear() == o2.getYear()) && (o1.getMonth() == o2.getMonth())) {
return 0;
}else if((o1.getYear()-o2.getYear()>0)|| ((o1.getYear()==o2.getYear())&&(o1.getMonth()-o2.getMonth()>0))){
return 1;
}else {
return -1;
}
}
}
}
dao.BookDao
public class BookDao {
//书柜
private List<Book> list = new ArrayList<>();
public BookDao(){
list.add(new Book("静夜思",12,1994,6));
list.add(new Book("一",20,2000,12));
list.add(new Book("二",40,2020,12));
list.add(new Book("三",35,2021,12));
}
public List<Book> getBookList(){
return list;
}
public Book add(Book book) {
list.add(book);
return book;
}
public boolean del(String name) {
int index = findBookByName(name);
if(index==-1){
System.out.println("图书不存在!");
}
list.remove(index);
return true;
}
public int findBookByName(String name){
for(int i=0;i<list.size();i++){
if(list.get(i).getName().equals(name)){
return i;
}
}
return -1;
}
public boolean update(String name, Book newBook) {
int index = findBookByName(name);
if(index == -1){
System.out.println("该图书不存在!");
}
Book book = list.get(index);
book.setName(newBook.getName());
book.setPrice(newBook.getPrice());
book.setYear(newBook.getYear());
book.setMonth(newBook.getMonth());
return true;
}
public Book findBookByWord(String word) {
for(int i=0;i<list.size();i++){
if(list.get(i).getName().contains(word)){
return list.get(i);
}
}
return null;
}
}
view.BookView
public class BookView {
Scanner input = new Scanner(System.in);
private BookDao bookDao = new BookDao();
public int menu() {
int menu = -1;
do {
System.out.println("1.图书新增");
System.out.println("2.图书删除");
System.out.println("3.图书修改");
System.out.println("4.根据图书名称模糊查找图书");
System.out.println("5.查看所有图书");
System.out.println("0.退出");
String strNum = input.nextLine();
try {
menu = Integer.parseInt(strNum);
break;
} catch (Exception e) {
System.out.println("请输入数字!");
menu();
}
if (menu < 0 || menu > 5) {
System.out.println("请输入正确的序号!");
menu();
}
}while(true);
if(menu == 0){
System.out.println("谢谢使用!");
}else if(menu == 1){
add();
}else if(menu == 2){
del();
}else if(menu == 3){
update();
}else if(menu == 4){
System.out.println("根据图书名称模糊查找图书");
System.out.println("请输入图书名称包含的关键字:");
String word = input.nextLine();
try{
Book book = bookDao.findBookByWord(word);
if(book!=null){
System.out.println("找到图书了:"+book);
}else{
System.out.println("查找失败");
}
}catch (Exception e){
System.out.println("查找失败!");
System.out.println(e.getMessage());
}
}else if(menu == 5){
int num = -1;
List<Book> list = bookDao.getBookList();
do {
System.out.println("请选择查看顺序");
System.out.println("1.随机查看");
System.out.println("2.按价格升序查看");
System.out.println("3.按出版升序查看");
System.out.println("0.退出");
System.out.println("---原顺序查看所有图书---");
String numStr = input.nextLine();
try {
num = Integer.parseInt(numStr);
break;
} catch (Exception e) {
System.out.println("请输入数字!");
System.out.println(e.getMessage());
}
}while(true);
if(num<0 || num>4){
System.out.println("请输入正确的序号!");
}else if(num == 1) {
for (Book book : list) {
System.out.println(book);
}
}else if(num == 2) {
System.out.println("---按价格排序---");
Collections.sort(list);
for (Book book : list) {
System.out.println(book);
}
}else if(num == 3) {
System.out.println("---按出版日期排序---");
Collections.sort(list,new Book.BookComparator());
for (Book book : list) {
System.out.println(book);
}
}
}
return menu;
}
public void update(){
System.out.println("---修改图书---");
System.out.println("请输入修改的图书名称:");
String name =input.nextLine();
System.out.println("请输入新的图书名称、价格、年份、月份");
Book newBook = new Book();
newBook.setName(input.nextLine());
String priceStr=input.nextLine();
double price = Double.parseDouble(priceStr);
newBook.setPrice(price);
String yearStr = input.nextLine();
int year = Integer.parseInt(yearStr);
newBook.setYear(year);
String monthStr = input.nextLine();
int month = Integer.parseInt(monthStr);
newBook.setMonth(month);
try{
if(bookDao.update(name,newBook)){
System.out.println("修改成功!");
}else{
System.out.println("修改失败!");
}
}catch (Exception e){
System.out.println("修改失败!");
System.out.println(e.getMessage());
}
}
public void del(){
System.out.println("---图书删除---");
System.out.println("请输入删除的图书名称:");
String name = input.nextLine();
try{
if(bookDao.del(name)){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}catch (Exception e){
System.out.println("删除失败");
System.out.println(e.getMessage());
}
}
public void add(){
System.out.println("---图书新增---");
Book book = new Book();
String name = insertName();
double price = insertPrice();
int year = insertYear();
int month = insertMonth();
book.setName(name);
book.setPrice(price);
book.setYear(year);
book.setMonth(month);
try{
book = bookDao.add(book);
System.out.println("新增成功!书名:"+book.getName());
}catch (Exception e){
System.out.println("新增失败!");
System.out.println(e.getMessage());
}
}
public String insertName(){
System.out.println("请输入书名:");
String name = input.nextLine();
return name;
}
public double insertPrice(){
double price = 0;
System.out.println("请输入价格:");
String priceStr = input.nextLine();
try{
price = Double.parseDouble(priceStr);
}catch (Exception e){
System.out.println("请输入价格数字!");
return insertPrice();
}
if(price < 0){
System.out.println("价格不能为负!请重新输入");
return insertPrice();
}
return price;
}
public int insertYear(){
int year = -1;
System.out.println("请输入出版年份:");
String yearStr = input.nextLine();
try{
year = Integer.parseInt(yearStr);
}catch (Exception e){
System.out.println("请输入年份数字!");
return insertYear();
}
if(year < 0 || year >2022){
System.out.println("年份有误,请重新输入!");
return insertYear();
}
return year;
}
public int insertMonth(){
int month = -1;
System.out.println("请输入出版的月份:");
String monthStr = input.nextLine();
try{
month = Integer.parseInt(monthStr);
}catch (Exception e){
System.out.println("请输入月份数字!");
return insertMonth();
}
if(month <0 || month >12){
System.out.println("月份输入有误,请重新输入!");
return insertMonth();
}
return month;
}
}
main.MainTest
public class MainTest {
public static void main(String[] args) {
BookView bookView = new BookView();
Scanner input = new Scanner(System.in);
Login login = new Login();
while (true) {
login.login();
break;
}
while(true) {
if(bookView.menu()==0){
break;
}
}
}
}
标签:训练任务,return,管理,System,Book,println,public,图书,out 来源: https://blog.csdn.net/mozhishao/article/details/118485177