码迷,mamicode.com
首页 > 编程语言 > 详细

java实现简易的图书馆管理系统

时间:2021-03-08 13:13:35      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:pre   code   rgba   def   size   ever   author   spl   管理员   

比较适合新手练手的一个简易项目

直接上源码

Book

package code;
/**
 * @author ztr
 * @version 创建时间:2021年3月4日 下午8:21:40
 * 类说明
 */
public class Book {
    private String name;
    private String author;
    private double price;
    private String type;
//    有参构造器
    public Book(String name, String author, double price, String type) {
        super();
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    
}

Cuser

package code;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * @author ztr
 * @version 创建时间:2021年3月4日 下午8:32:15 类说明
 */
public class Cuser extends User {
    Scanner sc = new Scanner(System.in);
    public Cuser(String name, String sex, int age) {
        super(name, sex, age);
    }
//    查找书籍
    @Override
    public void serach(List<Book> LibraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要查询的书籍名称");
        String name = sc.next();
        for (int i = 0; i < LibraryBookDate.size(); i++) {
            if (name != null) {
                if (LibraryBookDate.get(i).getName().equals(name)) {
                    System.out.println("查询成功");
                    System.out.println(LibraryBookDate.get(i).getName() + " "
                            + LibraryBookDate.get(i).getAuthor() + " "
                            + LibraryBookDate.get(i).getPrice() + " "
                            + LibraryBookDate.get(i).getType());
                }
            } else {
                System.out.println("没有此书");
            }
        }
        

    }
//遍历图书
    @Override
    public void display(List<Book> LibraryBookDate) {
        // 使用lambda表达式
        Iterator<Book> it = LibraryBookDate.iterator();
        while (it.hasNext()) {
            // 因为it.next()返回的是object类型所以需要进行类型强转
            Book book = (Book) it.next();
            System.out.println(book.getName() + " " + book.getAuthor() + " "
                    + book.getPrice() + " " + book.getType());
        }
    }
//借阅书籍
    @Override
    public void borrow(List<Book> LibraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要借阅的书籍书名:");
        String name = sc.next();
        // 获取集合的迭代器
        Iterator<Book> it = LibraryBookDate.iterator();
        while (it.hasNext()) {
            Book book = (Book) it.next();
            if (book.getName().equals(name)) {
                System.out.println(book.getName() + " " + book.getAuthor()
                        + " " + book.getPrice() + " " + book.getType());
                UserBookDate.userdateBooks.add(book);
                it.remove();
            } else {
                System.out.println("没有该书籍");
            }
        }
        
    }
//    归还书籍
    public void revert(List<Book> LibraryBookDate){
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要归还的书籍:");
        String name = sc.next();
        Iterator<Book> it = UserBookDate.userdateBooks.iterator();
        while(it.hasNext()){
            Book book = (Book)it.next();
            if(book.getName().equals(name)){
                it.remove();
                LibraryBookDate.add(book);
            }            
        }
        
    }
}

LibraryBookDate

package code;

import java.util.ArrayList;
import java.util.List;



/**
 * @author ztr
 * @version 创建时间:2021年3月4日 下午9:21:13
 * 类说明
 */
public class LibraryBookDate {
    static List<Book> LibraryBookDate = new ArrayList<Book>();
    
}

Menu

ackage code;

import java.util.Scanner;

/**
 * @author ztr
 * @version 创建时间:2021年3月6日 下午4:28:07 类说明
 */
public class Menu {
    public void menuRuser() {
        Ruser ruser = new Ruser("ztr", "man", 20);
        System.out.println("1.查阅书籍" + "/n" + "2.遍历书籍" + "/n" + "3.删除书籍"
                + "4.添加书籍" + "5.返回上一层");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        switch (n) {

        case 1:
            ruser.serach(LibraryBookDate.LibraryBookDate);
            menuRuser();
            break;
        case 2:
            ruser.display(LibraryBookDate.LibraryBookDate);
            menuRuser();
            break;
        case 3:
            ruser.borrow(LibraryBookDate.LibraryBookDate);
        
            menuRuser();
            break;
        case 4:
            ruser.insert(LibraryBookDate.LibraryBookDate);        
            menuRuser();
            break;
        case 5:
            menu();
            break;
        default:
            System.out.println("请重新输入:");
            menuRuser();
            break;
        }
        sc.close();
    }

    public void menuCuser() {
        Cuser cuser = new Cuser("x", "man", 10);
        System.out.println("1.查阅书籍" + "/n" + "2.遍历书籍" + "/n" + "3.借阅书籍"
                + "4.归还书籍" + "5.返回上一层");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        switch (n) {

        case 1:
            cuser.serach(LibraryBookDate.LibraryBookDate);
            menuCuser();
            break;
        case 2:
            cuser.display(LibraryBookDate.LibraryBookDate);
            menuCuser();
            break;
        case 3:
            cuser.borrow(LibraryBookDate.LibraryBookDate);
            menuCuser();
            break;
        case 4:
            cuser.revert(LibraryBookDate.LibraryBookDate);
            menuCuser();
            break;
        case 5:
            menu();
            break;
        default:
            System.out.println("请重新输入:");
            menuCuser();
            break;
        }
        sc.close();
    }

    public void menu() {
        System.out.println("====图书馆管理系统===");
        System.out.println("1.用户  2.管理员  3.退出");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        switch (n) {
        case 1:
            menuCuser();
            break;
        case 2:
            menuRuser();
        case 3:
            break;
        default:
            System.out.println("请重新输入");
            break;
        }
        sc.close();
    }

}

Operate

package code;

import java.util.List;

/**
 * @author ztr
 * @version 创建时间:2021年3月4日 下午8:25:01
 * 类说明
 */
public interface Operate {
    public void serach(List<Book> LibraryBookDate);
    public void display(List<Book> LibraryBookDate);
    public void borrow(List<Book> LibraryBookDate);
}

Ruser

package code;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * @author ztr
 * @version 创建时间:2021年3月6日 下午3:56:25 类说明
 */
public class Ruser extends User {
    Scanner sc = new Scanner(System.in);
    public Ruser(String name, String sex, int age) {
        super(name, sex, age);
    }

    // 查找书籍
    @Override
    public void serach(List<Book> LibraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要查找的书籍书名:");
        String name = sc.next();
        // 获取集合的迭代器
        Iterator<Book> it = LibraryBookDate.iterator();
        while (it.hasNext()) {
            Book book = (Book) it.next();
            if (book.getName().equals(name)) {
                System.out.println(book.getName() + " " + book.getAuthor()
                        + " " + book.getPrice() + " " + book.getType());
                UserBookDate.userdateBooks.add(book);
            } else {
                System.out.println("没有该书籍");
            }
        }
    

    }

    // 遍历图书
    @Override
    public void display(List<Book> LibraryBookDate) {
        // 使用lambda表达式
        Iterator<Book> it = LibraryBookDate.iterator();
        while (it.hasNext()) {
            // 因为it.next()返回的是object类型所以需要进行类型强转
            Book book = (Book) it.next();
            System.out.println(book.getName() + " " + book.getAuthor() + " "
                    + book.getPrice() + " " + book.getType());
        }
    }

    // 删除图书
    @Override
    public void borrow(List<Book> LibraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除书籍的书名:");
        String name = sc.next();
        // 获取集合的迭代器
        Iterator<Book> it = LibraryBookDate.iterator();
        while (it.hasNext()) {
            Book book = (Book) it.next();
            if (book.getName().equals(name)) {
                System.out.println(book.getName() + " " + book.getAuthor()
                        + " " + book.getPrice() + " " + book.getType());
                it.remove();
            } else {
                System.out.println("没有该书籍");
            }
        }
    }

    // 添加图书
    public void insert(List<Book> libraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请依次输入锁添加书籍的信息:1.书名2.作者3价格4类型");
        String name = sc.next();
        String author = sc.next();
        double price = sc.nextDouble();
        String type = sc.next();
        Book book = new Book(name, author, price, type);
        libraryBookDate.add(book);
        
    }
}

User

package code;
/**
 * @author ztr
 * @version 创建时间:2021年3月4日 下午8:24:46
 * 类说明
 */
public abstract class User implements Operate{
    private String name;
    private String sex;
    private int age;
    
    public User(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}

UserBookDate

package code;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ztr
 * @version 创建时间:2021年3月6日 下午2:07:20
 * 类说明
 */
public class UserBookDate {
    static List<Book> userdateBooks = new ArrayList<Book>();
}

Test

package code;
/**
 * @author ztr
 * @version 创建时间:2021年3月6日 下午4:47:25
 * 类说明
 */
public class Test {
    public static void main(String[] args){
        Menu menuone = new Menu();
        menuone.menu();
    }
}

功能非常简单,但可以自己进行功能的扩展

java实现简易的图书馆管理系统

标签:pre   code   rgba   def   size   ever   author   spl   管理员   

原文地址:https://www.cnblogs.com/zoutingrong/p/14491504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!