public class DVDset {
String[]name=new String[50];
int[]state=new int[50];
String[]date=new String[50];
int[]count=new int[50];
}
import java.util.Scanner;
import java.util.Date;
import java.text.SimpleDateFormat;
public class DVDmgr {
DVDset dvd=new DVDset();
//初始化DVD的信息,包括DVD名字、状态(是否被借出)、借出日期、 被借次数,dvd.state=1表示没有被借出,0表示被借出
public void initial(){
dvd.name[0]="罗马假日";
dvd.state[0]=1;
dvd.date[0]="";
dvd.count[0]=0;
dvd.name[1]="风声鹤唳";
dvd.state[1]=1;
dvd.date[1]="";
dvd.count[1]=0;
dvd.name[2]="浪漫满屋";
dvd.state[2]=1;
dvd.date[2]="";
dvd.count[2]=0;
}
//主菜单,并调用每一块的方法
public void menu(){
System.out.println("欢迎使用迷你DVD管理器");
System.out.println("------------------------");
System.out.println("1.新增");
System.out.println("2.查看");
System.out.println("3.删除");
System.out.println("4.借出");
System.out.println("5.归还");
System.out.println("6.爬行榜");
System.out.println("7.退出");
System.out.println("------------------------");
Scanner input=new Scanner(System.in);
System.out.print("请选择:");
int choose=input.nextInt();
switch(choose){
case 1:add();break;
case 2:chakan();break;
case 3:delete();break;
case 4:jie();break;
case 5:huan();break;
case 6:paihang();break;
case 7:System.out.println("谢谢使用");break;
}
}
//查看DVD的方法
public void chakan(){
Scanner input=new Scanner(System.in);
System.out.println("------------>查看");
System.out.println("序号\t状态\t名称\t借出日期");
for(int i=0;i<dvd.name.length;i++){
if(dvd.name[i]!=null){
System.out.print(i);
if(dvd.state[i]==0){
System.out.print("\t已借出");
}else{
System.out.print("\t可借");
}
System.out.print("\t"+dvd.name[i]);
if(dvd.state[i]==0){
System.out.print(dvd.date[i]);
}else{
System.out.print("\t");
}
System.out.println("");
continue;
}
}
System.out.print("输入0返回");
int answer=input.nextInt();
if (answer==0){
menu();
}
}
//新增DVD的方法
public void add(){
System.out.println("------------>新增");
Scanner input=new Scanner(System.in);
System.out.print("请输入名称");
for(int i=0;i<dvd.name.length;i++){
if(dvd.name[i]==null){
dvd.name[i]=input.next();
dvd.state[i]=1;
System.out.println("新增《"+dvd.name[i]+"》成功");
break;
}
}
System.out.print("输入0返回");
int answer=input.nextInt();
if (answer==0){
menu();
}
}
//删除DVD的方法,删除之前需要判断DVD的状态,如果被借出,则无法
删除
public void delete(){
Scanner input=new Scanner(System.in);
System.out.println("------------>删除");
System.out.println("请输入要删除的DVD名称");
String name=input.next();
for(int i=0;i<dvd.name.length;i++){
if(name.equals(dvd.name[i])){
if(dvd.state[i]==1){
dvd.name[i]=null;
System.out.println("删除"+"《"+dvd.name[i]+"》成功");
}else{
System.out.println("此DVD外借中,无法删除");
}
} else{
System.out.println("找不到该DVD");
}
System.out.print("输入0返回");
int answer=input.nextInt();
if (answer==0){
menu();
}
}
}
//借出DVD的方法,需要先判断DVD的状态,若已经被借出,则无法再次借出
public void jie(){
Scanner input=new Scanner(System.in);
System.out.println("------------>借出");
System.out.print("请输入DVD名称");
String name=input.next();
for(int i=0;i<dvd.name.length;i++){
if(name.equals(dvd.name[i])){
if(dvd.state[i]==1){
System.out.println("借出"+dvd.name[i]+"成功");
System.out.print("请输入借出日期");
String shijian=input.next();
dvd.state[i]=0;
dvd.date[i]=shijian;
dvd.count[i]++;
}else{
System.out.println(" 此DVD已经在外借中,无法再借出");
}
}
}
System.out.print("输入0返回");
int answer=input.nextInt();
if (answer==0){
menu();
}
}
//huan()和charge()方法目前木有学习、等待补充
public void huan() {
Scanner input = new Scanner(System.in);
System.out.print("------------>归还\n");
System.out.print("请输入归还的DVD名称:");
String returndvd = input.next();
for (int i = 0; i <= dvd.name.length; i++) {
if (returndvd.equals(dvd.name[i]) && dvd.state[i] == 0) {
System.out.print("请输入归还的日期:");
String date = input.next();
System.out.println("《"+returndvd+"》"+ "归还成功!");
System.out.println("借出日期为:" + dvd.date[i]);
System.out.println("归还日期为:" + date);
String d1 = dvd.date[i];
String d2 = date;
long day = charge(d2, d1);
System.out.println("应付租金:" + day + "元");
dvd.date[i] = "";
dvd.state[i] = 1;
break;
}
}
System.out.print("输入0返回");
int answer=input.nextInt();
if (answer==0){
menu();
}
}
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 = (d1.getTime() - d2.getTime())/ (24 * 60 * 60 * 1000);
} catch (Exception e) {
e.printStackTrace();
}
return charge;
}
//排行榜的方
法,用冒泡排序完成
public void paihang() {
System.out.println("次数\t名称");
for (int i = 0; i < dvd.name.length - 1; i++) {
for (int j = dvd.name.length-1; j >i; j--) {
if (dvd.count[j] > dvd.count[j - 1]) {
int temp = dvd.count[j];
dvd.count[j] = dvd.count[j - 1];
dvd.count[j - 1] = temp;
String pai=dvd.name[j];
dvd.name[j] = dvd.name[j - 1];
dvd.name[j - 1]=pai;
}
}
}
for (int i = 0; i < dvd.name.length ; i++) {
if (dvd.name[i] != null) {
System.out.println(dvd.count[i] + "\t" + dvd.name[i]);
}
}
}
public static void main(String[] args) {
DVDmgr sc=new DVDmgr();
sc.initial();
sc.menu();
}
}
//所有的显示类方法,都要先判断dvd.name 是否为null,是的话将不显示
java免费学习资料直播公开课群175161984