<pre class="java" name="code">DVD的测试类
public class Dome2 {
public static void main(String[] args) {
Dome1 v=new Dome1();
v.Show1();
}
}
DVD所有方法
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Dome1 {
String[] name = new String[50];//存DVD的名字
int[] state = new int[50];//记录DVD的状态
String[] date = new String[50];//记录DVD的日期
int[] count = new int[50];//记录DVD的借出次数
Scanner input = new Scanner(System.in);
//目录
public void Show() {
System.out.println("欢迎使用迷你DVD管理系统");
System.out.println("------------------");
System.out.println("0.查看排行榜");
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.退出");
}
public void Show1() {
Show2();
boolean flag = true;
do {
Show();
System.out.print("请选择:");
int num = input.nextInt();
switch (num) {
case 0:
list();
break;
case 1:
Show3();
break;
case 2:
Show4();
break;
case 3:
sc();
break;
case 4:
jc();
break;
case 5:
gh();
break;
case 6:
System.out.println("谢谢使用!");
flag = false;
break;
}
} while (flag);
}
//初始化
public void Show2() {
name[0] = "罗马假日";
state[0] = 0;
date[0] = "2013-01-17";
name[1] = "风声鹤唳";
state[1] = 1;
name[2] = "浪漫满屋";
state[2] = 1;
}
显示DVD
public void Show4() {
for (int i = 0; i < name.length; i++) {
if (name[i] == null) {
break;
} else if (state[i] == 0) {
System.out.println((i + 1) + "\t已借出\t" + "<<" + name[i]
+ ">>\t" + date[i]);
} else if (state[i] == 1) {
System.out.println((i + 1) + "\t可借出\t" + "<<" + name[i] + ">>");
}
}
}
DVD的排行榜
public void list(){
//Show2();
String []newname=new String[50];
int []newcount=new int[50];
for (int i = 0; i < name.length; i++) {
newname[i]=name[i];
newcount[i]=count[i];
}
for (int i = 0; i < newname.length; i++) {
for (int j = i+1; j < newname.length; j++) {
if(newcount[i]>newcount[j]){
int temp=newcount[i];
newcount[i]=newcount[j];
newcount[j]=temp;
String temp1=newname[i];
newname[i]=newname[j];
newname[j]=temp1;
}
}
}
System.out.println("次数\t名称");
for (int i = newname.length-1;i>=0; i--) {
if(newname[i]!=null){
System.out.println(newcount[i]+"\t《"+newname[i]+"》");
}
}
}
新增DVD
public void Show3() {
System.out.print("请输入你新增的名字:");
String name1=input.next();
for (int i = 0; i <name.length; i++) {
if(name[i]==null){
name[i]=name1;
state[i]=1;
System.out.println("新增"+"<<"+name1+">>成功!");
break;
}else if(i==name.length-1){
System.out.println("失败!");
}
}
}
//删除信息
public void sc(){
System.out.println("-------------------------------------->删除DVD\n");
System.out.print("请输入删除的名字:");
String s=input.next();
for (int i = 0; i < name.length; i++) {
if(name[i]==null){
System.out.println("没有找到匹配信息!");
break;
}else if (name[i] != null && name[i].equals(s)
&& state[i] == 1) {
int j=i;
while(name[j+1]!=null){
name[j]=name[j+1];
state[j]=state[j+1];
date[j]=date[j+1];
j++;
}
name[j]=null;
date[j]=null;
System.out.println("删除"+"<<"+s+">>"+"成功~~");
break;
}else if(name[i]!=null
&&name[i].equalsIgnoreCase(s)&&state[i]==0){
System.out.println(name[i]+"为借出状态!");
}
}
}
//借出DVD
public void jc(){
System.out.println("-------------------------------------->借出DVD\n");
System.out.print("请输入DVD的名称:");
String name1=input.next();
for (int i = 0; i <name.length; i++) {
if(name[i]==null){
System.out.println("没有找到匹配信息!");
break;
}else if(name[i].equals(name1)&&state[i]==1){
System.out.print("请输入借出的年月日:");
date[i]=input.next();
System.out.println("《"+name1+"》"+"借出成功~~");
state[i]=0;
count[i]++;
break;
}else if(name[i].equals(name1)&&state[i]==0){
System.out.println("不好意思,《"+name1+"》已被借出!");
break;
}
}
}
//归还DVD
public void gh(){
System.out.println("-------------------------------------->归还DVD\n");
System.out.print("请输入DVD的名称:");
String name1=input.next();
for (int i = 0; i <= name.length; i++) {
if(name[i]==null){
System.out.println("没有找到匹配信息~~");
break;
}else if (name[i].equals(name1) && state[i] == 0) {
System.out.print("请输入归还的日期:");
String redate = input.next();
System.out.println(name1 + "归还成功!");
System.out.println("借出日期为:" + date[i]);
System.out.println("归还日期为:" + redate);
String d1 = date[i];
String d2 = redate;
long day = charge(d2, d1);
System.out.println("应付租金:" + day + "元");
date[i] = redate;
state[i] = 1;
break;
}else if(name[i].equals(name1)&&state[i]==1){
System.out.println(name1+"可借DVD,输入错误。");
break;
}
}
}
//计算时期差
public long charge(String date1,String date2){
long charge=0;
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d1 =sd.parse(date1);
Date d2 =sd.parse(date2);
charge = (d1.getTime() - d2.getTime())/ (24 * 60 * 60 * 1000);
} catch (ParseException e) {
e.printStackTrace();
}
return charge;
}
}