图书管理(控制台输入)练习
建一个book类
package com.booktest;
//属性:编号 书名 作者 价格 书籍类型(如 玄幻 文学等)
public class Book {
private int number;
private String bookname;
private String author;
private double price;
private int count;
private String type;
public Book() {
}
public Book(int number, String bookname, String author, double price, int count, String type) {
super();
this.number = number;
this.bookname = bookname;
this.author = author;
this.price = price;
this.count = count;
this.type = type;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "[编号:" + number + ", 书名:" + bookname + ", 作者:" + author + ", 价格:" + price
+ ", 数量:" + count + ", 类型:" + type + "]";
}
}
建一个图书管理类
package com.booktest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/*
* 方法:
* a.添加书籍
* b.按照书名查询书籍
* c.卖书,通过卖书修改书籍数量(主要不要负数)
* d.通过编号,删除书籍
* e.通过书籍类型,查询所有书籍
*/
public class BookManager {
List<Book> book=new ArrayList<>();
//初始化
public void init() {
Book book1=new Book(11101, "倚天屠龙记", "金庸", 23.0, 20, "武侠");
Book book2=new Book(11102, "雪中悍刀行", "总管", 39.0, 10, "武侠");
Book book3=new Book(11103, "斗罗大陆", "唐家三少", 39.0, 10, "玄幻");
book.add(book1);
book.add(book2);
book.add(book3);
}
//添加书籍
public List<Book> addBook(int number,String booknmae, String author, double price, int count, String type ) {
Book bokk=new Book();
bokk.setNumber(number);
bokk.setBookname(booknmae);
bokk.setAuthor(author);
bokk.setPrice(price);
bokk.setCount(count);
bokk.setType(type);
book.add(bokk);
return book;
}
//按照书名查询书籍
public List<Book> selectFromName(String bookname){
Iterator<Book> it=book.iterator();
while (it.hasNext()) {
Book book = (Book) it.next();
if(book.getBookname().equals(bookname)) {
System.out.println(book);
}
}
return book;
}
//卖书,通过卖书修改书籍数量(主要不要负数)
public void saleBook(String bookname,int count) {
Iterator<Book> it=book.iterator();
while (it.hasNext()) {
Book book = (Book) it.next();
if(book.getBookname().equals(bookname)) {
int zongshu=book.getCount();
if(book.getCount()>=count) {
zongshu-=count;
book.setCount(zongshu);
System.out.println(book);
}else {
System.out.println("您要购买的书暂时没有那么多!!!");
}
}
}
}
//通过编号,删除书籍
public void deleteBook(int number) {
//克隆解决崩溃
//或者break;
Iterator<Book> it=book.iterator();
while (it.hasNext()) {
Book book = (Book) it.next();
if(book.getNumber()==number) {
it.remove();
}
}
}
//通过书籍类型,查询所有书籍
public void selectAll(String type) {
for (Book book2 : book) {
if(book2.getType().equals(type)) {
System.out.println(book2);
}
}
}
public void selectMore() {
for (Book book2 : book) {
System.out.println(book2);
}
}
public void toolTest() {
BookManager bookManger=this;
Scanner scanner=new Scanner(System.in);
System.out.println("*******欢迎来到图书管理********");
System.out.println("1.增加书籍");
System.out.println("2.根据名字查找书籍");
System.out.println("3.根据名字买书籍");
System.out.println("4.根据书籍编号删除书籍");
System.out.println("5.根据类型查找书籍");
System.out.println("6.查询图书");
int num=scanner.nextInt();
switch (num) {
case 1:
System.out.println("请输入图书编号:");
int number=scanner.nextInt();
System.out.println("请输入书名:");
String bookname=scanner.next();
System.out.println("请输入作者:");
String author=scanner.next();
System.out.println("请输入书籍的价格:");
double price=scanner.nextDouble();
System.out.println("请输入书籍的数目:");
int count=scanner.nextInt();
System.out.println("请输入书籍的类型:");
String type=scanner.next();
bookManger.addBook(number,bookname, author, price, count, type);
bookManger.toolTest();
break;
case 2:
System.out.println("请输入书籍的名字:");
String bookname1=scanner.next();
bookManger.selectFromName(bookname1);
bookManger.toolTest();
break;
case 3:
System.out.println("请输入书籍的名字:");
String bookname2=scanner.next();
System.out.println("请输入你要买几本");
int num1=scanner.nextInt();
bookManger.saleBook(bookname2, num1);
bookManger.toolTest();
break;
case 4:
System.out.println("请输入书籍编号:");
int number1=scanner.nextInt();
bookManger.deleteBook(number1);
bookManger.toolTest();
break;
case 5:
System.out.println("请输入你要查找的书籍类型:");
String type1=scanner.next();
bookManger.selectAll(type1);
bookManger.toolTest();
break;
case 6:
bookManger.selectMore();
bookManger.toolTest();
default:
break;
}
}
}
建一个测试类
package com.booktest;
public class Main {
public static void main(String[] args) {
BookManager bookManger=new BookManager();
bookManger.init();
bookManger.toolTest();
}
}
写的可能有很多不完善的地方,不过以我现在的水平我已经很满意了,好多东西都还不是很理解比如迭代器一边查询一边删除(或更改)就运行不了,虽然最后也能运行但就是不会使用克隆break解决;慢慢进步吧!哪里有错还希望指点出来,不要喷我喔,希望与大家一起进步,最后声明一点这只是我所想的,只要你写的对就按照自己的想法写,最后非常感谢!!!
题目在上篇最后可以点这里