Mini Project Practice Questions 1DVD

topic

  • Develop a mini DVD manager for a video store to manage DVD discs, including the following functions: add DVD, view DVD, delete DVD, lend DVD, and return DVD.
  • design:
  • 1) DVD type (attributes: id, name, lendCount, lendDate, status) (default is unique for each DVD) lendCount field is incremented by 1 for each loan
  • 2) DVDDB class (used to save multiple DVDs)
  • 3) DVDManager class (provides manipulation functions), add DVD, view DVD, delete DVD, lend DVD, return DVD, DVD rankings (in descending order of the number of loans), exit the system
  • Effect pictureInsert picture description here

Use ArrayList to solve the problem

  • Classes and methods used:
  • (1) Java's inherent classes
  • java.util.ArrayList class add method get method remove method
  • java.text.SimpleDateFormat class format method (output the current date in the specified format "yyyy-MM-dd HH:mm:ss")
  • java.util.Scanner class next method and nextInt method
  • (2) Custom class
  • DVD class DVDDB class DVDManager class DVDManagerSystem class

DVD category

public class DVD {
    
    
    private  int id;
    private  String name;
    private  int lendCount;
    private  String lendDate;
    private  boolean status;//true表示可借,false表示已借出

    public DVD() {
    
    
    }
      //包含id和name的构造方法,默认初始化其他属性的状态(没有借出日期,因为DVD尚未被借出)
    public DVD(int id, String name) {
    
    
        this.id = id;
        //默认借出次数为0
        this.name = name;
        this.lendCount = 0;
        //默认借出状态为true,表示可借
        this.status= true;
    }
    //id name lendCount lendDate status的Getter and Setter 方法
    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 int getLendCount() {
    
    
        return lendCount;
    }

    public void setLendCount(int lendCount) {
    
    
        this.lendCount = lendCount;
    }

    public String getLendDate() {
    
    
        return lendDate;
    }

    public void setLendDate(String lendDate) {
    
    
        this.lendDate = lendDate;
    }

    public boolean isStatus() {
    
    
        return status;
    }

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

DVDDB class

import java.util.ArrayList;
public class DVDDB {
    
    
    //创建一个ArrayList集合,用于存储DVD类的对象
        private ArrayList<DVD> dvddb;

    public DVDDB(ArrayList<DVD> dvddb) {
    
    
        this.dvddb = dvddb;
    }

    public DVDDB() {
    
    
    }

    public ArrayList<DVD> getDvddb() {
    
    
        return dvddb;
    }

    public void setDvddb(ArrayList<DVD> dvddb) {
    
    
        this.dvddb = dvddb;
    }
}

DVDManager class

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class DVDManager {
    
    


    //添加DVD的方法
    public void addDVD(DVDDB dvddb,DVD dvd){
    
    
         dvddb.getDvddb().add(dvd);
    }
    //查看DVD的方法
    public void lookDVD(DVDDB dvddb){
    
    
        System.out.println("序号           "+"          状态"+"         名称"+"             借出日期");
        ArrayList<DVD> dvds = dvddb.getDvddb();
        for (int i = 0; i < dvds.size(); i++) {
    
    
          DVD dvd  = dvds.get(i);
          String lendStatus;
          if(dvd.isStatus()){
    
    
              lendStatus = "可借";
          }else{
    
    
              lendStatus = "已借出";
          }
            System.out.println(dvd.getId()+"                       "+lendStatus + "         "+dvd.getName()+"            "+dvd.getLendDate());
        }
    }
    //删除DVD的方法
    public void deleteDVD(DVDDB dvddb,int id){
    
    
        for (int i = 0; i < dvddb.getDvddb().size(); i++) {
    
    
            if(dvddb.getDvddb().get(i).getId() == id){
    
    
                dvddb.getDvddb().remove(i);
            }
        }
    }
    //借出DVD的方法
    public void sendDVD(DVDDB dvddb,int id){
    
    
        for (int i = 0; i < dvddb.getDvddb().size(); i++) {
    
    
            if(dvddb.getDvddb().get(i).getId() == id){
    
    
                dvddb.getDvddb().get(i).setStatus(false);
                dvddb.getDvddb().get(i).setLendCount(dvddb.getDvddb().get(i).getLendCount()+1);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = simpleDateFormat.format(new Date());
                dvddb.getDvddb().get(i).setLendDate(time);
            }
        }
    }
    //归还DVD的方法
    public void returnDVD(DVDDB dvddb,int id){
    
    
        for (int i = 0; i < dvddb.getDvddb().size(); i++) {
    
    
            if(dvddb.getDvddb().get(i).getId() == id){
    
    
                //true表示可借
                dvddb.getDvddb().get(i).setStatus(true);
                dvddb.getDvddb().get(i).setLendDate("无");
            }
        }

    }
    //DVD排行榜
    public void rankDVD(DVDDB dvddb){
    
    
        //设置一个临时的DVD数据库
        DVDDB temp = new DVDDB(new ArrayList<DVD> ());
        for (int i = 0; i < dvddb.getDvddb().size(); i++) {
    
    
            temp.getDvddb().add(dvddb.getDvddb().get(i));
        }
        int max = 0;
        for (int i = 0; i < temp.getDvddb().size(); i++) {
    
    
            max = i;
            for (int j = i+1; j < temp.getDvddb().size(); j++) {
    
    
                if(temp.getDvddb().get(max).getLendCount() < temp.getDvddb().get(j).getLendCount()){
    
    
                     max = j;
                }
            }
            if(i != max){
    
    
               DVD dvd = temp.getDvddb().get(i);
               temp.getDvddb().add(i,temp.getDvddb().get(max));
               temp.getDvddb().add(max,dvd);
            }
        }
        lookDVD(temp);
    }
}

Client (or test) DVDManagerSystem

import java.util.ArrayList;
import java.util.Scanner;
public class DVDManagerSystem {
    
    
    public static void main(String[] args) {
    
    
        DVDDB dvddb = new DVDDB(new ArrayList<DVD>());
        DVDManager dvdManager = new DVDManager();
        Scanner sc = new Scanner(System.in);
        while(true){
    
    
                  System.out.println("欢迎使用迷你DVD管理器");
                  System.out.println("------------------------------");
                  System.out.println("1.新增DVD");
                  System.out.println("2.查看DVD");
                  System.out.println("3.删除DVD");
                  System.out.println("4.借出DVD");
                  System.out.println("5.归还DVD");
                  System.out.println("6.DVD排行榜");
                  System.out.println("7.退出");
                  System.out.println("------------------------------");
                  System.out.println("请选择功能");
                   int i = sc.nextInt();
            switch (i){
    
    
                case 1:
                    System.out.println("请输入要添加的DVD的id号和名字(用回车分割输入)");
                    int id = sc.nextInt();
                    String name = sc.next();
                    DVD dvd = new DVD(id,name);
                    dvdManager.addDVD(dvddb,dvd);break;
                case 2:
                    dvdManager.lookDVD(dvddb);break;
                case 3:
                    System.out.println("请输入要删除的DVD的id号");
                    int dvdDelete = sc.nextInt();
                    dvdManager.deleteDVD(dvddb,dvdDelete);
                    break;
                case 4:
                    System.out.println("请输入要借的DVD的id号");
                    int dvdLend = sc.nextInt();
                    dvdManager.sendDVD(dvddb,dvdLend);break;
                case 5:
                    System.out.println("请输入要归还的DVD的id号");
                    int dvdReturn = sc.nextInt();
                    dvdManager.returnDVD(dvddb,dvdReturn);break;
                case 6:dvdManager.rankDVD(dvddb);break;
                case 7:
                    System.out.println("期待您的下次使用!再见!");return;
                default:
                    System.out.println("输入的数字不正确,请重新输入");
                  }

            while (true) {
    
    
                System.out.println("------------------------------");
                System.out.println("按0返回");
                int j = sc.nextInt();
                if (j == 0) {
    
    
                    break;
                } else {
    
    
                    System.out.println("输入数据错误,请重试");
                }
            }
              }

    }
}

Program running results

Insert picture description here

Use Vector to solve the problem

Use an array to solve the problem

Guess you like

Origin blog.csdn.net/hypertext123/article/details/115287464