借书系统,首先创建了一个父类books:
public class books {
int sequence;//属性,序列号
String name;//属性,名称
public books(int sequence, String name) {
// TODO Auto-generated constructor stub
this.name = name;//赋值,以便后面主函数调用
this.sequence=sequence;
}
}
创建子类SerialNumberQuery:
import java.util.Scanner;
public class SerialNumberQuery {
public void SerialNumberQuery(int sequence,String name,books[] bb){
SerialNumberQuery tet=new SerialNumberQuery();
Scanner Qz=new Scanner(System.in);
try {
System.out.println("请输入要查找的书籍序列号:");
int c=Qz.nextInt();
int x=bb[c-1].sequence;
if(c==x){
System.out.println("您借阅的书籍是:"+bb[c-1].name+"\t欢迎再次借阅!");
}} catch (Exception e){
System.out.println("您输入有误或者不存在,请重新输入!");
tet.SerialNumberQuery(sequence, name, bb);
}
}
}
创建一个子类NameQuery:
import java.util.InputMismatchException;
import java.util.Scanner;
public class NameQuery {
public void NameQuery(int sequence,String name,books[] bb){
NameQuery tet1=new NameQuery();
Scanner Qz=new Scanner(System.in);
try {
System.out.println("请输入要查找的书籍的书名:");
String c=Qz.next();
int i;
for( i=0;i<bb.length;i++){
//System.out.println(bb[i]);
if(c.equals(bb[i].name)){
break;}
}
System.out.println("您借阅的书籍是:"+bb[i].name+"\t欢迎再次借阅!");
} catch (Exception e){
System.out.println("您输入有误或者不存在,请重新输入!");
tet1.NameQuery(sequence, name, bb);
}
}
}
主函数function:
import java.util.Scanner;
public class function {
private static int sequence;
private static String name;
static books[] bb={new books(1,"高数"),
new books(2,"论语"),
new books(3,"英语")
};
public static void main(String[] args) {
Scanner Qz=new Scanner(System.in);
// TODO Auto-generated method stub
SerialNumberQuery tet=new SerialNumberQuery();
NameQuery tet1=new NameQuery();
System.out.println("*****请问您是否需要查阅书籍?*****");
System.out.println("是请输入1\t否请输入0");
int a=Qz.nextInt();
if(a==1){
System.out.println("按序列查找请按1:");
System.out.println("按书名查找请按0:");
int b=Qz.nextInt();
switch (b) {
case 1:
tet.SerialNumberQuery(sequence, name, bb);
break;
case 0:
tet1.NameQuery(sequence, name, bb);
break;
}
System.out.println("欢迎下次再来!!!");
}
else{
System.out.println("欢迎下次再来!!!");
}
}
}