JAVA版的图书管理系统

Python010

JAVA版的图书管理系统,第1张

送你一份代码,结构大致按照你的需求了,自己增加一些小功能,不会的话请教你同学。

分给我,钱就不用了。

#include <string.h>

#define FORMAT "\n%-8d%-8d%-7d%-8d%-7d%-8d%-10s%-9s%-9s\n"

#include <stdio.h>

#include <conio.h>

#include <malloc.h>

#define NULL 0

#define N 100

#define LEN sizeof(struct book)

int M

struct book

{int Enum

int Cnum

char name[10]

char author[10]

char publishor[30]

struct date

{int year

int month

int day}time

int price

struct book*next

}go[N]

void print()

{printf("---------------------------------------------------------------------------\n")

printf("EnumCnumyear month daypricename authorpublishor\n")

printf("---------------------------------------------------------------------------\n")

}

void load()

{FILE *fp

int i

if((fp=fopen("book_list","rb"))==NULL)

{printf("cannot open file\n")

return

}

i=0

while((fread(&go[i],sizeof(struct book),1,fp))!=NULL)

{i++}

M=i

fclose(fp)

}

void save(int h)

{FILE *fp

int i

if ((fp=fopen("BOOK_LIS","wb"))==NULL)

{printf("cannot open file\n")

return

}

for (i=0i<hi++)

if(fwrite(&go[i],sizeof(struct book),1,fp)!=1)

printf("file write error\n")

fclose(fp)

}

void f1()

{FILE *fp

int i=0

fp=fopen("book_list","rb")

print()

while((fread(&go[i],sizeof(struct book),1,fp))!=NULL)

{printf(FORMAT,go[i].Enum,go[i].Cnum,go[i].time.year,go[i].time.month,go[i].time.day,go[i].price,go[i].name,go[i].author,go[i].publishor)

i++

}

fclose(fp)

getch()

}

void f2(int h)

{int i

printf("please input %d book's information\n",h)

printf("Enum Cnum yearmonth dayprice nameauthor publishor\n")

for(i=0i<hi++)

{printf("Enum:\n")

scanf("%d",&go[i].Enum)

printf("Cnum:\n")

scanf("%d",&go[i].Cnum)

printf("please input year month day\n")

scanf("%d%d%d",&go[i].time.year,&go[i].time.month,&go[i].time.day)

printf("price:\n")

scanf("%d",&go[i].price)

printf("name:\n")

scanf("%s",go[i].name)

printf("author:\n")

scanf("%s",go[i].author)

printf("publishor:\n")

scanf("%s",go[i].publishor)

}

save(h)

return

}

void f3()

{int i

struct book

char a[20],b[20],c[20]

load()

printf("please input 'name' or 'author':\n")

scanf("%s",a)

if(strcmp(a,"name")==0)

{printf("please input the book's name:\n")

scanf("%s",b)

for(i=0i<Mi++)

if(strcmp(b,go[i].name)==0)

{print()

printf(FORMAT,go[i].Enum,go[i].Cnum,go[i].time.year,go[i].time.month,go[i].time.day,go[i].price,go[i].name,go[i].author,go[i].publishor)

}

}

else

{printf("please input the book's author:\n")

scanf("%s",c)

for(i=0i<Mi++)

if(strcmp(c,go[i].author)==0)

{print()

printf(FORMAT,go[i].Enum,go[i].Cnum,go[i].time.year,go[i].time.month,go[i].time.day,go[i].price,go[i].name,go[i].author,go[i].publishor)

}

}

return

}

void f4()

{int i,j,k

struct book t

load()

for(i=0i<Mi++)

{k=i

for(j=i+1j<Mj++)

if(go[k].price>go[j].price)k=j

t=go[i]go[i]=go[k]go[k]=t

}

print()

for(i=0i<Mi++)

printf(FORMAT,go[i].Enum,go[i].Cnum,go[i].time.year,go[i].time.month,go[i].time.day,go[i].price,go[i].name,go[i].author,go[i].publishor)

}

void f5()

{FILE *fp

int i,j,M,flag

char name[10]

if((fp=fopen("book_list","rb"))==NULL)

{printf("cannot open file\n")

return

}

printf("\norriginal data:\n")

print()

for(i=0fread(&go[i],sizeof(struct book),1,fp)!=0i++)

printf(FORMAT,go[i].Enum,go[i].Cnum,go[i].time.year,go[i].time.month,go[i].time.day,go[i].price,go[i].name,go[i].author,go[i].publishor)

M=i

printf("\n input the deleted name:\n")

scanf("%s",name)

for(flag=1,i=0flag&&i<Mi++)

{if(strcmp(name,go[i].name)==0)

{for(j=ij<M-1j++)

{go[j].Enum=go[j+1].Enum

go[j].Enum=go[j+1].Enum

strcpy(go[j].name,go[j+1].name)

strcpy(go[j].author,go[j+1].author)

strcpy(go[j].publishor,go[j+1].publishor)

go[j].time.year=go[j+1].time.year

go[j].time.month=go[j+1].time.month

go[j].time.day=go[j+1].time.day

go[j].price=go[j+1].price

}

flag=0

}

}

if(!flag)

M=M-1

else

printf("not found!\n")

printf("\nNow,the content of file:\n")

fp=fopen("book_list","wb")

for(i=0i<Mi++)

fwrite(&go[i],sizeof(struct book),1,fp)

fclose(fp)

fp=fopen("book_list","wb")

for(i=0fread(&go[i],sizeof(struct book),1,fp)!=0i++)

printf(FORMAT,go[i].Enum,go[i].Cnum,go[i].time.year,go[i].time.month,go[i].time.day,go[i].price,go[i].name,go[i].author,go[i].publishor)

fclose(fp)

}

main()

{int i,h

clrscr()

while(1)

{printf ("\n 1 is a project that can output all information.\n")

printf ("\n 2 is a project that can add any book's information.\n")

printf ("\n 3 is a project that can search information.\n")

printf ("\n 4 is a project that can sort.\n")

printf ("\n 5 is a project that can del.\n")

printf ("\n 6 is a project that can leave.\n")

printf("please input number:1 or 2 or 3 or 4 or 5 or 6\n")

scanf("%d",&i)

switch (i)

{case 1:f1()break

case 2:

{printf ("if you want to add book's information,please input a data:h=")

scanf("%d",&h)

f2(h)}break

case 3:

{f3()getch()}break

case 4:{f4()getch()}break

case 5:{f5()getch()}break

case 6:exit (1)

}

clrscr()}

}

是属于正常现象。java图书管理系统录入不显示汉语是属于正常现象。Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。

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