book1是Book,EBook,Magazine的超类,他是一个抽象的类,继承后,子类(subclass)需要重写book1的抽象方法,
下面是book1的定义:
package test5;
abstract public class book1 {
public String Name; //定义书名
public String Author;//定义作者
public String Publishing_house;//出版社
public double Price;//价格
public String Id;//编号
public book1() {
super();
System.out.println("调用了book1的无参构造方法");
}
abstract public void setName(String name);
abstract public String getName();
public void setAuthor(String author) {
Author = author;
}
public String getAuthor() {
return Author;
}
public String getPublishing_house() {
return Publishing_house;
}
public void setPublishing_house(String publishing_house) {
Publishing_house = publishing_house;
}
public double getPrice() {
return Price;
}
public void setPrice(double price) {
Price = price;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
@Override
public String toString() {
return "book1 [Name=" + Name + ", Author=" + Author + ", Publishing_house=" + Publishing_house + ", Price="
+ Price + ", Id=" + Id + "]";
}
}
EBook:重写book的抽象方法
package test5;
public class EBook extends book1{
private static int downloadTimes=0;
public void setName(String Ename) {//改写抽象方法
Name = Ename;
}
public String getName() {
return Name;
}
public EBook() {
System.out.println("调用了EBook的无参构造方法");
}
public static int getDownloadTimes() {
return downloadTimes;
}
}
Magazine类:
package test5;
public class Magazine extends book1{
public Magazine() {
System.out.println("调用了Magazine无参构造方法");
}
// public Magazine(boolean isBorrowed) {
// super();
// System.out.println("调用了Magiazine的有参构造方法");
// this.isBorrowed = isBorrowed;
// }
// private boolean isBorrowed=false;
public void setName(String name) {
Name = name;
}
public String getName() {
return Name;
}
}
MainView:
package test5;
import java.util.ArrayList;
import java.util.Scanner;
public class MainView {
public ArrayList Book_List = new ArrayList<>();
public ArrayList EBook_List = new ArrayList<>();
public ArrayList Magazine_List = new ArrayList<>();
Scanner input = new Scanner(System.in);
public void start() {
//Book boo1 = new
Book book0 = new Book("西游记","吴承恩","人民教育出版社",22.5,"AN1001");
Book_List.add(book0);
Book book1 = new Book("三体","刘慈欣","机械工业出版社",50.0,"AN1002");
Book_List.add(book1);
Book book2 = new Book("资治通鉴","司马光","清华历史出版社",30.8,"AN1003");
Book_List.add(book2);
Book book3 = new Book("java编程","宋鹏飞","北京联合大学出版社",12.5,"AN1004");
Book_List.add(book3);
Book book4 = new Book("Android开发","杨青","高等教育出版社",200.0,"An1005");
Book_List.add(book4);
}
public void mainview() {
// TODO Auto-generated method stub
do {
System.out.println();
System.out.println("--------Oracle图书管理系统---------");
System.out.println("请选择");
System.out.println("1 add books");
System.out.println("2 remove books");
System.out.println("3 search books");
System.out.println("4 list all books");
System.out.println("5 exit");
int key = input.nextInt();
switch (key) {
case 1:
addBooks();
mainview();
break;
case 2:
removeBooks();
mainview();
case 3:
System.out.println("请选择序号:\n1 按照书名\t 2 按照作者");
switch (input.nextInt()) {
case 1:
searchBooksByName();
break;
case 2:
searchBooksByAuthor();
break;
default:
System.out.println("不好意思,输入有误");
break;
}
mainview();
case 4:
showAll_Book();
showAll_EBook();
showAll_Magazine();
mainview();
case 5:
System.out.println("查看电子书数量");
System.out.println("電子書庫存為"+EBook.getDownloadTimes());
case 6:
System.exit(0);
break;
default:
break;
}
} while (input.nextInt()!=5);
}
public void addBooks() {
System.out.println("请选择添加类型:");
System.out.println("1,Book\t2,EBook\t3,Maginze");
int key = input.nextInt();
if (key==1) {
Book book = new Book();
System.out.println("请输入书名");
book.setName(input.next());
System.out.println("请输入作者");
book.setAuthor(input.next());
System.out.println("请输入出版社名称");
book.setPublishing_house(input.next());
System.out.println("请输入价格");
book.setPrice(input.nextDouble());
System.out.println("请输入编号");
book.setId(input.next());
Book_List.add(book);
System.out.println("添加成功");
System.out.println("添加的信息如下");
showOne_Book(Book_List.size()-1);
}
else if (key==2) {
EBook eBook = new EBook();
System.out.println("请输入电子书名");
eBook.setName(input.next());
System.out.println("请输入电子书作者");
eBook.setAuthor(input.next());
System.out.println("请输入出版社");
eBook.setPublishing_house(input.next());
System.out.println("请输入价格");
eBook.setPrice(input.nextDouble());
System.out.println("请输入编号");
eBook.setId(input.next());
EBook_List.add(eBook);
System.out.println("添加成功");
System.out.println("添加的信息如下");
showOne_EBook(EBook_List.size()-1);
}
else if(key==3) {
Magazine maginze = new Magazine();
System.out.println("请输入杂志名");
maginze.setName(input.next());
System.out.println("请输入作者");
maginze.setAuthor(input.next());
System.out.println("请输入出版社");
maginze.setPublishing_house(input.next());
System.out.println("请输入价格");
maginze.setPrice(input.nextDouble());
System.out.println("请输入编号");
maginze.setId(input.next());
Magazine_List.add(maginze);
System.out.println("添加成功");
System.out.println("添加的信息如下");
showOne_Magazine(Magazine_List.size()-1);
}
}
public void removeBooks() {
System.out.println("请选择删除类型");
System.out.println("1,Book\t2,EBook\t3,Maginze");
int k = input.nextInt();
if (k==1) {
System.out.println("请选择需要移除的书籍序号:");
for (int i = 0; i < Book_List.size(); i++) {
showOne_Book(i);
}
int in=input.nextInt();
if (in>=0&&in<=Book_List.size()) {
Book_List.remove(in-1);
System.out.println("移除成功");
}
else System.out.println("您无权操作");
}
else if (k==2) {
System.out.println("请选择需要移除的电子书序号:");
for (int i = 0; i < EBook_List.size(); i++) {
showOne_EBook(i);
}
int in = input.nextInt();
if (in>0&&in=0&&in
由于本人也是菜鸟一枚,对于mainview部分代码的书写还有待改进,日后慢慢改,有的地方代码有冗余,写博客也是学习,为自己日后有时间也可以试着看看
main方法:
package test5;
public class Ma {
public static void main(String[] args) {
// TODO Auto-generated method stub
MainView mView = new MainView();
mView.start();
mView.mainview();
}
}
代码运行系统截图:3功能里面的按照人名和作者搜索只能搜索Book类型里的,其他两个还没有加入,先放在这里。