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

2021-06-09 12:04

阅读:374

标签: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 LibraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要查询的书籍名称");
        String name = sc.next();
        for (int i = 0; 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 LibraryBookDate) {
        // 使用lambda表达式
        Iterator 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 LibraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要借阅的书籍书名:");
        String name = sc.next();
        // 获取集合的迭代器
        Iterator 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 LibraryBookDate){
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要归还的书籍:");
        String name = sc.next();
        Iterator 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 LibraryBookDate = new ArrayList();
    
}

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 LibraryBookDate);
    public void display(List LibraryBookDate);
    public void borrow(List 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 LibraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要查找的书籍书名:");
        String name = sc.next();
        // 获取集合的迭代器
        Iterator 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 LibraryBookDate) {
        // 使用lambda表达式
        Iterator 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 LibraryBookDate) {
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除书籍的书名:");
        String name = sc.next();
        // 获取集合的迭代器
        Iterator 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 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 userdateBooks = new ArrayList();
}

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


评论


亲,登录后才可以留言!