图书管理系统

图书管理系统,用来实现图书上架,图书查询,图书销售和删除图书的方法。

public class Book {

    private int id;
    private String name;
    private String author;
    private double price;
    public static int number;
    private boolean statues = false;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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 int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public boolean isStatues() {
        return statues;
    }

    public void setStatues(boolean statues) {
        this.statues = statues;
    }

    public Book(int id, String name, String author, double price, int number){
        super();
        this.id = id;
        this.name = name;
        this.author = author;
        this.price = price;
        this.number = number;
    }

    @Override
    public String toString() {
        return "Book[" + "账号:" + id + ", 书名:'" + name + '\'' + ", 作者:'" + author + '\'' + ", 价格:" + price + '\'' + ", 数量:" + number + ']';
    }
}
public class BookList {
        private int usedsize;
        private Book[] books;

        public BookList() {
            this.books = new  Book[100];
            books[0]=new Book(111, "红楼梦", "曹雪芹", 159.8, 10);
            books[1]=new Book(222, "西游记", "吴承恩", 98, 13);
            books[2]=new Book(333, "三国演义", "罗贯中", 125.9, 9);
            books[3]=new Book(444, "水浒传", "施耐庵", 108.9, 11);
            books[4]=new Book(555, "画石", "曹雪芹", 95.8, 8);
            this.usedsize=5;
        }
        //下面可以写一些功能;
        public void  setBooks(int pos,Book book){
            this.books[pos]=book;
        }
        public Book getBook(int pos){
            return this.books[pos];
        }

        public int getUsedsize() {
            return usedsize;
        }

        public Book[] getBooks() {
            return books;
        }

        public void setUsedsize(int usedsize) {
            this.usedsize = usedsize;
        }
    }
import java.util.Scanner;

public class AddBook implements IBook{
    @Override
    public void work(BookList bookList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入图书编号:");
        int id = sc.nextInt();
        System.out.print("请输入图书名字:");
        String name = sc.next();
        System.out.print("请输入图书作者:");
        String author = sc.next();
        System.out.print("请输入图书价格:");
        int  price = sc.nextInt();
        System.out.print("请输入图书数量:");
        int number = sc.nextInt();
        Book book = new Book(id,name,author,price,number);
        int len = bookList.getUsedsize();
        bookList.setBooks(len,book);
        bookList.setUsedsize(len+1);
        System.out.println("添加成功!");
    }
}
import java.util.Scanner;

public class SearchBook implements IBook{
    @Override
    public void work(BookList bookList) {
        Scanner sc = new Scanner(System.in);
        System.out.print("输入需要查找图书的书名:");
        String name = sc.nextLine();
        for (int i =0;i<bookList.getUsedsize();i++){
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("查找成功,有这本书!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有这本书");
    }
}
import java.util.Scanner;

public class SaleBook implements IBook{
    @Override
    public void work(BookList bookList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查询的图书");
        String name = sc.nextLine();
        for (int i =0;i<bookList.getUsedsize();i++){
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                if(book.isStatues()==true){
                    System.out.println("该图书已经售出,无法购买!");
                    return;
                }else{
                    book.setStatues(true);
                    System.out.println("该图书成功销售!");
                    Book.number--;
                    return;
                }
            }
        }
        System.out.println("没有这本书,无法购买!");
    }
}
public class DeleteBook implements IBook{
    @Override
    public void work(BookList bookList) {
        Scanner sc = new Scanner(System.in);
        System.out.print("输入需要删除图书的书名:");
        String name = sc.nextLine();
        int i = 0;
        for (; i < bookList.getUsedsize(); i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                break;
            }
        }
        if (i >= bookList.getUsedsize()) {
            System.out.println("没有您需要删除的书!");
            return;
        }

        int pos = i;
        for (int j = pos; j < bookList.getUsedsize() - 1; j++) {
            Book book = bookList.getBook(j + 1);
            bookList.setBooks(j, book);
        }
        bookList.setUsedsize(bookList.getUsedsize() - 1);
        System.out.println("删除成功!");
    }
}
public class ExitManager implements IBook{
    @Override
    public void work(BookList bookList){
        System.exit(-1);
    }
}
public interface IBook {
    void work(BookList bookList);
}
public abstract class User {
    protected String name;
    protected IBook[] operation;

    public User(String name) {
        this.name = name;
    }
    public abstract int  menu();
    public void doOperation(int choice, BookList bookList){
        operation[choice-1].work(bookList);
    }
}
import java.util.Scanner;

//用集合开发图书管理系统,实现图书上架,图书查询,图书销售和书价修改等功能
public class TestUser extends User{
    public TestUser(String name) {
        super(name);
        this.operation = new IBook[]{
                new SearchBook(),
                new AddBook(),
                new DeleteBook(),
                new SaleBook(),
                new ExitManager(),
        };
    }

    public int menu(){
        System.out.println("-------------图书管理系统-------------");
        System.out.println("             1.图书查询");
        System.out.println("             2.添加图书");
        System.out.println("             3.图书删除");
        System.out.println("             4.图书销售");
        System.out.println("             5.退出系统");
        System.out.println("------------------------------------");
        System.out.println("请输入你的选择:");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();
        while (true){
            int choice = user.menu();
            user.doOperation(choice,bookList);
        }
    }
    public static User login(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你的姓名:");
        String name =sc.nextLine();
        System.out.println("请输入密码");//密码为111
        int  choice = sc.nextInt();
        if (choice == 1){
            return new TestUser(name);
        }else{
            return new TestUser(name);
        }
    }
}

这样就可以实现从控制台输出的代码,希望可以帮助到各位!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值