Java使用面向对象编程思维编写图书管理系统:增加,查询,修改,删除,退出,怎么写?

Python015

Java使用面向对象编程思维编写图书管理系统:增加,查询,修改,删除,退出,怎么写?,第1张

package com.bms

import java.util.ArrayList

import java.util.List

import java.util.Scanner

// book对象

public class Book {

private String bId// 编号

private String bName// 书名

// getset方法

public String getbId() {

return bId

}

public void setbId(String bId) {

this.bId = bId

}

public String getbName() {

return bName

}

public void setbName(String bName) {

this.bName = bName

}

//构造方法

public Book() {

}

public Book(String bId, String bName) {

this.bId = bId

this.bName = bName

}

/*

* 增加

* */

public static List<Book>add(List<Book>list) {

Scanner sn = new Scanner(System.in)

System.out.print("请输入编号:")

String bid = sn.next()

System.out.print("请输入名称:")

String bName = sn.next()

Book book = new Book(bid, bName)

for (Book b : list) {

if (b.bId.equals(book.bId)) {

System.out.println("编号重复,请重新输入!")

return list

}

}

list.add(book)

System.out.println("添加成功!")

return list

}

/*

* 查询

* */

public static void query(List<Book>list) {

System.out.println("编号\t书名")

for (Book b : list) {

System.out.println(b.getbId() + "\t" + b.getbName())

}

}

/*

* 修改

* */

public static void update(List<Book>list) {

query(list)

Scanner sc = new Scanner(System.in)// 键盘输入的对象

System.out.print("请输入编号:")

String s = sc.next()

Integer id = null

for (int i = 0i <list.size()i++) {

id = list.get(i).getbId().equals(s) ? i : null

}

if (id == null) {

System.out.println("输入的编号不存在,请重新选择!")

return

}

System.out.print("请输入新的书名:")

String newName = sc.next()

list.get(id).setbName(newName)

System.out.print("修改成功!")

}

/*

* 删除

* */

public static void del(List<Book>list) {

query(list)

Scanner sc = new Scanner(System.in)// 键盘输入的对象

System.out.print("请输入编号:")

String s = sc.next()

for (int i = 0i <list.size()i++) {

if (list.get(i).getbId().equals(s)) {

list.remove(i)

return

}

}

System.out.println("输入的编号不存在,请重新选择!")

}

}

/*

* 测试*/

class Test {

public static void main(String[] args) {

List<Book>bookList = new ArrayList<>() // 存放所有图书的列表

bookList.add(new Book("1", "Java 基础"))// 图书的列表添加一本图书

System.out.print("欢迎进入图书管理系统,")

boolean b = true

while (b) {

System.out.print("请选择:\n1.增加\n2.查询\n3.修改\n4.删除\n5.退出\n(输入序号):")

Scanner sn = new Scanner(System.in)// 键盘输入的对象

String select = sn.next()

switch (select) {

case "1":

System.out.println("您选择了增加")

Book.add(bookList)

break

case "2":

System.out.println("您选择了查询:")

Book.query(bookList)

break

case "3":

System.out.println("您选择了修改")

Book.update(bookList)

break

case "4":

System.out.println("您选择了删除")

Book.del(bookList)

break

case "5":

System.out.println("您选择了退出")

b = false

System.out.println("退出程序!")

break

default:

System.out.println("输入错误的序号,请重新输入")

break

}

}

}

}

密码在数据库中保存。

首先登陆本地数据库,然后输入管理密码,就能查找到存储用户密码的表。

图书管理系统,是一个由人、计算机等组成的能进行管理信息的收集、传递、加工、保存、维护和使用的系统,其功能一般包括图书信息管理、用户信息管理、图书借阅、图书归还、违约与毁坏赔偿、图书借还查询等等。

import java.io.File

import java.io.FileOutputStream

import java.util.ArrayList

import java.util.List

import java.util.Scanner

import org.dom4j.Document

import org.dom4j.DocumentHelper

import org.dom4j.Element

import org.dom4j.io.OutputFormat

import org.dom4j.io.SAXReader

import org.dom4j.io.XMLWriter

public class Book {

    private int no

    private String name

    private double value

    public Book() {

    }

    public Book(int no, String name, double value) {

        this.no = no

        this.name = name

        this.value = value

    }

    public double getValue() {

        return value

    }

    public void setValue(double value) {

        this.value = value

    }

    public String getName() {

        return name

    }

    public void setName(String name) {

        this.name = name

    }

    public int getNo() {

        return no

    }

    public void setNo(int no) {

        this.no = no

    }

}

class BookList {

    private List<Book> bookList

    public BookList() {

        bookList = readXML()

    }

    public long getCount() {

        return bookList.size()

    }

    public List<Book> getBookList() {

        return bookList

    }

    public void setBookList(List<Book> bookList) {

        this.bookList = bookList

    }

    public void add(Book book) {

        bookList.add(book)

    }

    public boolean delete(String name) {

        Book book = query(name)

        return bookList.remove(book)

    }

    public void update(Book bookBefore, Book bookAfter) {

        bookList.remove(bookBefore)

        add(bookAfter)

    }

    public Book query(String name) {

        Book temp = null

        for (Book book : bookList) {

            if (book.getName().equals(name)) {

                temp = book

            }

        }

        return temp

    }

    public synchronized void writeXmlDocument(Book book) {

        try {

            File file = new File("D:\\book.xml")

            Document document = null

            Element root = null

            if (!file.exists()) {

                // 新建student.xml文件并新增内容

                document = DocumentHelper.createDocument()

                root = document.addElement("Books")//添加根节点   

            } else {

                SAXReader saxReader = new SAXReader()

                document = saxReader.read(file)

                root = document.getRootElement()//获得根节点

            }

            Element secondRoot = root.addElement("Book")//二级节点   

            //为二级节点添加属性,属性值为对应属性的值   

            secondRoot.addElement("no").setText(book.getNo() + "")

            secondRoot.addElement("name").setText(book.getName() + "")

            secondRoot.addElement("value").setText(book.getValue() + "")

            OutputFormat format = OutputFormat.createPrettyPrint()

            format.setEncoding("GBK")

            XMLWriter writer = new XMLWriter(new FileOutputStream("D:\\book.xml"), format)

            writer.write(document)

            writer.close()

            document.clearContent()

        } catch (Exception e) {

            e.printStackTrace()

        }

    }

    public synchronized List<Book> readXML() {

        List<Book> list = new ArrayList<Book>()//创建list集合   

        File file = null

        try {

            file = new File("D:\\book.xml")//读取文件   

            if (file.exists()) {

                SAXReader saxReader = new SAXReader()

                Document document = saxReader.read(file)

                List nodeList = document.selectNodes("Books/Book")

                for (int i = 0 i < nodeList.size() i++) {

                    Element el = (Element) nodeList.get(i)

                    Book book = new Book()

                    book.setNo(Integer.parseInt(el.elementText("no")))

                    book.setName(el.elementText("name"))

                    book.setValue(Double.parseDouble(el.elementText("value")))

                    list.add(book)

                }

            }

        } catch (Exception e) {

            e.printStackTrace()

        }

        return list

    }

}

class Test {

    public static void main(String args[]) {

        BookList bl = new BookList()

        boolean bBreak = true

        while (bBreak) {

            System.out.println("请输入操作代码:")

            System.out.println("1:添加 2:删除 3:修改 4:查询 5:书籍统计 6:退出")

            Scanner sc = new Scanner(System.in)

            int code = sc.nextInt()

            if (code == 1) {

                System.out.println("请输入编号")

                int no = sc.nextInt()

                System.out.println("请输入书名")

                String name = sc.next()

                System.out.println("请输入售价")

                double value = sc.nextDouble()

                Book book = new Book(no, name, value)

                bl.add(book)

                bl.writeXmlDocument(book)

            } else if (code == 2) {

                System.out.println("请输入要删除的书籍名")

                String name = sc.next()

                if (bl.delete(name)) {

                    System.out.println("删除成功")

                } else {

                    System.out.println("书籍不存在")

                }

            } else if (code == 3) {

                System.out.println("请输入要修改的书籍名")

                String name = sc.next()

                Book bookBefore = bl.query(name)

                System.out.println("请输入新的编号")

                int newNo = sc.nextInt()

                System.out.println("请输入新的书名")

                String newName = sc.next()

                System.out.println("请输入新的售价")

                double value = sc.nextDouble()

                Book bookAfter = new Book(newNo, newName, value)

                bl.update(bookBefore, bookAfter)

            } else if (code == 4) {

                System.out.println("请输入要查询的书籍名")

                String name = sc.next()

                Book book = bl.query(name)

                System.out.println("编号:" + book.getNo() + " 书名:" + book.getName() + " 售价:" + book.getValue())

            } else if (code == 5) {

                List<Book> list = bl.getBookList()

                System.out.println("总书籍数:" + bl.getCount())

                for (Book book : list) {

                    System.out.println("编号:" + book.getNo() + " 书名:" + book.getName() + " 售价:" + book.getValue())

                }

            } else if (code == 6) {

                bBreak = false

            }

        }

    }

}

jar 包  dom4j.jar  jaxen-1.1.4.jar