JAVA学习第五周(畸形版图书管理系统)

   这周搞了一个不太完整图书管理系统,深刻的体会到了“用户”、“操作”、“资源”的作用,代码如下

资源(Book):包含Book,Bookshelf

package Book;

public class Book {
    private int ISBN;
    private String name;
    private String author;
    private String state;
    private String returnTime;

    public Book(int ISBN, String name, String author, String state) {
        this.ISBN = ISBN;
        this.name = name;
        this.author = author;
        this.state = state;
    }

    public String getReturnTime() {
        return this.returnTime;
    }

    public void setReturnTime(String returnTime) {
        this.returnTime = returnTime;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getISBN() {
        return this.ISBN;
    }

    public void setISBN(int ISBN) {
        this.ISBN = ISBN;
    }

    public String getAuthor() {
        return this.author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}
package Book;

public class Bookshelf {
    private int totalCount;
    private int currCount;
    public Book[] books = new Book[10];

    public Bookshelf() {
        this.books[0] = new Book(1, "三国演义", "罗贯中", "未借出");
        this.books[1] = new Book(2, "红楼梦", "曹雪芹", "未借出");
        this.books[2] = new Book(3, "西游记", "吴承恩", "未借出");
        this.books[3] = new Book(4, "水浒传", "施耐庵", "未借出");
        this.currCount = 4;
        this.totalCount = 4;
    }

    public int getCurrCount() {
        return this.currCount;
    }

    public void setCurrCount(int currCount) {
        this.currCount = currCount;
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public void setBook(Book book) {
        this.books[this.currCount] = book;
    }
}

操作(operation):包含returnBook、exitSystem

package operation;

import Book.Bookshelf;
import java.util.Scanner;

public class returnBook {
    public void work(Bookshelf bookshelf) {
        System.out.println("请输入您想还的书:");
        Scanner sc = new Scanner(System.in);
        int flag = 0;
        String name = sc.nextLine();

        for(int i = 0; i < bookshelf.getCurrCount(); i++) {
            if (name.equals(bookshelf.books[i].getName())) {
                if(bookshelf.books[i].getState() == "未借出") {
                    System.out.println("该书未被借出");
                }
                flag = 1;
                bookshelf.books[i].setState("未借出");
                break;
            }
        }
        if (flag == 0) {
            System.out.println("对不起,没有这本书!");
        }
    }
}
package operation;

public class exitSystem {
    public void exitSystem() {
        System.out.println("退出成功!");
        System.exit(0);
    }
}

用户(user):包含User、NormalUser

package user;

import Book.Bookshelf;
import java.util.Scanner;
import operation.exitSystem;
import operation.returnBook;

public class NormalUser extends User {
    public NormalUser(int num) {
        super(num);
        this.setNum(num);
        System.out.println("*****欢迎书证号为"+num+"的读者来到图书管理系统*****");
    }
    public int menu() {
        int num = super.getNum();
        System.out.println("请输入您的操作:");
        System.out.println("0.退出系统");
        System.out.println("1.归还图书");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
    public void work(int choice,Bookshelf bookshelf) {
        Scanner sc = new Scanner(System.in);
        if(choice == 0) {
            new exitSystem().exitSystem();
        }
        if (choice == 1) {
            new returnBook().work(bookshelf);
        }
    }
}
package user;

import Book.Bookshelf;
import java.util.Scanner;
import operation.exitSystem;
import operation.returnBook;

public class NormalUser extends User {
    public NormalUser(int num) {
        super(num);
        this.setNum(num);
        System.out.println("*****欢迎书证号为"+num+"的读者来到图书管理系统*****");
    }
    public int menu() {
        int num = super.getNum();//继承父类
        System.out.println("请输入您的操作:");
        System.out.println("0.退出系统");
        System.out.println("1.归还图书");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
    public void work(int choice,Bookshelf bookshelf) {
        Scanner sc = new Scanner(System.in);
        if(choice == 0) {
            new exitSystem().exitSystem();
        }
        if (choice == 1) {
            new returnBook().work(bookshelf);
        }
    }
}

最后还有一个主代码(Main)

import Book.Bookshelf;
import java.util.Scanner;
import user.NormalUser;
import user.User;

public class Main {
    public static User login() {
        int[] users = new int[2000];
        for (int i = 1; i < users.length; i++) {
            users[i] = i;
        }
        System.out.println("*******欢迎来到图书管理系统*******");
        System.out.println("请选择您的身份:");
        System.out.println("2->用户");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        if(choice == 2) {
            System.out.println("请输入您的书证号:");
            int num = sc.nextInt();
            for (int i = 1; i < users.length; i++) {
                if (num == i) {
                    System.out.println("登入成功!");
                    return new NormalUser(num);
                }
            }
            System.out.println("没有该用户!");
            return null;
        }
        return null;
    }
    public static void main(String[] args) {
        Bookshelf bookshelf = new Bookshelf();
        User user = login();
        while(true) {
            int choice = user.menu();
            user.work(choice,bookshelf);
        }
    }
}

   代码的执行不是那么完整,实力不太够,哈哈哈哈

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

还未被认可

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值