<pre name="code" class="java">
<pre name="code" class="java">public class Start {public static void main(String[] args) {DVDMgr dm = new DVDMgr();dm.initial();dm.startMenu();}}
public class DVDSet {
String[] name = new String[50];// 存储DVD名称
int[] state = new int[50];// 存储DVD借出状态
String[] date = new String[50];// 存储DVD借出日期
int []count = new int[100];//储存借出次数
}
import java.util.Date;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Scanner;public class DVDMgr {DVDSet dvd = new DVDSet();public void initial() {dvd.name[0] = "罗马假日";dvd.state[0] = 0;// 已借出dvd.date[0] = "2013-7-1";dvd.count[0] = 1;dvd.name[1] = "风声鹤唳";dvd.state[1] = 1;// 可借dvd.name[2] = "浪漫满屋";dvd.state[2] = 1;dvd.name[3] = "李冰的后花园";dvd.state[3] = 1;dvd.count[3] = 0;}// 排行榜public void list() {String newname[] = new String[100];int newcount[] = new int[100];newname = dvd.name;newcount = dvd.count;for (int i = 0; i < newname.length - 1; i++) {for (int j = 0; j < newname.length - 1 - i; j++) {if (newcount[i] > newcount[j]) {int x = newcount[i];newcount[i] = newcount[j];newcount[j] = x;String y = newname[i];newname[i] = newname[j];newname[j] = y;}}}System.out.println("次数\t\t名称");for (int i = 0; i < newname.length; i++) {if (newname[i] != null) {System.out.println(newcount[i] + "\t\t" + "<<" + newname[i]+ ">>");}}}public void startMenu() {Scanner input = new Scanner(System.in);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.println("--------------------------");System.out.println("\n请选择:");int choice = input.nextInt();switch (choice) {case 0:System.out.println("--->排行榜");list();System.out.println("************************");returnMain();break;case 1:System.out.println("--->新增DVD");add();System.out.println("************************");returnMain();break;case 2:System.out.println("--->查看DVD");search();System.out.println("*************************");returnMain();break;case 3:System.out.println("--->删除DVD");delete();System.out.println("*************************");returnMain();break;case 4:System.out.println("--->借出DVD");lend();System.out.println("*************************");returnMain();break;case 5:System.out.println("--->归还DVD");returnDvd();System.out.println("*************************");returnMain();break;case 6:System.out.println("谢谢使用!");break;default:System.out.println("输入错误!");break;}}public void returnMain() {Scanner input = new Scanner(System.in);System.out.println("输入0返回:");if (input.nextInt() == 0) {startMenu();} else {System.out.println("输入错误,异常终止");}}// 查看public void search() {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已借出\t" + "<<" + dvd.name[i]+ ">>\t" + dvd.date[i]);} else if (dvd.state[i] == 1) {System.out.println((i + 1) + "\t可借\t" + "<<" + dvd.name[i]+ ">>"+"\t");}}}// 新增public void add() {Scanner input = new Scanner(System.in);System.out.println("\n请输入DVD名称");String name1 = input.next();for (int i = 0; i < dvd.name.length; i++) {if (dvd.name[i] == null) {dvd.name[i] = name1;dvd.date[i] = "";dvd.state[i] = 1;System.out.println("新增" + name1 + "成功!");break;}}}// 删除public void delete() {boolean con = false;Scanner input = new Scanner(System.in);System.out.println("请输入DVD名称:");String name2 = input.next();for (int i = 0; i < dvd.name.length; i++) {if (dvd.name[i] != null && dvd.name[i].equalsIgnoreCase(name2)&& 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.date[j] = dvd.date[j + 1];j++;}dvd.name[j] = null;dvd.date[j] = null;System.out.println("删除<<" + name2 + ">>成功!");con = true;break;} else if (dvd.name[i] != null&& dvd.name[i].equalsIgnoreCase(name2) && dvd.state[i] == 0) {System.out.println("<<" + name2 + ">>为借出状态,不能删除!");con = true;break;}}if (con==false) {System.out.println("没有找到要删除的DVD");}}// 借出public void lend() {Scanner input = new Scanner(System.in);System.out.println("请输入DVD名称:");String name3 = input.next();for (int i = 0; i < dvd.name.length; i++) {if (dvd.name[i] == null) {System.out.println("没有找到匹配信息!");break;} else if (dvd.name[i].equals(name3) && dvd.state[i] == 1) {System.out.println("请输入借出日期(年-月-日):");dvd.date[i] = input.next();System.out.println("<<" + name3 + ">>借出成功!");dvd.state[i] = 0;dvd.count[i]++;break;} else if (dvd.name[i].equals(name3) && dvd.state[i] == 0) {System.out.println("不好意思<<" + name3 + ">>已被借出!");break;}}}// 归还public void returnDvd() {Scanner input = new Scanner(System.in);System.out.println("请输入DVD名称:");String name4 = input.next();double money = 0;for (int i = 0; i < dvd.name.length; i++) {if (dvd.name[i] == null) {System.out.println("没有找到匹配的信息!");break;} else if (dvd.name[i].equals(name4) && dvd.state[i] == 0) {System.out.println("请输入归还日期(年-月-日):");String year = input.next();money = charge(dvd.date[i], year);System.out.println("\n<<" + name4 + ">>归还成功!");System.out.println("借出日期为:" + dvd.date[i]);System.out.println("还书日期为:" + year);System.out.println("应付租金(1元/天)" + money);dvd.state[i]=1;dvd.date[i]="";break;} else if (dvd.name[i].equals(name4) && dvd.state[i] == 1) {System.out.println("<<" + name4 + ">>没有被借出,无法归还");}}}public long charge(String dstr1, String dstr2) {long charge = 0;SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");try {Date d1 = sd.parse(dstr1);Date d2 = sd.parse(dstr2);charge = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000);} catch (ParseException e) {e.printStackTrace();}return charge;}}