package chapter5;
import java.util.*;
public class MiniDVD {
public static void main(String[] args){
//扫描器
Scanner input = new Scanner(System.in);
//重要参数及初始化
//Create four arrays with length of 7
//定义四个数组长度为7
int date[] = new int[7]; //borrow date
int count[] = new int[7]; //total count
String name[] =new String[7]; //name of DVDs
String state[] = new String[7]; //state
//Initiate three DVD
//FirstDVD
name[0] = "罗马假日";
state[0] = "已借出";
date[0] = 1;
count[0] =15;
//Second DVD
name[1] = "风声鹤唳";
state[1] = "可借";
date[1] = 0;
count[1] =12;
//Third DVD
name[2] = "浪漫满屋";
state[2] = "可借";
date[2] = 0;
count[2] =30;
//循环进入条件
String choice = " ";
boolean flag = true;
do{
//Create DVD Menu
System.out.println("欢迎使用迷你DVD管理器");
System.out.println("------------------------------------------------------");
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.退出DVD");
System.out.println("------------------------------------------------------");
System.out.print("请选择:");
choice = input.next();
//Switch choice menu
switch (choice){
case "1":
System.out.println("--->新增DVD");
System.out.print("\n请输入DVD名称:");
boolean firstMonitor = true;
boolean secondMonitor = false;
String bookName = " ";
do{
bookName= input.next();
if(firstMonitor){
for(int i = 0; i<name.length; i++){
if(bookName.equals(name[i])){
System.out.println("货架上已经存在该DVD,请返回目录重新选择!");
secondMonitor = true;
break;
}
}
}
firstMonitor =false;
}while(firstMonitor);
if(!secondMonitor){
for (int j = 0;j<name.length; j++){
if((name[j])==null){
name[j]= bookName;
state[j] = "可借";
count[j] = 0;
System.out.println("新增《"+bookName+"》成功!");
System.out.println("***************************");
break;
}
if(name[name.length-2] !=null){
System.out.println("DVD货架已满,添加失败!");
System.out.println("***************************");
break;
}
}
}
break;
case "2":
System.out.println("--->查看DVD");
System.out.println("序号\t状态\t名称\t\t借出日期\t借出次数");
for (int i = 0; i<name.length; i++){
if(name[i] == null){
System.out.println("***************************");
break;
}
String myDate = " ";
if(date[i] != 0){
myDate = date[i]+"日";
}
System.out.println((i+1)+"\t"+state[i]+"\t"+"《"+name[i]+"》"+"\t"+myDate+"\t"+count[i]+"次");
}
break;
case "3":
System.out.println("--->删除DVD");
System.out.print("\n请输入DVD名称:");
String delName = input.next();
//define index monitor: check
int check = -1;
for(int i = 0; i < name.length; i++){
if(delName.equals(name[i])){
check = i;
break;
}
}
if(check != -1){
if(state[check].equals("可借")){
//Delete operation
for(int j = check; j < name.length-1; j++){
name[j] = name[j+1];
state[j] = state[j+1];
date[j] = date[j+1];
count[j] = count[j+1];
}
System.out.println("删除《"+delName+"》成功!");
System.out.println("***************************");
break;
}else{
System.out.println("DVD为借出状态,不允许删除!");
System.out.println("***************************");
}
}else{
System.out.println("没有找到匹配信息!");
System.out.println("***************************");
}
break;
case "4":
System.out.println("--->借出DVD");
System.out.print("\n请输入DVD名称:");
String lendName = input.next();
//Initiate lendDate with 1, so as to enter the do-loop
int lendDate = 1;
System.out.print("请输入借出日期:");
//Avoid user input wrong date
do{
lendDate = input.nextInt();
if((lendDate<1)||(lendDate>31)){
System.out.print("必须输入大于等于1且小于等于31的数字,请重新输入:");
}
}while((lendDate<1)||(lendDate>31));
//define index monitor
int index = -1;
for(int i = 0; i < name.length; i++){
if(lendName.equals(name[i])){
index = i;
break;
}
}
//lend operation
if(index != -1){
if(state[index].equals("可借")){
state[index] = "已借出";
count[index] +=1;
date[index] = lendDate;
System.out.println("借出《"+lendName+"》成功!");
System.out.println("***************************");
}else{
System.out.println("《"+lendName+"》已被借出!");
System.out.println("***************************");
}
}else{
System.out.println("没有找到匹配信息!");
System.out.println("***************************");
}
break;
case "5":
System.out.println("--->归还DVD");
System.out.print("\n请输入DVD名称:");
String returnName = input.next();
//define index monitor
int monitor = -1;
for(int i = 0; i < name.length; i++){
if(returnName.equals(name[i])){
monitor = i;
break;
}
}
//Initiate myLendPeriod with 0, and enter the do-loop
int returnDate = 0;
int myLendPeriod = 0;
do{
System.out.print("请输入归还日期:");
//Avoid user input wrong date
do{
returnDate = input.nextInt();
if(monitor != -1){
myLendPeriod = returnDate - date[monitor];
}
if(returnDate > 31){
System.out.print("一个月只有31天,请重新输入:");
}
if(myLendPeriod < 0){
System.out.println("归还日期不能小于借出日期,请重新输入:");
}
}while((returnDate>31)||(myLendPeriod<0));
//Return operation
if(monitor != -1){
if(state[monitor].equals("已借出")){
state[monitor] = "可借";
date[monitor] = 0;
System.out.println("归还《"+returnName+"》成功!");
System.out.println("借出日期为:"+date[monitor]+"日");
System.out.println("归还日期为:"+returnDate+"日");
System.out.println("应付租金(元):"+myLendPeriod);
System.out.println("***************************");
}else{
System.out.println("该DVD没有被借出!无法进行归还操作。");
System.out.println("***************************");
}
}else{
System.out.println("没有找到匹配信息!");
System.out.println("***************************");
}
break;
}while(myLendPeriod < 0);
break;
case "6":
flag = false;
break;
default:
}
if(flag){
System.out.print("输入任意值返回:");
choice = input.next();
}
}while(flag);
//program exit
System.out.println("程序结束");
}
}