文档库

最新最全的文档下载
当前位置:文档库 > java-DVD管理编程

java-DVD管理编程

package DVD管理器;

import java.text.*;

import java.util.Date;

import java.util.Scanner;

public class DVDSet {

public String[] name=new String[50]; //存储DVD名称

public int[] stade=new int[50];//存储借出状态0:已借出1:可借

public String[] date=new String[50];//存储借出日期

public int[] count=new int[50];//存储借出次数

}

class DVDMgr {

/**

* @param args

* 主菜单输出方法、借出/新增/查看/删除/借出/归还DVD方法、排行榜输出方法

*/

static DVDSet dvd=new DVDSet(); //创建DVD对象

static Scanner input=new Scanner(System.in);

public static void startMenu(){ //主菜单输出并根据输入判断要实现的功能

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. 退出");

System.out.print("--------------------------------------\n");

System.out.print("请选择:");

int choice=input.nextInt();

switch (choice) {

case 0:

list(); //调用借出排行榜的方法输出排行榜

break;

case 1:

add(); //调用新增DVD的方法

break;

case 2:

search(); //调用查看DVD的方法

break;

case 3:

delete(); //调用删除DVD的方法

break;

case 4:

lend(); //调用借出DVD的方法

break;

case 5:

returnDVD(); //调用归还DVD的方法

break;

case 6:

System.out.println("谢谢使用,欢迎再来"); //退出

break;

}

}

public static void returnMain(){ //输入0返回主菜单方法System.out.print("输入0返回:");

if (input.nextInt()==0) {

startMenu();

}else{

System.out.println("输入错误,请重新输入:");

returnMain();

}

}

public static void add(){ //实现新增DVD的方法System.out.println("--->新增DVD");

System.out.print("请输入DVD名字:");

String name=input.next();

for (int i = 0; i < http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html.length; i++) {

if (http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i]==null) {

http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i]=name;

dvd.stade[i]=1;

System.out.println("新增"+name+"成功!");

break;

}

}

System.out.println("********************************");

returnMain();

}

public static void search(){ //实现查看dvd方法System.out.println("--->查看DVD");

System.out.println("序号\t状态\t名称\t借出日期");

for (int i = 1; i

if (http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i]==null) {

break;

}else{

if (dvd.stade[i]==0) {

System.out.println(i+"\t已借出\t"+http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i]+"\t"+dvd.date[i]);

}else{

System.out.println(i+"\t可借\t"+http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i]+"\t"+dvd.date[i]);

}

}

}

System.out.println("********************************");

returnMain();

}

public static void delete(){ //实现删除DVD方法System.out.println("--->删除DVD");

System.out.print("请输入DVD名称:");

String name=input.next();

for (int i = 0; i

if (http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i]!=null&http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i].equalsIgnoreCase(name)&&dvd.stade[i]==1) { int j=i;

while (http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[j+1]!=null) {

http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[j]=http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[j+1];

dvd.stade[j]=dvd.stade[j+1];

dvd.date[j]=dvd.date[j+1];

j++;

}

http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html=null;

dvd.stade=null;

http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html=null;

System.out.println("删除《"+name+"》成功!");

}if(http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i]!=null&http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i].equalsIgnoreCase(name)&&dvd.stade[i]==0){

System.out.println("《"+name+"》"+"已经借出,不可以删除!");

break;

}else{

System.out.println("没有该DVD信息!");

}

}

}

public static void lend(){ //实现借出DVD方法System.out.println("--->借出DVD");

System.out.println("请输入要借出的DVD名称:");

String want=input.next();

for (int i = 0; i

if (http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i].equals(want)&&dvd.stade[i]==1) {

dvd.stade[i]=0;

System.out.println("请输入借出日期(格式为:年-月-日):");

dvd.date[i]=input.next();

System.out.println("借出成功!");

dvd.count[i]++;

break;

}if (http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i].equals(want)&&dvd.stade[i]==0) {

System.out.println("该DVD已被借出。");

break;

}

}

System.out.println("*******************************");

returnMain();

}

public static void returnDVD(){ //实现归还DVD并计算佣金long yongjin=0;

System.out.println("请输入DVD名称:");

String want=input.next();

for (int i = 0; i < http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html.length; i++) {

if(http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i].equals(want)&&dvd.stade[i]==0){

dvd.stade[i]=1;

System.out.println("请输入归还日期(格式为:年-月-日):");

String redate=input.next();

yongjin=charge(dvd.date[i],redate);

System.out.println(want+"归还成功!");

System.out.println("借出日期为:"+dvd.date);

System.out.println("归还日期为:"+redate);

System.out.println("应付租金:"+yongjin+"元");

break;

}if (http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i].equals(want)&&dvd.stade[i]==1) {

System.out.println("该DVD未被借出,无法继续操作!");

break;

}else{

System.out.println("未找到该DVD信息!");

}

}

System.out.println("****************************");

returnMain();

}

public static 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=(d2.getTime()-d1.getTime())/(24*60*60*1000);

} catch (ParseException e) {

e.printStackTrace();

}

return charge;

}

public static void list(){ //实现排行榜输出功能String [] newname=new String[50];

int [] newcount=new int[50];

for (int i = 0; i < http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html.length; i++) {

newname[i]=http://www.wendangku.net/doc/e278a1fbbed5b9f3f80f1c58.html[i];

newcount[i]=dvd.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 tempname=newname[i];

newname[i]=newname[j];

newname[j]=tempname;

}

}

}

System.out.println("排行榜:");

System.out.println("***************************");

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]+"》");

}

}

System.out.println("*******************************");

returnMain();

}

}

/*public class Test {

/**

* @param args

* 测试类运行

@SuppressWarnings("static-access")

public static void main(String[] args) {

DVDMgr dvd=new DVDMgr(); //创建一个DVDMgr对象

dvd.startMenu(); //调用主菜单输出方法}*/