import java.text.SimpleDateFormat;
import java.util.Scanner;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
public class DVDMag {
DVDSet dvd = new DVDSet();
Scanner input = new Scanner(System.in);
// 初始化3张dDVD信息
public void initial() {
dvd.name[0] = "罗马假日";
dvd.state[0] = 0;
dvd.count[0]=15;//借出第一张DVD次数
dvd.data[0] = "2010-07-01";
dvd.name[1] = "风声鹤唳";
dvd.state[1] = 1;
dvd.count[1]=12;
dvd.name[2] = "浪漫满屋";
dvd.state[2] = 1;
dvd.count[2]=30;
}
/**
* 主菜单
*
*/
public void startMenu() {
System.out.println("欢迎使用迷你DVD管理系统");
System.out.println("-------------------------------");
System.out
.println("0.退出排行榜\n1.新增DVD\n2.查看DVD\n3.删除DVD\n4.借出DVD\n5.归还DVD\n6.退 出\n");
System.out.println("---------------------------------");
System.out.println("请选择:");
int choice = input.nextInt();
//实现菜单切换
switch (choice) {
case 0:
list();// 排行榜
returnMain();// 返回主菜单
break;
case 1:
add();//新增
returnMain();
break;
case 2:
search();//查看
returnMain();
break;
case 3:
delete();//删除
returnMain();
break;
case 4:
lend();//借出
returnMain();
break;
case 5:
returnDvd();
returnMain();
break;
case 6:
System.out.println("谢谢使用!");
break;
default:
System.out.println("请输入正确的数字:");
returnMain();
break;
}
}
/**
* 实现返回主菜单
*
*/
public void returnMain() {
System.out.println("*********************************");
System.out.println("输入0返回:");
if (input.nextInt() == 0) {
startMenu();
} else {
System.out.println("输入错误,异常终止!");
}
}
/**
* 实现查看DVD主菜单
*
*/
public void search() {
System.out.println("--->查看DVD\n");
System.out.println("序号"+"\t状态\t"+"名称"+"\t\t"+"借出时间");
for (int i = 0; i < dvd.name.length; i++) {
if (dvd.name[i] == null) {
break;
} else if (dvd.state[i] == 0) {
System.out.println((i + 1) + "\t已借出" + "<<" + dvd.name[i]
+ ">>\t" + dvd.data[i]);
} else if (dvd.state[i] == 1) {
System.out.println((i + 1) + "\t可借\t" + "<<" + dvd.name[i]
+ ">>");
}
}
}
/**
* 实现新增DVD信息
*
*/
public void add(){
System.out.println("--->新增DVD");
for(int i=0;i<dvd.name.length;i++){
if(dvd.name[i]==null){
dvd.state[i]=1;//新增为可借状态
System.out.println("请输入DVD名称:");
dvd.name[i]=input.next();
System.out.println("新增<<"+dvd.name[i]+">>成功!");
break;
}
}
}
//删除DVD信息
public void delete() {
System.out.println("-->删除DVD");
boolean flag;
System.out.println("请输入DVD名称:");
String name = input.next();
for (int i = 0; i < dvd.name.length; i++) {
if (dvd.name[i] != null && dvd.name[i].equalsIgnoreCase(name)
&& dvd.state[i] == 1) {
int j = i;
while (dvd.name[j + 1] != null) {
dvd.name[j] = dvd.name[j + 1];
dvd.state[j] = dvd.state[j + 1];
dvd.data[j] = dvd.data[j + 1];
j++;
}
// 最后一个不为空的元素置空
dvd.name[j] = null;
dvd.data[j] = null;
System.out.println("删除《" + name + "》成功");
flag = true;
break;
} else if (dvd.name[i] != null
&& dvd.name[i].equalsIgnoreCase(name) && dvd.state[i] == 0) {
System.out.println("《" + name + "》为借出状态,不能删除!");
flag = true;
break;
}
}
}
/**
* 实现DVD的业务处理
*
*/
public void lend() {
System.out.println("-->借出DVD");
System.out.println("请输入DVD名称:");
String want = input.next();
for (int i = 0; i < dvd.name.length; i++) {
if (dvd.name[i] == null) {
System.out.println("没有找到匹配信息!");
break;
} else if (dvd.name.equals(want) && dvd.state[i] == 1) {
System.out.println("找到匹配可借!");
break;
} else if (dvd.name[i].equals(want) && dvd.state[i] == 0) {
System.out.println("找到匹配信息已被借出!");
}
}
}
public void returnDvd() {
/**
* 计算日期差
*
* @param dstr1
* 第一个日期
* @Param dstr2
* 第二个日期 @ return charge 日期差
*/
System.out.println("归还DVD\n");
System.out.println("请输入DVD名称:");
String name=input.next();
System.out.println("归还日期(-年-月-日)为:");
String data=input.next();
//遍历数组查找
for(int i=0;i<dvd.name.length;i++){
if(dvd.name[i]!=null&&name.equalsIgnoreCase(dvd.name[i])&&dvd.state[i]==0){
System.out.println("归还<<"+name+">>成功!");
dvd.state[i]=1;//归还后可在被借出
System.out.println("接触日期:"+dvd.data[i]);
System.out.println("归还日期:"+data);
try {
System.out.println("应付租金(元)"+charge(dvd.data[i],data+"\n"));
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}else if(dvd.name[i]==null){
System.out.println("归还失败!");
break;
}
}
}
public long charge(String dstr1, String dstr2) throws java.text.ParseException {
long charge = 0;
SimpleDateFormat sd = new SimpleDateFormat("YYYY-MM-dd");
try {
java.util.Date d1 = sd.parse(dstr1);
java.util.Date d2 = sd.parse(dstr2);
charge = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000);
} catch (ParseException e) {
e.printStackTrace();
}
return charge;
}
/**
* 实现借出排行榜
*
*/
public void list() {
System.out.println("--->排行榜\n");
System.out.println("************************************");
System.out.println("次数" + "名称");
int[] newcount = new int[50];// 创建新数组
String[] newname = new String[50];
for (int i = 0; i < dvd.count.length; i++) {
newcount[i] = dvd.count[i];// 把count赋值给新数组
newname[i] = dvd.name[i];// 把name赋值给新数组
}
/**
* 排序
*/
for (int i = 0; i < newname.length; i++) {// 比较数组中每个元素的借出次数
for (int j = i + 1; j < newname.length - 1; j++) {
if (newcount[j] > newcount[i]) {// 如果借出的多,排序靠前
// 根据借出的次数交换位置
int temp = newcount[i];
newcount[i] = newcount[j];
newcount[j] = temp;
// 交换DVD名称位置
String remp = newname[i];
newname[i] = newname[j];
newname[j] = remp;
}
}
}
/**
* 输出排行榜
*/
for(int i=0;i<newcount.length;i++){
if(newname[i]==null){
break;
}else{
System.out.println(newcount[i]+"\t<<"+newname[i]+">>");
}
}
}
}
评论0