迷你DVD

public class DVD {
    private int ID;//id
    private String status;//状态
    private String name;//名称
    private String date;//借出日期
    private int count;//次数

    @Override
    public String toString() {
        return "DVD{" +
                "ID=" + ID +
                ", status='" + status + '\'' +
                ", name='" + name + '\'' +
                ", date='" + date + '\'' +
                ", count=" + count +
                '}';
    }

    public DVD() {
    }

    public DVD(int ID, String status, String name, String date, int count) {
        this.ID = ID;
        this.status = status;
        this.name = name;
        this.date = date;
        this.count = count;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getName() {
        return name;
    }

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

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

package com.zhiyou1002;

import jdk.nashorn.internal.ir.IfNode;

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

/**
 * @author shkstart
 * 工厂
 */
public class Factories {

    DVD[] dvds = new DVD[20];
    

    /**
     * 利用构造器给DVD初始化
     */
    public Factories() {
        dvds[0] = new DVD(1, "以借出", "小王子", "2020年10月07日", 999);
        dvds[1] = new DVD(2, "可借", "朝花夕拾", "\t\t\t", 999);
        dvds[2] = new DVD(3, "可借", "罗马假日", "\t\t\t", 1);
        dvds[3] = new DVD(4, "可借", "风声鹤唳", "\t\t\t", 1);
        dvds[4] = new DVD(5, "可借", "浪漫满屋", "\t\t\t", 1);
    }


    /**
     * 查看
     */
    public void show() {
        System.out.println("序号\t状态\t\t\t名称\t\t借出日期\t\t\t借出次数");
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] != null) {
                System.out.println(dvds[i].getID() + "\t" + dvds[i].getStatus() + "\t\t" + dvds[i].getName() + "\t\t" + dvds[i].getDate() + "\t" + dvds[i].getCount());
            }
        }
    }


    /**
     * 判断ID是否重复
     *
     * @param id
     */
    public boolean add_1(int id) {
        boolean find = false;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] != null) {
                if (dvds[i].getID() == id) {
                    find = false;
                    break;
                } else {
                    //确定id不重复
                    find = true;
                }
            }
        }
        return find;
    }

    /**
     * 判断书是否重复
     *
     * @param name
     * @return
     */
    public boolean add_2(String name) {
        boolean find = false;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] != null) {
                if (dvds[i].getName().equals(name)) {
                    find = false;
                    break;
                } else {
                    //确定id不重复
                    find = true;
                }
            }
        }
        return find;
    }

    /**
     * 新增
     *
     * @param id
     * @param name
     */
    public void add(int id, String name) {
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] == null) {
                dvds[i] = new DVD(id, "可借", name, "\t\t\t", 0);
                System.out.println("新增图书成功!");
                break;
            }
        }
    }

    /**
     * 借出
     *
     * @param name
     */
    public void lend(String name) {
        boolean find = false;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] != null) {
                //判断是否有这本书
                if (dvds[i].getName().equals(name)) {//找到书籍
                    System.out.println("找到此书");
                    find = true;
                    //判断是否可借
                    if (dvds[i].getStatus().equals("可借")) {

                        //借出次数加1
                        dvds[i].setCount(dvds[i].getCount() + 1);
                        //设置日期
                        SimpleDateFormat time = new SimpleDateFormat("yyyy年MM月dd日");
                        Date date = new Date();
                        String format = time.format(date);
                        dvds[i].setDate(format);
                        //设置状态
                        dvds[i].setStatus("以借出");
                        System.out.println("成功借出!");
                    } else {
                        System.out.println("此书以借出");
                    }
                }
            }
        }
        if (find) {

        } else {
            System.out.println("未找到!");
        }
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(int id) {
        boolean find = false;
        int index = -1;
        for (int i = 0; i < dvds.length; i++) {

            if (dvds[i] != null) {
                if (dvds[i].getID() == id) {
//                    dvds[i] = null;
//                    index = i;
                    index = i;
                    find = true;
                }
            }
            //前一个向前移动1为
            if (find && index != -1&&index<dvds.length-1) {
                dvds[index] = dvds[index + 1];
                index++;
            }
        }
        if (find) {
            System.out.println("删除成功!");
        } else {
            System.out.println("删除失败!");
        }
    }

    /**
     * 归还
     */
    public void returns(String name) {
        boolean find = false;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i].getName().equals(name)) {
                if (dvds[i].getStatus().equals("以借出")) {

                    dvds[i].setDate("\t\t\t");//更改日期
                    dvds[i].setStatus("可借");//更改状态

                    find = true;
                    break;
                } else {
                    System.out.println("此书已经存在!");
                    break;
                }

            }
        }
        if (find) {
            System.out.println("归还成功!");
        } else {
            System.out.println("输入错误!");
        }
    }

}
package com.zhiyou1002;

import java.util.Scanner;

/**
 * @author shkstart
 * 测试类
 */
public class Text {
    public static void main(String[] args) {
        Factories factories = new Factories();
        Scanner sc = new Scanner(System.in);
        dd:
        while (true) {
            System.out.println("欢迎使用迷你DVD管理器");
            System.out.println("************************************************");
            System.out.println("<1>新增DVD " + "<2>查看DVD " + "<3>删除DVD " + "<4>借出DVD " + "<5>归还DVD " + "<6>退出DVD ");
            System.out.println("************************************************");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    System.out.println("➴➵新增DVD");
                    System.out.println("请输DVD的ID:");
                    int id = sc.nextInt();
                    boolean b = factories.add_1(id);
//                    System.out.println("b = " + b);
                    if (b) {
                        System.out.println("请输DVD名称:");
                        String name = sc.next();
                        boolean b1 = factories.add_2(name);
//                        System.out.println("b1 = " + b1);
                        if (b1) {
                            factories.add(id, name);
                        } else {
                            System.out.println("此书已经存在!");
                        }

                    } else {
                        System.out.println("ID已经存在");
                    }


                    break;
                case 2:
                    System.out.println("➶➷查看DVD");
                    factories.show();

                    break;
                case 3:
                    System.out.println("➶➷删除DVD");
                    System.out.println("请输入要删除的DVD的ID:");
                    int id2 = sc.nextInt();
                    factories.delete(id2);

                    break;
                case 4:
                    System.out.println("➶➷借出DVD");
                    System.out.println("请输入你要借出的DVD名字:");
                    String name2 = sc.next();
                    factories.lend(name2);

                    break;
                case 5:
                    System.out.println("➶➷归还DVD");
                    System.out.println("请输入要归还的DVD的名字:");
                    String name3 = sc.next();
                    factories.returns(name3);

                    break;
                case 6:

                    break dd;
                default:
                    System.out.println("输入错误!");

            }

        }
        System.out.println("谢谢惠顾!");

    }
}
posted @ 2020-12-05 19:29  MikiKawai  阅读(88)  评论(0编辑  收藏  举报