1.代码原理
①JAVA对象数组的构建
②类的调用
③方法的重载(OVERLODING)
④构造方法的使用
2.源代码
①bookFeature
此类是每本书的信息和操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
class bookFeature { private int ID; private String name; private double price; private String author; public bookFeature(){ ID=0; name=null; author=null; price=0; } public void setID(int ID){ this.ID=ID; } public void setName(String name){ this.name=name; } public void setPrice(double price){ this.price=(price>0)?price:0; } public void setAuthor(String author){ this.author=author; } public int getID(){ return ID; } public String getName(){ return name; } public double getPrice(){ return price; } public String getAuthor(){ return author; } public void clearBook(){ this.author=null; this.name=null; this.price=0; this.ID=0; } } |
②bookList
此类是书的列表对象及方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import java.util.*; class bookList { private int bookNum=0; final int MAXSIZE=1001; bookFeature[] List=new bookFeature[MAXSIZE]; public bookList(){ bookNum=0; for(int i=0;i<MAXSIZE;i++){ List[i]=new bookFeature(); } } public void moreBook() { bookNum++; bookFeature book=new bookFeature(); String tempName,tempAuthor; double tempPrice; book.setID(bookNum); Scanner input=new Scanner(System.in); System.out.print("Please input this book's name: "); tempName=input.next(); book.setName(tempName); System.out.print("Please input this book's author: "); tempAuthor=input.next(); book.setAuthor(tempAuthor); System.out.print("Please input this book's price: "); tempPrice=input.nextDouble(); book.setPrice(tempPrice); List[bookNum]=book; System.out.println("Input Succeed!!!"); } public void delBook(int index){ for(int i=index;i<=bookNum;i++){ List[i]=List[i+1]; } List[bookNum].clearBook(); bookNum--; } public void delBook(String name){ int index=0; for(int i=1;i<=bookNum;i++){ if(List[i].getName().equals(name)){ index=i;break; } } for(int i=index;i<=bookNum;i++){ List[i]=List[i+1]; } List[bookNum].clearBook(); bookNum--; } public void getInfo(){ System.out.println("Book Num: "+bookNum); System.out.println("Here is the list!"); System.out.println("********************************"); System.out.println("ID Title Author Price "); for(int i=1;i<=bookNum;i++){ System.out.println(i+" "+List[i].getName()+" "+List[i].getAuthor()+" "+List[i].getPrice()); } System.out.println("********************************"); } } |
③public class main
主程序调用类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.*; public class test { public static void main(String[] args){ bookList my=new bookList(); Scanner input=new Scanner(System.in); System.out.println("Input the size of all book:"); int n=input.nextInt(); for(int i=1;i<=n;i++){ System.out.println(i+" "); my.moreBook(); } my.getInfo(); System.out.println("---------------Delete Books----------------"); System.out.println("Please input ID of the book you wanna delete: "); Scanner input2=new Scanner(System.in); int index=input2.nextInt(); my.delBook(index); my.getInfo(); System.out.println("Please input name of the book you wanna delete: "); Scanner input3=new Scanner(System.in); String name=input3.next(); my.delBook(name); my.getInfo(); } } |
3.实验结果
基于eclipse
书信息的输入
删除指定的书,xxx.delBook(int ID)和xxx.delBook(String name)函数的重载