自己参照某租车系统写的一个图书管理系统
代码如下:
BOOK抽象类
package com.BookSystem;
public abstract class Book {
int id;
String name;
int price;
int must_learn;
int select_learn;
public Book(int id,String name, int price,int must_learn, int select_learn){
this.id= id;
this.name=name;
this.price=price;
this.must_learn=must_learn;
this.select_learn=select_learn;
}
public abstract void pri();
}
story类
package com.BookSystem;
public class Story extends Book{
public Story(int id, String name, int price,int select_learn) {
super(id, name, price, 0, select_learn);
// TODO Auto-generated constructor stub
}
@Override
public void pri() {
// TODO Auto-generated method stub
System.out.println(id+"\t" +name +"\t" +price+"元\t" +select_learn);
}
}
profession类
package com.BookSystem;
public class Profession extends Book{
public Profession(int id, String name, int price,int must_learn) {
super(id, name, price, must_learn,0);
// TODO Auto-generated constructor stub
}
@Override
public void pri() {
// TODO Auto-generated method stub
System.out.println(id +"\t" +name +"\t" +price+"元\t"+must_learn);
}
}
book_Admin
package com.BookSystem;
import java.util.Scanner;
public class Book_Admin {
int sum_price=0;
int sum_must = 0;
int sum_select = 0;
StringBuilder pri_must = new StringBuilder();
StringBuilder pri_select = new StringBuilder();
Scanner in = new Scanner(System.in);
public Book[] book_message() {
Book[] books = {
new Story(1,"简爱",45, 1),
new Story(2,"悲惨世界",56,1),
new Story(3,"安娜",34,1),
new Profession(4,"计算机基础",43,1),
new Profession(5,"JAVA基础",65,1),
};
System.out.println("您可租借的书目及价格表");
System.out.println("ID 名称 价格 数量");
for(int i = 0; i<books.length; i++) {
books[i].pri();
}
return books;
}
public void book_admin_function(Book[] books) {
//客户选择书籍、数量
System.out.println("请输入需要借的数量");
int number = in.nextInt();
for (int i = 0; i<number;i++)
{
System.out.println("请输入"+(i+1)+"书籍序号");
int id = in.nextInt();
//用户输入从1开始 数组下标从0开始
sum_must +=books[id-1].must_learn;
sum_select+=books[id-1].select_learn;
sum_price+=books[id-1].price;
if (books[id-1].must_learn>0)
{
pri_must.append(books[id-1].name+"\t");
}
if (books[id-1].select_learn>0)
{
pri_select.append(books[id-1].name +"\t");
}
}
System.out.println("---------租书明细-----------");
System.out.println("专业课书籍:"+pri_must);
System.out.println("课外书籍:"+pri_select);
System.out.println("书籍数量:" +(sum_must+sum_select));
this.pri_bill(sum_price);
}
private void pri_bill(int sum_price2) {
// TODO Auto-generated method stub
System.out.println("请输入需要借的天数");
int num = in.nextInt();
System.out.println("-----租书总价格-----");
System.out.println("您的借书费用为:"+ num*sum_price2+"元");
}
}
TestSystem主函数
package com.BookSystem;
import java.util.Scanner;
public class TestSystem {
Scanner in = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
TestSystem testSystem = new TestSystem();
}
public TestSystem() {
System.out.println("欢迎使用图书管理系统:");
System.out.println("是否进入此系统:Y/N");
while(true) {
String enter;
enter = in.next();
enter = enter.toUpperCase();
if(enter.equals("Y")) {
Book_Admin book_admin = new Book_Admin();
Book[] book = book_admin.book_message();
book_admin.book_admin_function(book);
}
else
{
System.out.println("感谢您的使用:请输入'N'退出此系统");
String str ;
str = in.next();
str = str.toUpperCase();
if (str.equals("N")) {
break;
}
}
}
}
}