求助帖
救救孩子吧,为什么输入鬼吹灯明明Book数组是有的返回的是null,还有怎么book[i]也是null,为什么呀
Book work类
package Library;
import java.util.Scanner;
public class Bookwork implements Customer,houesManager {
Scanner scanner=new Scanner(System.in);
Book book[]=new Book[30];
public void init(){
book[0]=new Book(10001,"哈利波特","罗琳","1997-3-2",20,39.9);
book[1]=new Book(10002,"鬼吹灯","天下","2008-7-1",34,27.8);
book[2]=new Book(10003,"宇宙奥秘","霍金","2003-12-23",30,78.0);
}
public Book searchbook(String name){
int i; Book book1=null;
for (i=0;i<book.length;i++){
if (book[i]!=null&& book[i].bookname.equals(name)){
book1=book[i];
break;
}else book1=null;
}
return book1;
}
public void showbookall(){
for (Book book1:book){
if (book1!=null) {
System.out.println("图书编码" + book1.id + "图书名称" + book1.bookname + "图书作者" + book1.author + "图书出版日期" + book1.pubtime + "图书数量" + book1.booknumber + "图书价格" + book1.price);
}
}
}
public int seachbookint() {
int i=0;
for (Book book1:book){
i++;
}
return i;
}
@Override
public void input() {
int i,j= 3;
System.out.println("请输入入库图书名称:");
String bookname=scanner.nextLine();
for (i=0;i<=j;i++){
if (book[i].bookname .equals(bookname)){
book[i].booknumber=+1;
break;
}
else {
try {
book[i].bookname.equals(bookname);
System.out.println("请输入入库图书作者:");
book[i].author=scanner.nextLine();
System.out.println("请输入入库图书出版日期:");
book[i].pubtime=scanner.nextLine();
System.out.println("请输入入库图书数量:");
book[i].booknumber =scanner.nextInt();
System.out.println("请输入入库图书价格:");
book[i].price =scanner.nextDouble ();
book[i].id=book[i-1].id+1;
break;
}catch (Exception e){
e.printStackTrace();
}
}
}
}
@Override
public void outbook() {
System.out.println("请输入入出库图书名称:");
String bookname=scanner.nextLine();
Book book= this.searchbook(bookname);
if (book==null){
System.out.println("图书不存在");
}
else{
System.out.println("请输入入出库图书数量");
book.booknumber=book.booknumber-scanner.nextInt();
}
}
@Override
public void addbook() {
System.out.println("请输入添加图书名称:");
String bookname=scanner.nextLine();
Book book1= this.searchbook(bookname);
int i=this.seachbookint();
if (book1 ==null){
try {
book[i].bookname=bookname;
System.out.println("请输入入库图书作者:");
book[i].author=scanner.nextLine();
System.out.println("请输入入库图书出版日期:");
book[i].pubtime=scanner.nextLine();
System.out.println("请输入入库图书数量:");
book[i].booknumber =scanner.nextInt();
System.out.println("请输入入库图书价格:");
book[i].price =scanner.nextDouble ();
book[i].id=book[i-1].id+1;
}catch (Exception e){
e.printStackTrace();
}
}else {
System.out.println("图书已存在无需添加");
}
}
@Override
public void reachbook() {
System.out.println("请输入查询图书名称:");
String bookname=scanner.nextLine();
Book book1= this.searchbook(bookname);
if (book1 ==null){
System.out.println("没有你要的书");
}
else {
book1.showbook(book1);
}
}
public double buybook(){
System.out.println("请输入购买图书名称:");
String bookname=scanner.nextLine();
Book book1= this.searchbook(bookname);
double b=book1.price;
System.out.println("请输入购买图书数量:");
int a=scanner.nextInt();
return a*b;
}
public Gift buygift1()
{ System.out.println("请输入想要的礼物:1.钢笔,2.CD,3.包装,4.不需要");
int a=scanner.nextInt();
Gift gift=null;
switch (a){
case 1:
gift=new Pen();
break;
case 2:
gift =new CD ();
break;
case 3:
gift=new Pack();
break;
case 4:
gift=new Nogift ();
break;
}
return gift;
}
@Override
public double cheakout() {
Bookwork bookwork=new Bookwork();
double a= bookwork.buybook();
Gift gift1=this.buygift1();
double b= gift1.giftprice();
return a+b;
}
}
TestLibrary类
package Library;
import Exception1.Exception1;
import java.util.Scanner;
public class TestLibrary {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Bookwork bookwork=new Bookwork();
bookwork.init();
int a = 0, j = 3;
while (a < j) {
System.out.println("请输入账号");
String name = scanner.nextLine();
System.out.println("请输入密码");
String pass = scanner.nextLine();
User user = new User();
user.userinit();
int i = user.login(name, pass);
if (i == 1) {
Manage manage = new Manage();
bookwork.showbookall();
System.out.println("请选择操作:1.查找图书,2.图书入库,3.添加书本,4.图书出库");
try {
int d=scanner.nextInt();
switch (d){
case 1:
System.out.println("请输入查找图书名字");
scanner.nextLine();
String name1=scanner.nextLine();
bookwork.showbookall();
Book book1= manage.searchbook(name1);
if (book1!=null){
book1.showbook(book1);
}else{
System.out.println("未找到图书信息");
}
break;
case 2:
manage.input();
bookwork.showbookall();
break;
case 3:
manage.addbook();
bookwork.showbookall();
break;
case 4:
manage.outbook();
bookwork.showbookall();
break;
default: throw new Exception();
}
}catch (Exception e){
e.printStackTrace();
System.out.println("请输入正确的数字");
}
break;
} else if (i == 2) {
monCustomer monCustomer = new monCustomer();
break;
}
else {
System.out.println("密码错误请重新输入");
a++;
System.out.println(i);
}
}
}
}
Manage类
package Library;
public class Manage extends User implements houesManager {
public Manage(){};
public Manage(String name ,String pass){
super(name,pass);
}
Bookwork bookwork=new Bookwork();
@Override
public void input() {
bookwork.input();
}
@Override
public void outbook() {
bookwork.outbook();
}
@Override
public void addbook() {
bookwork.addbook();
}
@Override
public Book searchbook(String bookname) {
Book book=bookwork.searchbook(bookname);
return book;
}
}
houseManage接口
package Library;
public interface houesManager {
void input();
void outbook();
void addbook();
Book searchbook(String bookname);
}
Book类
package Library;
public class Book {
int id;
String bookname;
String author;
String pubtime;
int booknumber;
double price;
int buynum;
String gift;
public Book(){}
public Book(int id,String bookname,String author,String pubtime,int booknumber,double price){
this.author=author;
this.bookname=bookname;
this.id=id;
this.pubtime=pubtime;
this.booknumber=booknumber;
this.price=price;
}
public void showbook(Book book1){
if (book1!=null){
System.out.println("图书编码"+book1.id+"图书名称"+book1.bookname+"图书作者"+book1.author+"图书出版日期"+book1.pubtime+"图书数量"+book1.booknumber+"图书价格"+book1.price);
}
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setAuthor(String author) {
this.author = author;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public double getPrice() {
return price;
}
public void setBooknumber(int booknumber) {
this.booknumber = booknumber;
}
public void setBuynum(int buynum) {
this.buynum = buynum;
}
public void setGift(String gift) {
this.gift = gift;
}
public void setPrice(double price) {
this.price = price;
}
public void setPubtime(String pubtime) {
this.pubtime = pubtime;
}
public int getBooknumber() {
return booknumber;
}
public int getBuynum() {
return buynum;
}
public String getAuthor() {
return author;
}
public String getBookname() {
return bookname;
}
public String getGift() {
return gift;
}
public String getPubtime() {
return pubtime;
}
public double getprice(double price,int buynum){
double p;
p=price*buynum;
return p;
}
}
User类
package Library;
public class Book {
int id;
String bookname;
String author;
String pubtime;
int booknumber;
double price;
int buynum;
String gift;
public Book(){}
public Book(int id,String bookname,String author,String pubtime,int booknumber,double price){
this.author=author;
this.bookname=bookname;
this.id=id;
this.pubtime=pubtime;
this.booknumber=booknumber;
this.price=price;
}
public void showbook(Book book1){
if (book1!=null){
System.out.println("图书编码"+book1.id+"图书名称"+book1.bookname+"图书作者"+book1.author+"图书出版日期"+book1.pubtime+"图书数量"+book1.booknumber+"图书价格"+book1.price);
}
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setAuthor(String author) {
this.author = author;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public double getPrice() {
return price;
}
public void setBooknumber(int booknumber) {
this.booknumber = booknumber;
}
public void setBuynum(int buynum) {
this.buynum = buynum;
}
public void setGift(String gift) {
this.gift = gift;
}
public void setPrice(double price) {
this.price = price;
}
public void setPubtime(String pubtime) {
this.pubtime = pubtime;
}
public int getBooknumber() {
return booknumber;
}
public int getBuynum() {
return buynum;
}
public String getAuthor() {
return author;
}
public String getBookname() {
return bookname;
}
public String getGift() {
return gift;
}
public String getPubtime() {
return pubtime;
}
public double getprice(double price,int buynum){
double p;
p=price*buynum;
return p;
}
}