博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MINIDVD
阅读量:4992 次
发布时间:2019-06-12

本文共 5220 字,大约阅读时间需要 17 分钟。

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
int date[] = new int[7]; 
int count[] = new int[7];
String name[] =new String[7];
String state[] = new String[7];
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{
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){
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("可借")){
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

System.out.print("请输入借出日期:");

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("程序结束");
}
}

转载于:https://www.cnblogs.com/s1297-lgy/p/5698102.html

你可能感兴趣的文章
Canvas链式操作
查看>>
学渣乱搞系列之网络流学习
查看>>
Acdream A - Unique Attack
查看>>
java遍历List的多种方法
查看>>
【投票】你心目中的Excel催化剂价值有多大(附主流国内外收费插件供参考)?...
查看>>
算法复习——半平面交(bzoj2618凸多边形)
查看>>
关于在Intellij Idea中使用JSTL标签库报错的问题
查看>>
如何用自己电脑做服务器,绑定域名建一个个人网站
查看>>
.ds_store是什么文件
查看>>
递归C++
查看>>
POJ 1751 Highways(最小生成树&Prim)题解
查看>>
linux 安装openssh-server, openssh-client
查看>>
Java继承的基本概念及其限制 总结
查看>>
RF1001: 各浏览器对 '@font-face' 规则支持的字体格式不同,IE 支持 EOT 字体,Firefox Safari Opera 支持 TrueType 等字体...
查看>>
Socket 学习(三)
查看>>
题解 CF43B 【Letter】
查看>>
CommandName and CommandArgument
查看>>
[z]FNV哈希算法
查看>>
通过层序和中序遍历序列重建二叉树
查看>>
【Git】git clone与git pull区别
查看>>