幕后角色的博客

JAVA入门基础知识

JAVA项目之图书管理系统

Bookstore父类——抽象类

package com.zjm.www.bookex;

/**
 * 图书库
 * @author Zjm
 *
 */
import java.util.ArrayList;

public abstract class Bookstore {
    //public static ArrayList<Bookstore> list = new ArrayList();
    private int id;
    private String bookName;
    private String author;// 作者
    private String press;// 出版社
    private double pay;// 价格

//  public Bookstore() {
//  }
//
//  public Bookstore(int id, String bookName, String author, String press, double pay) {
//      super();
//      this.id = id;
//      this.bookName = bookName;
//      this.author = author;
//      this.press = press;
//      this.pay = pay;
//  }
    public abstract void addBook();
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getpress() {
        return press;
    }

    public void setPress(String press) {
        this.press = press;
    }

    public double getPay() {
        return pay;
    }

    public void setPay(double pay) {
        this.pay = pay;
    }

}

Books类——继承Bookstore抽象类,(纸质书类)

package com.zjm.www.bookex;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 质纸质书
 * @author Zjm
 *
 */
public class Books extends Bookstore{
    static ArrayList<Books> blist = new ArrayList();
    private int page;//页码
    public Books() {}
    public Books(int id, String bookName, String author, String press, double pay,int page) {
        setId(id);
        setBookName(bookName);
        setAuthor(author);
        setPress(press);
        setPay(pay);
        this.page = page;
    }
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    @Override
    public void addBook() {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        int idid = 0;
        System.out.println("请输入书的编号");
        int id = scan.nextInt();
        System.out.println("请输入书名: ");
        scan.nextLine();
        String bookName = scan.nextLine();
        System.out.println("请输入作者名: ");
        String author = scan.nextLine();
        System.out.println("请输入出版社名字: ");
        String press = scan.nextLine();
        System.out.println("请输入售价: ");
        double pay = scan.nextDouble();
        System.out.println("请输入页码");
        int page = scan.nextInt();
        for (int j = 0; j < Books.blist.size(); j++) {
            if (Books.blist.get(j).getId() == id) {
                idid = 1;
            }
        }
        for (int j = 0; j < EBooks.elist.size(); j++) {
            if (EBooks.elist.get(j).getId() == id) {
                idid = 1;
            }
        }
        for (int j = 0; j < Magazine.mlist.size(); j++) {
            if (Magazine.mlist.get(j).getId() == id) {
                idid = 1;
            }
        }
        if (idid == 1) {
            System.out.println("输入的图书编号已存在,图书编号不可重复");
        } else {
            System.out.println("添加成功!");
            System.out.print("您添加的信息为:  ");
            Books.blist.add(new Books(id, bookName, author, press, pay, page));
            System.out.println("序列号:" + Books.blist.size() + " 书名:" + bookName + " 作者:" + author + " 出版社:" + press
                    + " 售价" + pay + "元" + " 编号:" + id + " 页码:" + page);
        }
    }


}

EBooks类——继承Bookstore类(电子书类)

package com.zjm.www.bookex;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 电子书
 * @author Zjm
 *
 */
public class EBooks extends Bookstore{
    static ArrayList<EBooks> elist = new ArrayList();
    private int downloadTimes;
    public EBooks() {}
    public EBooks(int id, String bookName, String author, String press, double pay,int downloadTimes) {
        setId(id);
        setBookName(bookName);
        setAuthor(author);
        setPress(press);
        setPay(pay);
        this.downloadTimes = downloadTimes;
    }
    public int getDownloadTimes() {
        return downloadTimes;
    }
    public void setDownloadTimes(int downloadTimes) {
        this.downloadTimes = downloadTimes;
    }
    @Override
    public void addBook() {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        int idid = 0;
        System.out.println("请输入书的编号");
        int id = scan.nextInt();
        System.out.println("请输入书名: ");
        scan.nextLine();
        String bookName = scan.nextLine();
        System.out.println("请输入作者名: ");
        String author = scan.nextLine();
        System.out.println("请输入出版社名字: ");
        String press = scan.nextLine();
        System.out.println("请输入售价: ");
        double pay = scan.nextDouble();
        // System.out.println("请输入下载量");
        // int downloadTimes = scan.nextInt();
        int downloadTimes = 0;
        for (int j = 0; j < Books.blist.size(); j++) {
            if (Books.blist.get(j).getId() == id) {
                idid = 1;
            }
        }
        for (int j = 0; j < EBooks.elist.size(); j++) {
            if (EBooks.elist.get(j).getId() == id) {
                idid = 1;
            }
        }
        for (int j = 0; j < Magazine.mlist.size(); j++) {
            if (Magazine.mlist.get(j).getId() == id) {
                idid = 1;
            }
        }
        if (idid == 1) {
            System.out.println("输入的图书编号已存在,图书编号不可重复");
        } else {
            System.out.println("添加成功!");
            System.out.print("您添加的信息为:  ");
            EBooks.elist.add(new EBooks(id, bookName, author, press, pay, downloadTimes));
            System.out.println("序列号:" + (Books.blist.size() + EBooks.elist.size()) + " 书名:" + bookName + " 作者:" + author
                    + " 出版社:" + press + " 售价" + pay + "元" + " 编号:" + id + " 下载量:" + downloadTimes);
        }
    }


}

Magazine类——继承Bookstore类(杂志类)

package com.zjm.www.bookex;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 杂志
 * @author Zjm
 *
 */
public class Magazine extends Bookstore{
    static ArrayList<Magazine> mlist = new ArrayList();
    private String issue;
    public Magazine() {};
    public Magazine(int id, String bookName, String author, String press, double pay,String issue) {
        setId(id);
        setBookName(bookName);
        setAuthor(author);
        setPress(press);
        setPay(pay);
        this.issue = issue;
    }
    public String getIssue() {
        return issue;
    }
    public void setIssue(String issue) {
        this.issue = issue;
    }
    @Override
    public void addBook() {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        int idid = 0;
        System.out.println("请输入书的编号");
        int id = scan.nextInt();
        System.out.println("请输入书名: ");
        scan.nextLine();
        String bookName = scan.nextLine();
        System.out.println("请输入作者名: ");
        String author = scan.nextLine();
        System.out.println("请输入出版社名字: ");
        String press = scan.nextLine();
        System.out.println("请输入售价: ");
        double pay = scan.nextDouble();
        System.out.println("请输入期号");
        String issue = scan.next();
        for (int j = 0; j < Books.blist.size(); j++) {
            if (Books.blist.get(j).getId() == id) {
                idid = 1;
            }
        }
        for (int j = 0; j < EBooks.elist.size(); j++) {
            if (EBooks.elist.get(j).getId() == id) {
                idid = 1;
            }
        }
        for (int j = 0; j < Magazine.mlist.size(); j++) {
            if (Magazine.mlist.get(j).getId() == id) {
                idid = 1;
            }
        }
        if (idid == 1) {
            System.out.println("输入的图书编号已存在,图书编号不可重复");
        } else {
            System.out.println("添加成功!");
            System.out.print("您添加的信息为:  ");
            Magazine.mlist.add(new Magazine(id, bookName, author, press, pay, issue));
            System.out.println("序列号:" + (Books.blist.size() + EBooks.elist.size() + Magazine.mlist.size()) + " 书名:"
                    + bookName + " 作者:" + author + " 出版社:" + press + " 售价" + pay + "元" + " 编号:" + id + " 期号:" + issue);
        }
    }

}

User类——主类(主要方法,核心在这个类)

package com.zjm.www.bookex;

/**
 * 用户界面
 * 
 * @author Zjm
 *
 */
import java.util.Scanner;

public class User {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Books b11 = new Books(1, "西游记", "吴承恩", "四川人民出版社", 89.3, 899);
        Books b12 = new Books(4, "三国演义", "罗贯中", "凤凰传媒出版社", 99, 1036);
        Books b13 = new Books(10, "西水浒传", "西罗贯中", "江苏无锡出版社", 37.1, 861);
        EBooks b21 = new EBooks(3, "斗罗大陆", "杨建国", "软件174", 9, 1387);
        EBooks b22 = new EBooks(2, "校花的贴身高手", "唐三藏", "江苏信息", 23.1, 903);
        EBooks b23 = new EBooks(5, "西方的我", "孙悟空", "东方出版社", 8, 56);
        Magazine b31 = new Magazine(9, "装男人", "施耐庵", "江苏出版社", 1.3, "五月十五日");
        Magazine b32 = new Magazine(16, "无锡市民", "小明", "中华人民出版社", 2.6, "五月十日");
        Books.blist.add(b11);
        Books.blist.add(b12);
        Books.blist.add(b13);
        EBooks.elist.add(b21);
        EBooks.elist.add(b22);
        EBooks.elist.add(b23);
        Magazine.mlist.add(b31);
        Magazine.mlist.add(b32);
        new Operation();
    }

}

class Operation {
    Scanner scan = new Scanner(System.in);
    static int aa = 0;
    int a;

    public Operation() {
        do {
            System.out.println("1.存入图书\n2.查找图书\n3.删除图书\n4.查看所有图书\n5.退出\n请输入选项,按回车键确认");
            a = scan.nextInt();
            System.out.println("您选择的功能是: " + a);
            if (a == 1) {
                a1();
            } else if (a == 2) {
                a2();
            } else if (a == 3) {
                a3();
            } else if (a == 4) {
                a4();
            } else if (a == 5) {
                System.out.println("已选择退出");
            } else {
                System.out.println("请选择正确的选项");
            }
        } while (a != 5);
        System.out.println("谢谢使用");
    }

    private static void a1() {
        Scanner scan = new Scanner(System.in);
        System.out.println("1.添加纸质书,2.添加电子书,3.添加杂志");
        int b = scan.nextInt();
        if (b == 1) {
            Books books = new Books();
            books.addBook();
        } else if (b == 2) {
            EBooks ebooks = new EBooks();
            ebooks.addBook();
        } else if (b == 3) {
            Magazine magazine = new Magazine();
            magazine.addBook();
        }
    }
    private static void a2() {
        int aa = 0;
        String pd;
        int aaa = 100;
        Scanner scan = new Scanner(System.in);
        System.out.println("1 按照书名查询,2 按编号查找");
        int c = scan.nextInt();
        if (c == 1) {
            System.out.println("请输入书名关键字");
            String name = scan.next();
            // 纸质书的查找 关键字查找
            for (int i = 0; i < Books.blist.size(); i++) {
                if (Books.blist.get(i).getBookName().indexOf(name) >= 0) {
                    System.out.println("纸质书:  "+"序列号:" + (i + 1) + " 书名:" + Books.blist.get(i).getBookName() + " 作者:"
                            + Books.blist.get(i).getAuthor() + " 出版社:" + Books.blist.get(i).getpress() + " 售价:"
                            + Books.blist.get(i).getPay() + "元" + " 编号:" + Books.blist.get(i).getId() + " 页码: "
                            + Books.blist.get(i).getPage());
                    aa = 1;
                } else {}
            }
            //电子书的查找  关键字查找
            for (int i = 0; i < EBooks.elist.size(); i++) {
                if (EBooks.elist.get(i).getBookName().indexOf(name) >= 0) {
                    System.out.println("电子书:  "+"序列号:" + (i + 1 + Books.blist.size()) + " 书名:" + EBooks.elist.get(i).getBookName()
                            + " 作者:" + EBooks.elist.get(i).getAuthor() + " 出版社:" + EBooks.elist.get(i).getpress() + " 售价:"
                            + EBooks.elist.get(i).getPay() + "元" + " 编号:" + EBooks.elist.get(i).getId() + " 下载量: "
                            + EBooks.elist.get(i).getDownloadTimes());
                    aa = 1;
                } else {}
            }
            //杂志的查找  关键字查找
            for (int i = 0; i < Magazine.mlist.size(); i++) {
                if (Magazine.mlist.get(i).getBookName().indexOf(name) >= 0) {
                    System.out.println("序列号:" + (i + 1 + Books.blist.size() + EBooks.elist.size()) + " 书名:"
                            + Magazine.mlist.get(i).getBookName() + " 作者:" + Magazine.mlist.get(i).getAuthor() + " 出版社:"
                            + Magazine.mlist.get(i).getpress() + " 售价:" + Magazine.mlist.get(i).getPay() + "元" + " 编号:"
                            + Magazine.mlist.get(i).getId() + " 第: " + Magazine.mlist.get(i).getIssue() + "期");
                    aa = 1;
                } else {}
            }
            if (aa == 1) {
                System.out.println("请输入需要借阅的书的序列号,0退出");
                int ff = scan.nextInt();
                if (ff > 0 && ff <= Books.blist.size()) {
                    Books.blist.remove(ff - 1);
                    System.out.println("已借阅");
                } else if (ff > Books.blist.size() && ff <= Books.blist.size() + EBooks.elist.size()) {
                    EBooks.elist.remove(ff - Books.blist.size() - 1);
                    System.out.println("已借阅");
                } else if (ff > (Books.blist.size() + EBooks.elist.size())&& ff <= (Books.blist.size() + EBooks.elist.size() + Magazine.mlist.size())) {
                    Magazine.mlist.remove(ff - Books.blist.size() - EBooks.elist.size() - 1);
                    System.out.println("已借阅");
                }else if(ff==0) {
                    System.out.println("已退出");
                }else {
                    System.out.println("输入的序列号为空,已自动返回上级菜单");
                }
            } else {
                System.out.println("没有找到这本书");
            }
        } else if (c == 2) {//按照编号查找图书
            aa = 0;
            pd="";
            System.out.println("请输入图书编号");
            int id1 = scan.nextInt();
            //按照编号在纸质书查
            for (int i = 0; i < Books.blist.size(); i++) {
                if (Books.blist.get(i).getId() == id1) {
                    System.out.println("纸质书:  "+"序列号:" + (i + 1) + " 书名:" + Books.blist.get(i).getBookName() + " 作者:"
                            + Books.blist.get(i).getAuthor() + " 出版社:" + Books.blist.get(i).getpress() + " 售价:"
                            + Books.blist.get(i).getPay() + "元" + " 编号:" + Books.blist.get(i).getId() + " 页码: "
                            + Books.blist.get(i).getPage());
                    aa = 1;
                    aaa = i;
                    pd = "纸质书";
                } else {}
            }
            //按照编号在电子书里查
            for (int i = 0; i < EBooks.elist.size(); i++) {
                if (EBooks.elist.get(i).getId() == id1) {
                    System.out.println("电子书:  "+"序列号:" + (i + 1 + Books.blist.size()) + " 书名:" + EBooks.elist.get(i).getBookName()
                            + " 作者:" + EBooks.elist.get(i).getAuthor() + " 出版社:" + EBooks.elist.get(i).getpress() + " 售价:"
                            + EBooks.elist.get(i).getPay() + "元" + " 编号:" + EBooks.elist.get(i).getId() + " 下载量: "
                            + EBooks.elist.get(i).getDownloadTimes());
                    aa = 1;
                    aaa = i;
                    pd = "电子书";
                } else {}
            }
            //按照编号在杂志里查
            for (int i = 0; i < Magazine.mlist.size(); i++) {
                if (Magazine.mlist.get(i).getId() == id1) {
                    System.out.println("杂志:  "+"序列号:" + (i + 1 + Books.blist.size() + EBooks.elist.size()) + " 书名:"
                            + Magazine.mlist.get(i).getBookName() + " 作者:" + Magazine.mlist.get(i).getAuthor() + " 出版社:"
                            + Magazine.mlist.get(i).getpress() + " 售价:" + Magazine.mlist.get(i).getPay() + "元" + " 编号:"
                            + Magazine.mlist.get(i).getId() + " 第: " + Magazine.mlist.get(i).getIssue() + "期");
                    aa = 1;
                    aaa = i;
                    pd = "杂志";
                } else {}
            }
            if (aa == 1) {
                System.out.println("是否借阅:1.是,2.否");
                int bb = scan.nextInt();
                if (bb == 1) {
                    if(pd == "纸质书") {
                        Books.blist.remove(aaa);
                    }else if(pd == "电子书") {
                        EBooks.elist.remove(aaa);
                    }else if(pd == "杂志") {
                        Magazine.mlist.remove(aaa);
                    }
                    System.out.println("已借阅");
                } else {}
            } else {
                System.out.println("没有找到这本书");
            }
        }
    }

    private static void a3() {
        int aa = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入要删除的图书编号");
        int id1 = scan.nextInt();
        // 查找纸质书
        for (int i = 0; i < Books.blist.size(); i++) {
            if (Books.blist.get(i).getId() == id1) {
                System.out.println("纸质书:");
                System.out.println("序列号:" + (i + 1) + " 书名:" + Books.blist.get(i).getBookName() + " 作者:"
                        + Books.blist.get(i).getAuthor() + " 出版社:" + Books.blist.get(i).getpress() + " 售价:"
                        + Books.blist.get(i).getPay() + "元" + " 编号:" + Books.blist.get(i).getId() + " 页码: "
                        + Books.blist.get(i).getPage());
                aa = 1;
                Books.blist.remove(i);
            } else {
            }
        }
        // 查找电子书
        for (int i = 0; i < EBooks.elist.size(); i++) {
            if (EBooks.elist.get(i).getId() == id1) {
                System.out.println("电子书:");
                System.out.println("序列号:" + (i + 1 + Books.blist.size()) + " 书名:" + EBooks.elist.get(i).getBookName()
                        + " 作者:" + EBooks.elist.get(i).getAuthor() + " 出版社:" + EBooks.elist.get(i).getpress() + " 售价:"
                        + EBooks.elist.get(i).getPay() + "元" + " 编号:" + EBooks.elist.get(i).getId() + " 下载量: "
                        + EBooks.elist.get(i).getDownloadTimes());
                aa = 1;
                EBooks.elist.remove(i);
            } else {
            }
        }
        // 查找杂志
        for (int i = 0; i < Magazine.mlist.size(); i++) {
            if (Magazine.mlist.get(i).getId() == id1) {
                System.out.println("杂志:");
                System.out.println("序列号:" + (i + 1 + Books.blist.size() + EBooks.elist.size()) + " 书名:"
                        + Magazine.mlist.get(i).getBookName() + " 作者:" + Magazine.mlist.get(i).getAuthor() + " 出版社:"
                        + Magazine.mlist.get(i).getpress() + " 售价:" + Magazine.mlist.get(i).getPay() + "元" + " 编号:"
                        + Magazine.mlist.get(i).getId() + " 第: " + Magazine.mlist.get(i).getIssue() + "期");
                aa = 1;
                Magazine.mlist.remove(i);
            } else {
            }
        }

        if (aa == 1) {
            System.out.println("已删除");
        } else {
            System.out.println("没有找到该书");
        }
    }

    private static void a4() {
        // for (Bookstore book : Bookstore.list) {
        // System.out.println("序列号: "+ book.getXu() +"书名: "+book.getBookName()+"
        // 作者:"+book.getAuthor()
        // +" 出版社: "+ book.getpress()+" 售价: "+book.getPay()+"元"+" 编号:"+book.getId());
        // }
        Scanner scan = new Scanner(System.in);
        System.out.println("纸质书:");
        for (int i = 0; i < Books.blist.size(); i++) {
            System.out.println("序列号:" + (i + 1) + " 书名:" + Books.blist.get(i).getBookName() + " 作者:"
                    + Books.blist.get(i).getAuthor() + " 出版社:" + Books.blist.get(i).getpress() + " 售价:"
                    + Books.blist.get(i).getPay() + "元" + " 编号:" + Books.blist.get(i).getId() + " 页码: "
                    + Books.blist.get(i).getPage());
        }
        System.out.println("电子书:");
        for (int i = 0; i < EBooks.elist.size(); i++) {
            System.out.println("序列号:" + (i + 1 + Books.blist.size()) + " 书名:" + EBooks.elist.get(i).getBookName()
                    + " 作者:" + EBooks.elist.get(i).getAuthor() + " 出版社:" + EBooks.elist.get(i).getpress() + " 售价:"
                    + EBooks.elist.get(i).getPay() + "元" + " 编号:" + EBooks.elist.get(i).getId() + " 下载量: "
                    + EBooks.elist.get(i).getDownloadTimes());
        }
        System.out.println("杂志:");
        for (int i = 0; i < Magazine.mlist.size(); i++) {
            System.out.println("序列号:" + (i + 1 + Books.blist.size() + EBooks.elist.size()) + " 书名:"
                    + Magazine.mlist.get(i).getBookName() + " 作者:" + Magazine.mlist.get(i).getAuthor() + " 出版社:"
                    + Magazine.mlist.get(i).getpress() + " 售价:" + Magazine.mlist.get(i).getPay() + "元" + " 编号:"
                    + Magazine.mlist.get(i).getId() + " 第: " + Magazine.mlist.get(i).getIssue() + "期");
        }
        System.out.println("是否需要借阅1.是,其他任意数字键返回");
        int f = scan.nextInt();
        if (f == 1) {
            System.out.println("请输入需要借阅的书的序列号");
            int ff = scan.nextInt();
            if (ff > 0 && ff <= Books.blist.size()) {
                Books.blist.remove(ff - 1);
                System.out.println("已借阅");
            } else if (ff > Books.blist.size() && ff <= Books.blist.size() + EBooks.elist.size()) {
                EBooks.elist.remove(ff - Books.blist.size() - 1);
                System.out.println("已借阅");
            } else if (ff > (Books.blist.size() + EBooks.elist.size())
                    && ff <= (Books.blist.size() + EBooks.elist.size() + Magazine.mlist.size())) {
                Magazine.mlist.remove(ff - Books.blist.size() - EBooks.elist.size() - 1);
                System.out.println("已借阅");
            } else {
                System.out.println("请输入正确的序列号,已自动返回上级菜单");
            }
        } else {
        }
    }
}

以上就是JAVA图书管理系统的完整代码,但是还没有实现存储到SQL或TXT文本中的功能,也无法从SQL,TXT中读取数据

阅读更多
个人分类: JAVA小练
想对作者说点什么? 我来说一句

图书管理系统(java)

2009年03月22日 13.17MB 下载

java项目 图书管理系统SSH

2012年06月30日 7.56MB 下载

没有更多推荐了,返回首页

不良信息举报

JAVA项目之图书管理系统

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭