トピック
- 次の機能を含む、DVDディスクを管理するためのビデオストア用のミニDVDマネージャーを開発します:DVDの追加、DVDの表示、DVDの削除、DVDの貸し出し、およびDVDの返却。
- 設計:
- 1)DVDカテゴリ(属性:id、name、lendCount、lendDate、status)(デフォルトはDVDごとに一意)lendCountフィールドはローンごとに1ずつ増加します
- 2)DVDDBクラス(複数のDVDを保存するために使用)
- 3)DVDManagerクラス(操作機能を提供)、DVDの追加、DVDの表示、DVDの削除、DVDの貸し出し、DVDの返却、DVDランキングリスト(貸し出し数の降順)、システムの終了
- エフェクト画像
ArrayListを使用して問題を解決します
- 使用されるクラスとメソッド:
- (1)Java固有のクラス
- java.util.ArrayListクラスaddメソッドgetメソッドremoveメソッド
- java.text.SimpleDateFormatクラスフォーマットメソッド(現在の日付を指定されたフォーマット「yyyy-MM-ddHH:mm:ss」で出力します)
- java.util.ScannerクラスのnextメソッドとnextIntメソッド
- (2)カスタムクラス
- DVDクラスDVDDBクラスDVDManagerクラスDVDManagerSystemクラス
DVDカテゴリー
public class DVD {
private int id;
private String name;
private int lendCount;
private String lendDate;
private boolean status;
public DVD() {
}
public DVD(int id, String name) {
this.id = id;
this.name = name;
this.lendCount = 0;
this.status= true;
}
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クラス
import java.util.ArrayList;
public class DVDDB {
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クラス
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class DVDManager {
public void addDVD(DVDDB dvddb,DVD dvd){
dvddb.getDvddb().add(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());
}
}
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);
}
}
}
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);
}
}
}
public void returnDVD(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(true);
dvddb.getDvddb().get(i).setLendDate("无");
}
}
}
public void rankDVD(DVDDB dvddb){
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);
}
}
クライアント(またはテスト)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("输入数据错误,请重试");
}
}
}
}
}
プログラム実行結果
ベクトルを使用して問題を解決する
配列を使用して問題を解決する