DVD管理器集合版

时间:2024-03-26 14:06:14

利用所学的集合写出的DVD管理系统,运用到了所学到集合基础。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet; import org.omg.CORBA.PUBLIC_MEMBER;
import org.w3c.dom.ls.LSInput;
class DVDSetcompare implements Comparator{
public int compare(Object o1, Object o2) {
DVDSet d1 = (DVDSet)o1;
DVDSet d2 = (DVDSet)o2;
return d1.count-d2.count;
}
} class DVDSet {
String name;//DVD的名字
int state ;//借出状态:0已借出/1:可借
String date ;//DVD借出日期
int count;//借出次数 public DVDSet(String name,int state, String date, int count){
this.name = name;
this.state = state;
this.date = date;
this.count = count;
} public String toString() {
// TODO Auto-generated method stub
return this.name;
} }
class DVDMgr{
//创建集合用来存贮对象的信息
List list = new ArrayList<>(); int pas;//记录每次借出的记录次数 private DVDSet DVDSet;
public void initial() { list.add(new DVDSet("罗马假日", 0, "2016-2-1", 30));
list.add(new DVDSet("风声鹤唳", 1, null, 50));
list.add(new DVDSet("浪漫满屋", 1, null, 1)); }
public void startMenu(){
System.out.println("**************^^^^^^^^^^^*****************");
System.out.println("欢迎使用迷你DVD管理器");
String[] book = {"1.新增DVD","2.查看DVD","3.删除DVD","4.借出DVD","5.归还DVD","6.排行榜","7.退出"};
for(int i = 0; i<book.length;i++){ System.out.println(book[i]);
} System.out.println("*******************************");
System.out.print("请选择:");
Scanner scan = new Scanner(System.in);
int chioce = scan.nextInt();
switch(chioce){
case 1:
addDVD();
break;
case 2:
lookupDVD();
break;
case 3:
deleteDVD();
break;
case 4:
lendDVD();
break;
case 5:
returnDVD();
break;
case 6:
listDVD();
break;
case 7:
System.out.println("感谢使用,欢迎您下次使用!!!!!");
break;
default:System.out.println("输入错误,异常终止"); }
} //*************查看开始***********
public void lookupDVD(){
System.out.println("--->查看DVD\n"); System.out.println("序号\t状态\t名称\t\t借出日期");
for(int i = 0;i<list.size();i++){
DVDSet dvd= (DVDSet)list.get(i);
if (dvd.state==0) {
System.out.println(i+ "\t" + "已借出" + "\t" + dvd.name+ "\t\t" + dvd.date);
}
else if (dvd.state==1) {
System.out.println(i + "\t" + "可借" + "\t" + dvd.name+ "\t\t");
} } returnMain();
}
//**********返回函数块*******
public void returnMain(){
Scanner input = new Scanner(System.in);
System.out.print("输入0返回:");
if(input.nextInt()==0){
startMenu();
}else{ System.out.println("输入错误,异常终止,请中重新输入");
returnMain();
} }
//******************返回函数块结束********* //*************实现增加DVD开始*************
public void addDVD(){
Scanner input = new Scanner(System.in);
System.out.println("***************>>>>新增DVD\n");
System.out.println("请输入DVD名称");
String name1 = input.next();
list.add(new DVDSet(name1, 1, null, 0));
System.out.println(name1+"新增成功");
returnMain();
} //*************实现增加DVD结束*************
//*************实现删除DVD开始*************
public void deleteDVD(){
System.out.println("***************>>>>删除DVD\n");
System.out.print("请输入你要删除DVD的名称:");
Scanner input =new Scanner(System.in);
String name = input.next();
Boolean boolean1 = false;
for (int i = 0; i < list.size(); i++) {
DVDSet dvdSet = (DVDSet)list.get(i); if ((int)dvdSet.state==0&&dvdSet.name.equals(name)) {
System.out.println(dvdSet.name+"已借出无法删除");
deleteDVD();
}
else if(dvdSet.name.equals(name)&&dvdSet.state==1){
list.remove(i);
boolean1= true;
} } if (boolean1) {
System.err.println(name+"删除成功"); }else {
System.err.println("没有这个DVD,请重亲输入");
deleteDVD();
}
System.out.println();
returnMain(); }
//*************实现删除DVD结束************* //*************实现借出DVD开始*************
public void lendDVD(){
Scanner input = new Scanner(System.in);
System.out.println("***************>>>>借出DVD\n");
System.out.println("请输入你要借的DVD的名称"); String lendname = input.next();
boolean b = false;
for (int i = 0; i < list.size(); i++) {
DVDSet dvdSet = (DVDSet)list.get(i);
if(dvdSet.name.equals(lendname)&&dvdSet.state==1){
System.out.println("请输入借出的日期(年-月-日)");
String date = input.next();
list.set(i, new DVDSet(lendname, 0, date,pas++ ));
b=true; }else if(dvdSet.name.equals(lendname)&&(int)dvdSet.state==0){
System.out.println("<<"+lendname+">>已经被借出");
returnMain();
}
} if (b==true) {
System.out.println("借出<<"+lendname+">>成功!");
}else {
System.out.println("找不到该DVD比配的信息");
returnMain();
} returnMain();
}
//*************实现借出DVD结束************* //*************实现归还DVD结束*************
public void returnDVD(){
Scanner input = new Scanner(System.in);
System.out.println("***************>>>>归还DVD\n");
System.out.println("请输入你要归还的DVD的名称");
String returndvd = input.next();
for(int i =0 ;i<list.size();i++){
DVDSet dvdSet = (DVDSet)list.get(i);
if (returndvd.equals(dvdSet.name)&&dvdSet.state==0) {
System.out.println("请输入归还的DVD的日期");
String date = input.next();
System.out.println(returndvd+"归还成功!");
System.out.println("借出日期为:" + dvdSet.date);
System.out.println("归还日期为:" + date); //计算归还的本金
String d1 =dvdSet.date;//第一个日期
String d2 = date;//第二个日期
long charge = 0;
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
try{
Date D1 = sd.parse(d1);
Date D2 = sd.parse(d2);
charge = ((D2.getTime()-D1.getTime())/(24*60*60*1000))/10; }catch(ParseException e){
e.printStackTrace(); }
list.set(i, new DVDSet(returndvd,1, null, 1)); System.out.println("应付租金:" + charge + "元");
break;
}else if(dvdSet.name.equals(returndvd)&&dvdSet.state==1) {
System.out.println("这DVD没有被借出,无需归还");
break;
}/*else if (dvdSet.name!=returndvd) {
System.out.println("这DVD没有");
break;
} */ } returnMain();
}
//*************实现归还DVD结束************* //*************实现排行榜DVD开始*************
public void listDVD(){
System.out.println("欢迎进入Dvd排行榜");
TreeSet set = new TreeSet<>(new DVDSetcompare());
for (int i = 0; i < list.size(); i++) {
DVDSet dvdSet = (DVDSet)list.get(i);
set.add(dvdSet);
}
Object[] arr = set.toArray();
int num = 0;
for (int i = set.size()-1; i >=0; i--) {
num++;
System.out.println("第"+num+"名"+"\t"+arr[i]);
}
returnMain(); } } public class Demo { /**
* @param args
*/
public static void main(String[] args) {
DVDMgr dm = new DVDMgr();
dm.initial();
dm.startMenu();
} }