请你使用Java相关技术编写一个应用程序,实现图书的管理。要求:
1.自定义图书类(包括图书的状态变量、方法);
2.实现查找图书方法的重载,可以根据书名或者作者、以及书名和作者信息找到图书;
3.创建测试类,包含10本图书的对象(可使用);通过标准输入(键盘),调相应的查找方法,显示图书的详细信息(书名、作者、出版社、出版时间、价格等)。
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//创建了4个Library对象
System.out.println("请选择查找方法:\n" + "1.书名查找 2.作者名查找 3.书名作者名查找");
Scanner mc = new Scanner(System.in);
int a = mc.nextInt();
if (a == 1) { //if(b.equals("jav"))意思是若b中的字符串与括号内字符串相等
System.out.println("请输入书名:");
String b = mc.next();
if (b.equals("jav")) {
Out(0);
} else if (b.equals("qjj")) {
Out(1);
} else if (b.equals("spl")) {
Out(2);
} else if (b.equals("python")) {
Out(3);
} else System.out.println("查找失败");
} else if (a == 2) {
System.out.println("请输入作者名:");
String b = mc.next();
if (b.equals("赵老师")) {
Out(0);
} else if (b.equals("钱老师")) {
Out(1);
} else if (b.equals("孙老师")) {
Out(2);
} else if (b.equals("李老师")) {
Out(3);
} else System.out.println("查找失败");
} else if (a == 3) {
System.out.println("请输入书名:");
String b = mc.next();
System.out.println("请输入作者名:");
String c = mc.next();
if (b.equals("jav") || c.equals("赵老师")) {
Out(0);
} else if (b.equals("qjj") || c.equals("钱老师")) {
Out(1);
} else if (b.equals("spl") || c.equals("孙老师")) {
Out(2);
} else if (b.equals("python") || c.equals("李老师")) {
Out(3);
} else System.out.println("查找失败");
}
}
static void Out(int a) {
Library t1 = new Library("jav", "赵老师", "晋江");
Library t2 = new Library("qjj", "钱老师", "番茄");
Library t3 = new Library("spl", "孙老师", "废文");
Library t4 = new Library("python", "李老师", "海棠");
//创建集合用以保存Library对象,泛型限制为Library
ArrayList<Library> books = new ArrayList<>();
//在集合末尾添加对象
books.add(t1);
books.add(t2);
books.add(t3);
books.add(t4);
Library temp = books.get(a);
System.out.println("书名:" + temp.getName() + " 作者:" + temp.getzzanme() + " 出版社:" + temp.getpress());
}
}
class Library {
String name;
String zzname;
String press;
public String getName() {
return name;
}
public String getzzanme() {
return zzname;
}
public String getpress() {
return press;
}
public Library(String name, String zzname, String press) {
this.name = name;
this.zzname = zzname;
this.press = press;
}
}
最后若要输出所有Library对象
//遍历集合
for (int i = 0; i < teachers.size(); i++) {
//新对象临时保存一下
Teacher temp = teachers.get(i);
System.out.println("书名:"+temp.getName()+" 作者:"+temp.getzzanme()+" 出版社:"+temp.getpress());
}