图书管理系统分析: 1.定义Book类 2.完成主界面和选择 3.完成查询所有图书 4.完成添加图书 5. 修改图书 6.完成修改图书
1.定义Book类
public class demo01 {
public static void main(String[] args) {
ArrayList<Book> mz = new ArrayList<>();
mz.add(new Book("西游记", 19));
mz.add(new Book("红楼梦", 33.99));
ArrayList<Book> it = new ArrayList<>();
it.add(new Book("java入门到放弃", 88.8));
it.add(new Book("php入门到放弃", 88.8));
Map<String, ArrayList<Book>> bookMap = new HashMap<>();
bookMap.put("名著", mz);
bookMap.put("it书籍", it);
while (true) {
System.out.println("--------欢迎来到图书管理系统--------");
// 1.2.输出功能选项
System.out.println("1.查看书籍");
System.out.println("2.添加书籍");
System.out.println("3.删除书籍");
System.out.println("4.修改书籍");
System.out.println("5.退出");
// 1.3.提示用户输入
Scanner sc = new Scanner(System.in);
int operation = sc.nextInt();
switch (operation) {
case 1:
showBooks(bookMap);
break;
case 2:
addBook(bookMap);
break;
case 3:
deleteBook(bookMap);
break;
case 4:
editBook(bookMap);
break;
case 5:
System.out.println("大爷慢走,欢迎下次再来");
System.exit(0);
break;
default:
System.out.println("大爷您输入的不存在,请重新输入:");
break;
}
}
}
//完成删除图书
private static void deleteBook(Map<String, ArrayList<Book>> bookMap) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的书籍类型:");
String type = sc.next();
System.out.println("请输入要删除的书名:");
String name = sc.next();
ArrayList<Book> books = bookMap.get(type);
if (books == null) {
System.out.println("没有" + type + "这种类型的书籍");
return;
}
for (int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if (book.getName().equals(name)) {
books.remove(i);
}
System.out.println("恭喜您,删除" + name + "成功");
return;
}
}
//完成修改图书
private static void editBook(Map<String, ArrayList<Book>> bookMap) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的书籍类型:");
String type = sc.next();
System.out.println("请输入要修改的书名:");
String name = sc.next();
System.out.println("请输入要新的的书籍名称:");
String newname = sc.next();
System.out.println("请输入要新的的书籍新的价格:");
double newprice = sc.nextDouble();
ArrayList<Book> books = bookMap.get(type);
if (books == null) {
System.out.println("您输入的类型不存在!");
return;
}
for (Book book : books) {
if (book.getName().equals(name)) {
book.setName(newname);
book.setPrice(newprice);
System.out.println("恭喜您,修改" + name + "书籍成功");
return;
}
}
System.out.println("您输入的书籍不存在!");
}
//完成添加图书
private static void addBook(Map<String, ArrayList<Book>> bookMap) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要添加的书籍类型:");
String type = sc.next();
System.out.println("请输入要添加的书名:");
String name = sc.next();
System.out.println("请输入要添加的书的价格:");
double price = sc.nextDouble();
Book newbook = new Book(name, price);
ArrayList<Book> books = new ArrayList<>();
bookMap.put(type, books);
if (books == null) {
books = new ArrayList<>();
bookMap.put(type, books);
}
boolean exists = false;
for (Book book : books) {
if (book.getName().equals(name)) {
exists = true;
}
}
if (exists) {
System.out.println(name + "书籍存在");
} else {
books.add(newbook);
System.out.println("添加" + name + "成功");
}
}
// 完成查询所有图书
private static void showBooks(Map<String, ArrayList<Book>> bookMap) {
System.out.println("类名\t书名\t价格");
Set<Map.Entry<String, ArrayList<Book>>> entrySet = bookMap.entrySet();
for (Map.Entry<String, ArrayList<Book>> entry : entrySet) {
String key = entry.getKey();
System.out.println(key);
ArrayList<Book> value = entry.getValue();
for (Book book : value) {
System.out.println("\t\t" + book.getName() + "\t" + book.getPrice());
}
}
}
}
2.测试类并完成各项功能
/**
* created by sheting on
* User: hl.c
* Date:2019/3/15
* Time: 22:00
*/
public class Book {
private String name;
private double price;
public Book() {
}
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;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
public Book(String name, double price) {
this.name = name;
this.price = price;
}
}
运行效果:
这系统很简易啊,有什么意见留言哦!