Java学习日记 2022.7.3

文章目录

    • 图书管理系统
    • Book类
    • Demo类
    • DateUtil类


图书管理系统

图书类Book
属性
书名 name
作者 author
出版社 publisher
定价 price
租借人 leaser
出租日期 rentDate
日租金 rent
租借天数 day
状态 state

不提供带参构造方法

提供成员方法show

提供getter/setter方法

测试类Demo
静态变量
控制台扫描器
图书数组 bookArray, 长度为5

静态代码块 static{} 初始化3个Book对象,  一个未逾期的一个已逾期的, 
		一个7日内逾期的, 一个未逾期的
                   存储到bookArray数组中用于测试

主函数 - do-while 打印菜单, 并让用户选择
	1. 查看图书列表 display()
	2. 添加图书     addBook()
	3. 删除图书    deleteBook()
	4. 租借         rentOut()
	5. 归还         giveBack()
	6. 续租         relet()
		 要求: 用户输入续租的天数, 然后更新对象中的天数
		 	测试的时候, 可以对一个已经过期的图书进行续租, 
			续租成功后, 再次查看已逾期时, 就不会出现了
	7. 查看7日内即将到期的 expireIn7Days()
		 要求: 找出7日内即将到期的图书, 并打印出剩余天数
	8. 查看已逾期的	overdue()
		 要求: 找出已经逾期的图书, 并打印出逾期天数
	9. 退出         

编写相应的静态函数实现上述功能

Book类

package BookSystem;

public class Book {
    private String name;
    private String author;
    private String publisher;
    private Double price;
    private String leaser;
    private String rentDate;
    private Double rent;
    private Integer day;
    private Integer state;

    public void show(){
        System.out.println("********************");
        System.out.print("书名:" + this.name);
        System.out.print("\t作者:" + this.author);
        System.out.print("\t出版社:" + this.publisher);
        System.out.print("\t定价:" + this.price);
        System.out.println("\t状态:" + (this.state == 0? "未租借" : "已租借"));
    }

    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 String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getLeaser() {
        return leaser;
    }

    public void setLeaser(String leaser) {
        this.leaser = leaser;
    }

    public String getRentDate() {
        return rentDate;
    }

    public void setRentDate(String rentDate) {
        this.rentDate = rentDate;
    }

    public Double getRent() {
        return rent;
    }

    public void setRent(Double rent) {
        this.rent = rent;
    }

    public Integer getDay() {
        return day;
    }

    public void setDay(Integer day) {
        this.day = day;
    }

    public Integer getState() {
        return state;
    }

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

Demo类

package BookSystem;

import java.util.Date;
import java.util.Scanner;

public class Demo {
    static Scanner sc = new Scanner(System.in);
    static Book[] bookArray = new Book[5];
    static {
        Book book1 = new Book();
        book1.setName("老人与海");
        book1.setAuthor("欧内斯特·海明威");
        book1.setPublisher("中国友谊出版公司");
        book1.setPrice(39.80);
        book1.setLeaser("张三");
        book1.setRentDate("2022-6-24");
        book1.setRent(1.50);
        book1.setDay(10);
        book1.setState(1);
        bookArray[0] = book1;

        Book book2 = new Book();
        book2.setName("呐喊");
        book2.setAuthor("鲁迅");
        book2.setPublisher("人民文学出版社");
        book2.setPrice(20.00);
        book2.setLeaser("李四");
        book2.setRentDate("2022-6-15");
        book2.setRent(2.0);
        book2.setDay(10);
        book2.setState(1);
        bookArray[1] = book2;

        Book book3 = new Book();
        book3.setName("西厢记");
        book3.setAuthor("王实甫");
        book3.setPublisher("三秦出版社");
        book3.setPrice(49.60);
        book3.setLeaser("王五");
        book3.setRentDate("2022-7-1");
        book3.setRent(2.5);
        book3.setDay(15);
        book3.setState(1);
        bookArray[2] = book3;
    }

    public static void main(String[] args) {
        int choice;
        do {
            System.out.println("***********************************");
            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("***********6.续租图书***************");
            System.out.println("***********7.查看七日内即将到期的图书**");
            System.out.println("***********8.查看已逾期的图书*********");
            System.out.println("***********9.退出叮当图书************");
            System.out.println("***********************************");
            System.out.println("请选择:");
            choice = sc.nextInt();
            switch (choice) {
                case 1:
                    display();
                    break;
                case 2:
                    addBook();
                    break;
                case 3:
                    deleteBook();
                    break;
                case 4:
                    rentOut();
                    break;
                case 5:
                    giveBack();
                    break;
                case 6:
                    relet();
                    break;
                case 7:
                    expireIn7Days();
                    break;
                case 8:
                    overdue();
                    break;
                case 9:
                    System.out.println("叮当图书已退出!");
                    break;
                default:
                    System.out.println("输入不合法!");
                    break;
            }
        } while (choice != 9);
    }

    private static void overdue() {
        System.out.println("*****欢迎进入查看逾期图书界面*****");
        for (int i = 0; i < bookArray.length; i++) {
            Book book = bookArray[i];
            if (book == null){
                break;
            }else{
                int rentDay = timeRemaining(book);
                if (rentDay > book.getDay()){
                    System.out.println((i + 1) + "号图书:");
                    book.show();
                    System.out.println("租借人:"  + book.getLeaser()
                            + "出租日期:" + book.getRentDate()
                            + "租借天数:" + book.getDay());
                }
            }
        }

    }

    private static int timeRemaining(Book book){
        Long now = new Date().getTime();
        Long rentTime = DateUtil.sting2date(book.getRentDate()).getTime();
        int rentDay = (int) ((now - rentTime)/1000/60/60/24);
        return rentDay;
    }

    private static void expireIn7Days() {
        System.out.println("*****欢迎进入查看即将到期图书界面*****");
        for (int i = 0; i < bookArray.length; i++) {
            Book book = bookArray[i];
            if (book == null){
                break;
            }else{
                int rentDay = timeRemaining(book);
                if (rentDay < book.getDay() && rentDay < 7 && rentDay >=0){
                    System.out.println((i + 1) + "号图书:");
                    book.show();
                    System.out.println("租借人:"  + book.getLeaser()
                            + "出租日期:" + book.getRentDate()
                            + "租借天数:" + book.getDay());
                }
            }
        }
    }

    private static void relet() {
        System.out.println("*********欢迎进入续租图书界面**********");
        display();
        System.out.println("请选择续租的图书:");
        int choice = sc.nextInt() - 1;
        Book book = bookArray[choice];
        if (book == null){
            System.out.println("该书籍为空,请正确选择");
        }else if (book.getState() == 0){
            System.out.println("该图书未被借出,无法续租");
        }else {
            System.out.println("租借人:"  + book.getLeaser()
                            + "出租日期:" + book.getRentDate()
                            + "租借天数:" + book.getDay());
            System.out.println("请输入需要续租的天数:");
            book.setDay(book.getDay() + sc.nextInt());
            bookArray[choice] = book;
            System.out.println("书名:" + book.getName()
                    + "作者" +  book.getAuthor() + "续租成功!");
        }
    }

    private static void giveBack() {
        System.out.println("*********欢迎进入归还图书界面**********");
        display();
        System.out.println("请选择需要归还的图书:");
        int choice = sc.nextInt() -1;
        Book book = bookArray[choice];
        if (book == null){
            System.out.println("该书籍为空,请正确选择");
        }else if (book.getState() == 0){
            System.out.println("该图书未被借出,无法归还");
        }else {
            System.out.println("请支付:" + book.getRent() * book.getDay() + "元");
            book.setState(0);
            System.out.println("书名:" + book.getName()
                    + "作者" +  book.getAuthor() + "归还成功!");
        }
    }

    private static void rentOut() {
        System.out.println("*********欢迎进入租借图书界面**********");
        display();
        System.out.println("请选择需要租借的图书:");
        int choice = sc.nextInt() -1;
        Book book = bookArray[choice];
        if (book == null){
            System.out.println("该书籍为空,请正确选择");
        }else if (book.getState() == 1){
            System.out.println("该图书已被借出,无法租借");
        }else {
            System.out.println("请输入租借人姓名:");
            book.setLeaser(sc.next());
            System.out.println("请输入出租日期:");
            book.setRentDate(sc.next());
            System.out.println("请输入租借天数:");
            book.setDay(sc.nextInt());
            book.setState(1);
            System.out.println("书名:" + book.getName()
                    + "作者" +  book.getAuthor() + "租借成功!");
        }
    }

    private static void deleteBook() {
        System.out.println("*********欢迎进入删除图书界面**********");
        display();
        System.out.println("请选择需要删除的图书:");
        int i;
        int choice = sc.nextInt()-1;
        Book book = bookArray[choice];
        if (book.getState() == 1){
            System.out.println("该图书尚未归还,无法删除");
        }else {
            System.out.println("书名:" + book.getName()
                    + "作者" +  book.getAuthor() + "删除成功!");
            for (i = choice; i < bookArray.length - 1 && bookArray[i] != null; i++) {
                bookArray[i] = bookArray[i+1];
            }
            bookArray[i -1] = null;
        }
    }

    private static void addBook() {
        System.out.println("*********欢迎进入添加图书界面**********");
        Book book = new Book();
        System.out.println("请输入书名:");
        book.setName(sc.next());
        System.out.println("请输入作者:");
        book.setAuthor(sc.next());
        System.out.println("请输入出版社:");
        book.setPublisher(sc.next());
        System.out.println("请输入定价:");
        book.setPrice(sc.nextDouble());
        System.out.println("请输日租金:");
        book.setRent(sc.nextDouble());
        System.out.println("请输入状态(未出租为0,已出租为1):");
        book.setState(sc.nextInt());
        for (int i = 0; i < bookArray.length; i++) {
            if (bookArray[i] == null){
                bookArray[i] = book;
                System.out.println("书名:" + book.getName()
                        + "作者" +  book.getAuthor() + "入库成功!");
                break;
            }
        }
    }

    private static void display() {
        System.out.println("*********欢迎进入图书列表查看界面**********");
        for (int i = 0; i < bookArray.length; i++) {
            System.out.println((i + 1) + "号图书:");
            if (bookArray[i] == null){
                System.out.println("空");
            }else{
                bookArray[i].show();
            }
        }
    }


}

DateUtil类

package BookSystem;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    private static final SimpleDateFormat SDF =
            new SimpleDateFormat("yyyy-MM-dd");

    public static String date2sting(Date date){
        return SDF.format(date);
    }

    public static Date sting2date(String dateStr){
        try {
            return SDF.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
}

你可能感兴趣的:(学习日记,java,学习,开发语言)