新手学习-图书管理系统


只完成了一小部分 刚学习的时候写的东西
管理员账号admin 密码a
注册的时候需要账号要3-5位数字组成

———2017.6.27

ClientMain.java

import static java.lang.System.out;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

public class ClientMain {
    static HashSet<Account> set = new HashSet<Account>();
    static TreeSet<Book> treeset = new TreeSet<Book>();

    // 客户端
    public static void main(String[] args) {
        // 添加管理员账号
        set.add(new Account("admin", "a"));
        while (true) {
            welcome();
            choose();

        }

    }

    // 注册系统
    public static void register() {
        out.println("进入注册系统.....");
        out.println("请输入账号");
        boolean flag = true;
        String id;
        String password = null;
        Scanner scanner = new Scanner(System.in);
        while (flag) {
            while (scanner.hasNext()) {
                id = scanner.next();

                // 检测账号是否规范
                if (id.matches("[1-9]\\d{3,5}")) {
                    out.println("请输入密码.....");
                    while (scanner.hasNext()) {
                        password = scanner.next();                      
                        break;
                    }

                    // 检测账号是否已经注册
                    if (set.add(new Account(id, password))) {
                        flag = false;
                        out.println("注册成功!!!");
                        out.println("当前注册的账号有" + set);
                        break;
                    } else { // 如果账号已经注册 则返回到输入账号位置
                        out.println("此帐号已注册.....请重新输入账号...");
                    }
                } else {
                    out.println("你的账号不规范!!");
                }
            }
        }
    }

    // 登录系统---->图书管理选择系统
    public static void login() {
        out.println("进入登录系统..........");
        out.println("请输入账号");
        String id;
        String password = null;
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            id = scanner.next();
            // 输入账号过程
            if (id.matches("[1-9]\\d{3,5}") | id.equals("admin")) {
                out.println("请输入密码.....");
                while (scanner.hasNext()) {
                    password = scanner.next();

                    break;
                }
                // 判断账号是否正确
                if (set.contains(new Account(id, password))) {
                    if (id.equals("admin")) {
                        out.print("管理员登陆成功...");
                        out.println("进入图书管理系统");

                        manageChoose();// 进入图书管理系统
                    } else {
                        out.print("登陆成功...");
                        out.println("进入图书管理系统");

                        manageChoose();// 进入图书管理系统
                    }
                    break;
                } else { // 返回输入位置
                    out.println("你的账号或者密码有误");
                    break;
                }
            } else { //// 返回输入位置
                out.println("您的账号有误.....请重新输入....");
            }
        }
    }

    // 登录系统-选择系统
    public static void choose() {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String string = scanner.next();
            switch (string) {
            case ("a"):
            case ("A"): {
                login();
                break;
            }
            case ("b"):
            case ("B"): {
                register();
                break;
            }
            case ("c"):
            case ("C"): {
                System.exit(0);
                break;
            }
            }

            break;
        }
    }

    // 登录欢迎界面
    public static void welcome() {
        out.println("************************************************************");
        out.println("*                                                          *");
        out.println("*                                                          *");
        out.println("*                                                          *");
        out.println("*                                                          *");
        out.println("*             欢迎进入图书管理系统   V1.0                    *");
        out.println("*                                                          *");
        out.println("*                                                          *");
        out.println("*                                                          *");
        out.println("*                             Copyright ? 2017 by lijing   *");
        out.println("************************************************************");
        out.println("  请选择功能:    用户登录(A)    用户注册(B)    退出系统(C)");
    }

    // 图书馆界面
    public static void welcome2() {
        System.out.println("请选择功能:  查看图书(A) 添加图书(B) 修改图书(C) 删除图书(D)");
        System.out.println("修改用户名(E) 修改密码(F) 修改用户(G) 删除用户(H) 查看所有用户(I) 注销(Q)");
    }

    // 图书管理系统-选择系统
    public static void manageChoose() {
        String choose = null;
        Scanner scanner = new Scanner(System.in);
        // 检测输入的按键是什么 进入相应的功能中
        while(true){
            welcome2();
            while (scanner.hasNext()) {

                choose = scanner.next();

                break;
            }
            switch (choose) {
                case ("a"):
                case ("A"): {
                    out.println("查看图书");
                    check();
                    break;
                }
                case ("b"):
                case ("B"): {
                    out.println("添加图书");
                    add();
                    break;
                }
                case ("c"):
                case ("C"): {
                    out.println("修改图书");
                    break;
                }
                case ("q"):
                case ("Q"): {
                    out.println("注销用户");
                    return;

                }
            }
        }

    }

    // 图书查看系统
    public static void check() {
//      String sort = "A";

        /*switch(sort){
            case("a"):
            case("A"):{
                IdSort  compartor = new IdSort();
            }
        }*/
        System.out.println("|书名|    |作者|    |出版社|   |价格|    |书号|    |出版日期|");
        Iterator<Book> it = treeset.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println("请选择查看方式 : (A)价格排序 (B)作者排序 (C)出版日期排序 (E)返回上一级");
    }

    // 图书添加系统
    public static void add() {

        Scanner scanner = new Scanner(System.in);
        String name = null;
        String author = null;
        String publishing = null;
        double price = 0;
        int id = 0;
        String date = null;
        System.out.println("请输入书名");
        while (scanner.hasNext()) {
            name = scanner.next();
            break;
        }
        System.out.println("请输入作者");
        while (scanner.hasNext()) {

            author = scanner.next();
            break;
        }
        System.out.println("请输入出版社");
        while (scanner.hasNext()) {

            publishing = scanner.next();
            break;
        }
        System.out.println("请输入价格");
        while (scanner.hasNextDouble()) {

            price = scanner.nextDouble();
            break;
        }
        System.out.println("请输入书号");
        while (scanner.hasNextInt()) {

            id = scanner.nextInt();
            break;
        }
        System.out.println("请输入出版日期");
        while (scanner.hasNext()) {

            date = scanner.next();
            break;
        }
        treeset.add(new Book(name, author, publishing, price, id, date));

    }

}

Book.java

public class Book implements Comparable<Book> {
    String name;
    String author;
    String publishing;
    double price;
    int id;
    String date;

    public Book(String name, String author, String publishing, double price, int id, String date) {

        this.name = name;
        this.author = author;
        this.publishing = publishing;
        this.price = price;
        this.id = id;
        this.date = date;
    }


    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.name+"   "+this.author+"     "+this.publishing+"     "+this.price+"     "+this.id+"  "+this.date;
    }


    @Override
    public int compareTo(Book o) {
        // TODO Auto-generated method stub
        return this.id-o.id;
    }



}

Account.java



/**
 * Created by LiJing on 2017/6/27.
 */
public class Account {
    String id;
    String password;




    public Account(String id, String password) {

        this.id = id;
        this.password = password;
    }

    @Override
    public String toString(){
        return this.id;
    }

    @Override
    public int hashCode(){
        //让管理员可以登录 同时防止普通用户注册管理员
        if(this.id.equals("admin")){
            return 0;
        }

        return Integer.parseInt(this.id);
    }
    @Override
    public boolean equals(Object o){
        Account account=(Account)o;
        return this.id.equals(account.id)&&this.password.equals(account.password)/**/;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言源代码的图书管理系统 #include<iostream.h> #include<fstream.H> #include<stdlib.h> #include<string.h> struct bookData { int booknumber; char bookname[30]; int store; }; struct Person { char name[20]; char studynumber[10]; int count; bookData Rbook[2]; //bookData Rbook; }; class History { public: History(fstream& a){readerbook=a;} void writehistory(); void readhistory(fstream&); private: fstream readerbook; }; class Liberian; void Find(fstream&); void CreateTxt(fstream&); class Reader { friend Liberian; public: Reader(fstream&); void print(); void handlebook(fstream&); void change(); bool findreader(); void Addreader(); void deletereader(); private: fstream file; Person person; }; class Liberian { public: Liberian(char *,char *); void changeReader(Reader&); void selfprint(); void Append(fstream&); private: char workname[20]; char worknumber[20]; }; int main() { fstream bookRecord(" book.dat",ios::in| ios::out); if(!bookRecord) { cerr<<" Can't open,try again!"<<endl; exit(1); } fstream readerRecord("reader.dat",ios::in|ios::out); if(!readerRecord) { cerr<<" Can't open,try again!"<<endl; exit(1); } int choice; int key; Reader reader(readerRecord);//读者管理系统 Liberian liberian("李天","123456");//图书管理员 History rec(readerRecord);//声明显示历史记录类 while(1) { cout<<" 请输入您的选择:\n\n" <<"1--图书管理员\n" <<"2--读者系统\n" <<"0--结束程序\n"; cin>>choice; switch(choice) { case 1: { cout<<" Hello,图书管理员:\n"; liberian.selfprint();cout<<endl; cout<<"1-- 增加新图书\n" <<"2-- 改变读者的属性\n" <<"3-- 显示历史记录\n" <<"0-- 返回\n"; cin>>key; switch(key) { case 1: liberian.Append(bookRecord); break; case 2: liberian.changeReader(reader); break; case 3: rec.readhistory(readerRecord); case 0: break ; } } break; case 2: { cout<<"学生读者\n"; int iflag=1; //reader.findreader(); cout<<"1-- 借书与还书信息\n" <<"2-- 修改个人信息\n" <<"0-- 返回\n"; cin>>key; switch(key) { case 1: reader.handlebook(bookRecord); break; case 2: reader.change(); break; case 0: break; } } break; default: return 0; } } return 0; } //增加书籍 void Liberian::Append(fstream& storef) { bookData book; int choice; int key; int num; storef.seekp(0,ios::end); int posEnd=storef.tellp(); cout<<"*************书籍入库***************"<<endl; while(1) { cout<<" Please enter your choice:"<<endl; cout<<"1--添加新书\n"<<"2--已有书籍\n"<<"0--返回\n"; cin>>choice; switch(choice) { case 1: cout<<" 书号, 书名, 数量?"<<endl; cin>>book.booknumber>>book.bookname>>book.store; storef.write(reinterpret_cast<char*>(&book),sizeof(bookData)); break; case 2: storef.seekg(0,ios::beg); cout<<"Booknumber?\n"; cin>>key; do{ storef.read(reinterpret_cast<char*>(&book),sizeof(bookData)); }while(book.booknumber!=key&&storef.tellg()!=posEnd); if(book.booknumber==key) { cout<<book.booknumber<<"----"<<book.bookname<<"----"<<book.store<<endl; cout<<" 输入已有书增加的数量:"<<endl; cin>>num; if(num>0) book.store+=num; else { cout<<" Invalid input"<<endl; } storef.seekp(-long(sizeof(bookData)),ios::cur);//////////////////// storef.write(reinterpret_cast<char*>(&book),sizeof(bookData)); cout<<"现在书籍: "<<book.bookname<<" 余量为: "<<book.store<<endl; } else cout<<"找不到此书,请重新查阅书号是否正确!"<<endl; break; case 0: return ; } } } //书籍查询 void Find(fstream & f) { bookData book; int key; int choice; f.seekg(0,ios::end); int posEnd=f.tellp(); cout<<"*************书籍查询***************"<<endl; while(1) { cout<<" 请输入您的选择\n" <<" 1-- 检索一本书\n" <<"2-- 显示全部书籍\n" <<"0-- 返回\n"; cin>>choice; switch(choice) { case 1: f.seekg(0,ios::beg); cout<<"输入你想检索书的书号"<<endl; cin>>key; do{ f.read(reinterpret_cast<char*>(&book),sizeof(bookData)); }while(book.booknumber!=key&&f.tellg()!=posEnd); if(book.booknumber==key) cout<<book.booknumber<<"---"<<book.bookname<<"---"<<book.store<<endl; else cout<<"找不到此书,请重新确认!"<<endl; break; case 2: f.seekg(0,ios::beg); do{ f.read(reinterpret_cast<char*>(&book),sizeof(bookData)); cout<<book.booknumber<<"---"<<book.bookname<<"--"<<book.store<<endl; }while(f.tellg()!=posEnd); break; case 0: return ; } } } Reader::Reader(fstream& c) { file=c; Person person={"0","0",0,{{0,"0",0}, {0,"0",0}}}; } void Reader::print() { cout<<"Name"<<"---"<<person.name<<'\n\n'<<"studynumber---"<<person.studynumber<<'\n'; } //操作书籍?????????????????????? void Reader::handlebook(fstream &filee) { int key; int choice; bookData book; bool iflag;int num=0; filee.seekp(0,ios::end); int posEnd=filee.tellp(); while(!(iflag=findreader())) { num++; if(num>=3) return; } //cout<<person.name; while(1) { cout<<"1-- 借书\n" <<"2-- 还书\n" <<"3-- 查找一本书\n" <<"0-- 返回"<<'\n'; cin>>choice; switch(choice) { case 1: { filee.seekp(0,ios::end); int posEnd=filee.tellp(); if(person.count<=1) { cout<<" 输入你要借阅图书的书号:"<<endl; cin>>key; filee.seekg(0); do{ filee.read(reinterpret_cast<char*>(&book),sizeof(bookData)); }while(book.booknumber!=key&&filee.tellp()!=posEnd); //cout<<book.booknumber<<endl; if(book.booknumber==key) { //cout<<" 找到你想借阅的图书,成功借阅!"<<endl; if(book.store>0) { person.Rbook[person.count]=book; person.count+=1; book.store-=1; filee.seekp(-long(sizeof(bookData)),ios::cur); filee.write(reinterpret_cast<char*>(&book),sizeof(bookData)); cout<<" 找到你想借阅的图书,成功借阅!"<<endl; cout<<"书籍 :"<<book.bookname<<"剩余的本数为:"<<book.store<<endl; } else cout<<" 现在此图书已经被借完了,请耐心等待几天!"<<endl; } else cout<<" 找不到你想要的图书"<<endl; } else cout<<"你最多只能借2本\n\n"; } break; case 2: { bookData blankbook={0,"0",0}; //if(person.count>0) //{ int iflag=0; filee.seekg(0); cout<<" 输入你想要还的书的书号:"<<endl; cin>>key; for(int i=0;i<2;i++) { if(key==person.Rbook[i].booknumber) { person.Rbook[i]=blankbook; cout<<"成功归还此书!"; iflag=1; person.count--; do{ filee.read(reinterpret_cast<char*>(&book),sizeof(bookData)); }while(book.booknumber!=key&&filee.tellp()!=posEnd); if(book.booknumber==key) { book.store+=1; filee.seekp(-long(sizeof(bookData)),ios::cur); filee.write(reinterpret_cast<char*>(&book),sizeof(bookData)); cout<<"书籍 :"<<book.bookname<<"余本量为: "<<book.store<<endl; } } } if(!iflag) { cout<<" 你没有借阅那本书,请确认!"<<endl; } // } // else // cout<<"你没有借书,请重新确认!\n\n"; } break; case 3: Find(filee); break; case 0: return; } } } void Reader::change() { char newname[20]; char newnumber[10]; int choice; file.seekp(0,ios::cur); int Posend=file.tellp(); bool iflag;int num=0; while(!(iflag=findreader())) { num++; if(num>=3) return; } cout<<" 请输入你的选择!"<<endl; cout<<"1--改变名字!\n" <<"2--修改学号r\n" <<"0--返回\n"; cin>>choice; switch(choice) { case 1: cout<<"输入你的新名字!\n"; cin>>newname; strcpy(person.name,newname); break; case 2: cout<<"输入你的信学号?\n"; cin>>newnumber; strcpy(person.studynumber,newnumber); break; default: break;; } file.seekp(-long(sizeof(Person)),ios::cur); file.write(reinterpret_cast<char*>(&person),sizeof(Person)); cout<<"信息修改成功!"<<endl; } void Liberian::selfprint() { cout<<" 图书管理员 :\n" <<worknumber<<"---"<<workname<<" 为您服务!\n"; } Liberian::Liberian(char *a,char*b) { strcpy(workname,a); strcpy(worknumber,b); } void Liberian::changeReader(Reader& a) { int choice; char newname[20];char newnumber[10]; cout<<"修改读者的数据信息!\n"; cout<<"1-- 增加一个读者\n" <<"2-- 删除一个读者\n" <<"0-- 结束程序\n"; cin>>choice; switch(choice) { case 1: a.Addreader(); break; case 2: a.deletereader(); break; case 0: return; } } void Reader::Addreader() { Person temp;bookData book={0,"0",0}; file.seekp(0,ios::end); int Posend=file.tellp(); cout<<"输入你想要增加的读者的名字"<<endl; cin>>temp.name; cout<<"新读者的学号:"<<endl; cin>>temp.studynumber; temp.Rbook[1]=book; temp.Rbook[0]=book; temp.count=0; file.write(reinterpret_cast<char*>(&temp),sizeof(Person)); cout<<" 成功添加!"<<temp.name<<endl; } void Reader::deletereader() { file.seekp(0,ios::end); int Posend=file.tellp(); Person person; char name[20]; cout<<"输入要删除的读者的名字!"<<endl; cin>>name; file.seekg(0); do{ file.read(reinterpret_cast<char*>(&person),sizeof(Person)); }while(strcmp(name,person.name)&&file.tellp()!=Posend); if(!strcmp(name,person.name)) { bookData blankbook={0,"0",0}; Person guest={"0","0",0,{{0,"0",0},{0,"0",0}}}; file.seekp(-long(sizeof(Person)),ios::cur); file.write(reinterpret_cast<char*>(&guest),sizeof(Person)); cout<<"成功删除!"<<person.name<<endl; } else cout<<"查无此人!"<<endl; } bool Reader::findreader() { file.seekp(0,ios::end); int Posend=file.tellp(); char name[20]; Person guest={"0","0",0,{{0,"0",0},{0,"0",0}}}; cout<<"输入查找人的名字!"; cin>>name; file.seekg(0); do{ file.read(reinterpret_cast<char*>(&person),sizeof(Person)); }while(strcmp(name,person.name)&&file.tellp()!=Posend); if(!strcmp(name,person.name)) { cout<<"查到此人:"<<person.name<<endl; return true; } else { cout<<"找不到此人,请重新确认!"<<endl; return false; } } //借阅历史 void History::readhistory(fstream& a) { readerbook=a; char name[10]="0"; readerbook.seekp(0,ios::end); int Posend=readerbook.tellp(); Person guest; int iflag=1; cout<<" 书籍借阅信息如下:"<<endl; cout<<"姓名 学号 借书量 书籍名"<<endl; readerbook.seekg(0,ios::beg); do{ readerbook.read(reinterpret_cast<char*>(&guest),sizeof(Person)); if(strcmp(guest.Rbook[1].bookname,name) || strcmp(guest.Rbook[1].bookname,name)) { cout<<guest.name<<" "<<guest.studynumber<<" "<<guest.count<<" "; for(int i=0;i<2;i++) if(strcmp(guest.Rbook[i].bookname,name)) cout<<guest.Rbook[i].bookname; iflag=0; } }while(readerbook.tellp()!=Posend); if(iflag) cout<<"没有读者借阅图书!"<<endl; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值