minidvd入门练习

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lvyongjie105/article/details/96446201
package com.jb.minidvd;

public class DVDSet {
	String[] name = new String[50];   //数组1存储DVD名称数组
    int[] state = new int[50];        //数组2存储DVD借出状态:0已借出/1可借
    String[] date=new String[50];     //数组3存储DVD借出日期
    int[] count=new int[50];         //借出次数
   
}

package com.jb.minidvd;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

import javax.print.DocFlavor.INPUT_STREAM;

public class DVDMgr {
	Scanner sc = new Scanner(System.in);
	
	DVDSet dvd = new DVDSet();
/**
 * 初始化变量
 */
	 public void initial() {
	    	
	    	dvd.name[0] = "罗马假日";
	    	dvd.state[0] = 0; 
	    	dvd.date[0]="2010-7-1";
	    	dvd.count[0]=15;
	    	
	    	dvd.name[1] = "风声鹤唳";
	    	dvd.state[1] = 1;  
	    	dvd.count[1]=12;
	    	
	    	dvd.name[2] = "浪漫满屋";
	    	dvd.state[2] = 1;
	    	dvd.count[2]=30;
	    }	
	/**
	 * 菜单
	 */
	 
		public 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("请选择: ");
		   	 Scanner input = new Scanner(System.in);
		   	 int choice = input.nextInt();
		
		   	switch(choice){
	   	 	case 0:
	   	    	list();
	   	    	break;
	   	 	case 1:
	   	 		add();
	   	    	break;
	   	    case 2: 
	   	    	search();
	   	    	break;
	   	    case 3:
	   	    	delete();
	   	    	break;
	   	    case 4:
	   	    	lend();
	   	    	break;
	   	    case 5:
	   	    	returnDvd();
	   	    	break;
	   	    case 6:
	   	    	System.out.println("\n谢谢 使 用!");
	   	    	break;
	   	 }
    }
	
		public void search(){
	    	System.out.println("---> 查看DVD\n");
	    	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]+">>");
	   		    }
	   	    }
	    	System.out.println("**************************");
	    	returnMain(); 	 
	    }
		
		
		public void add() {
			System.out.println("---> 新增DVD\n");    	
		   	 System.out.print("请输入DVD名称: ");
		   	 String name = sc.next();  
		   	 for (int i = 0; i < dvd.name.length; i++) {
		   		if(dvd.name[i] == null){    //查询最后一个空位置插入
		   			dvd.name[i]=name;
		   			dvd.state[i]=1;//置新增的DVD可借状态
		   			 System.out.println("新增《"+name+"》成功!");
		   			 break;
		   		 }
		   		 
			}
		   	System.out.println("**************************");
		   	 returnMain();   	
		}
		
		public void delete() {
			boolean flag = false;
			
			System.out.println("---> 删除DVD\n");
	    	System.out.print("请输入DVD名称: ");
	    	String name = sc.next();
	    	
	    	for (int i = 0; i < dvd.name.length; i++) {
				
	    		if (dvd.name[i]!=null&&dvd.name[i].equalsIgnoreCase(name)&&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("删除《"+name+"》成功!");
	    			flag=true;
	    			break;
				}else if(dvd.name[i]!=null&&dvd.name[i].equalsIgnoreCase(name)&&dvd.state[i]==0){
					System.out.println("《"+name+"》为借出状态,不能删除!"); 
	      			flag=true;//置位
	      			break;
				}
	    		
			}
	    	if(!flag){
	      		System.out.println("没有找到匹配信息!");
	      	}
	      	System.out.println("**************************");
	      	returnMain(); 
			
		}
		
	    public void lend(){
	      	 System.out.println("---> 借出DVD\n");
	      	 
	      	 
	      	 System.out.print("请输入DVD名称: ");
	      	 String want = sc.next();  //要借出的DVD名称 
	      	 for(int i = 0; i < dvd.name.length; i++){   		 
	      		 if(dvd.name[i] == null){    //无匹配
	      			 System.out.println("没有找到匹配信息!");
	      			 break;
	      		 }else if(dvd.name[i].equals(want)&& dvd.state[i]==1){  //找到匹配可借	 
	      			 dvd.state[i] = 0;
	      			 System.out.print("请输入借出日期(年-月-日):");
	      			 dvd.date[i]=sc.next();
	      			 System.out.println("借出《"+want+"》成功!");   			
	      			 dvd.count[i]++;
	      			 break;
	      		 }else if(dvd.name[i].equals(want)&& dvd.state[i]==0){  //找到匹配已被借出 			
	      			 System.out.println("《"+want+"》已被借出!");
	      			 break;
	      		 }   		 
	      	 }
	      	 System.out.println("**************************");
	      	 returnMain();
	       }
	    /**
	     * 归还DVD并计算租金
	     */
	    public void returnDvd(){
	   	 System.out.println("---> 归还DVD\n");
	   	 
	   	 Scanner input = new Scanner(System.in);
	   	 long loan=0;//租金
	   	 System.out.print("请输入DVD名称: ");
	   	 String want = 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(want) && dvd.state[i]==0){  //找到匹配	 
	   			 dvd.state[i] = 1;
	   			 System.out.print("请输入归还日期(年-月-日):");
	   			 String redate=input.next();
	   			 //计算租金
	   			 loan=charge(dvd.date[i],redate);
	   			 System.out.println("\n归还《"+want+"》成功!");
	   			 System.out.println("借出日期为:"+dvd.date[i]);
	   			 System.out.println("归还日期为:"+redate);
	   			 System.out.println("应付租金(元):"+loan);
	   			 break;
	   		 }  else if(dvd.name[i].equals(want) && dvd.state[i]==1){ //找到匹配但没有借出
	   			System.out.println("该DVD没有被借出!无法进行归还操作。");
	   			break;
	   		 }
	   	 }
	   	 System.out.println("**************************");
	   	 returnMain();
	    }
	    /**
	     * 计算日期差
	     * @param dstr1 第一个日期
	     * @param dstr2 第二个日期
	     * 
	     */
	    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;
	    }
	    /**
	     * 借出排行榜
	     */
	    public void list(){
	    	//定义新数组,用来存放排序后DVD信息
	    	String[] newname = new String[50];   //       
	        int[] newcount=new int[50];  
	        for(int k=0;k<dvd.name .length;k++){
	        	 newname[k]=dvd.name[k];
	             newcount[k]=dvd.count[k];
	        }
	          	
	    	//利用冒泡排序算法进行排序
	    	for(int i=0;i<newname.length;i++){
	    		for(int j=i+1;j<newname.length;j++){
	    			if(newcount[i]>newcount[j]){
	    				int tempc=newcount[i];
	    				newcount[i]=newcount[j];
	    				newcount[j]=tempc;
	    				String tempn=newname[i];
	    				newname[i]=newname[j];
	    				newname[j]=tempn;
	    			}    			
	    		}    		
	    	}
	    	System.out.println("---> 排行榜\n");
	    	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();
	       }
	
	    
		
		
		
		
		private void returnMain() {
			// TODO Auto-generated method stub
			System.out.println("输入0返回:");
			if(sc.nextInt()==0) {
				startMenu();
			}else {
				
				System.out.println("输入错误, 异常终止!");
			}
			
		}
		
		

}

package com.jb.minidvd;

public class Start {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DVDMgr dm=new DVDMgr();	
		dm.initial();
		dm.startMenu();

	}

}

展开阅读全文

菜鸟关于miniDVD的借出代码

07-23

import java.util.*;rn/**rn * 定义name和state数组,来存放DVD名称和状态rn * @author Administratorrn *rn */rnpublic class DVDSet rn String[] name = new String[50]; rn int[] state=new int[50];rn rn public void initial() rn Scanner input = new Scanner(System.in);rn String answer = "y";rn rn for (int i = 0; i < name.length; i++) rn System.out.print("第" + (i + 1) + "的DVD名称:");rn name[i] = input.next();rn System.out.println("是否借出?(0:借出 1:可借)");rn state[i] = input.nextInt();rn rnrn System.out.print("是否继续(y/n)");rn answer = input.next();rn if (answer.equals("n")) rn break;rn rn rnrn rnrnrnDVDset类rnrnrnrnimport java.util.*;rn/**rn * 初始化DVD信息rn * @author Administratorrn *rn */rnpublic class DVDMgr rnrn DVDSet dvd = new DVDSet(); // 调用DVDSet类rnrn public void setDate() //初始化数据rn int[]state=dvd.state;rn dvd.initial();rn rnrn public void startMenu() //显示菜单rn System.out.println("欢迎使用MiniDVD Mgr 1.0");rn System.out.println("-------------------------------");rn System.out.println("1.查看DVD\n2.借出DVD\n3.退出MiniDVD Mgr");rn System.out.println("-------------------------------");rn System.out.print("\n请选择:");rn Scanner input = new Scanner(System.in);rn int choice = input.nextInt();rn switch (choice) rn case 1: rn this.search(); //调用查询块rn break;rn case 2:rn this.lend(); //调用借出块rn break;rnrn case 3:rn System.out.println("\n欢迎使用!");rn rn rnrn public void search() //查询块rn System.out.println("MyDVD Mgr 1.0 ---> 查询DVD\n");rn String[] A = dvd.name;rn int[] B = dvd.state;rn String C;rn for (int i = 0; i < A.length; i++) rn if (A[i] == null) rn break;rn rnrn if (B[i] == 0) rn C = "已借出";rn System.out.println(A[i] + "\t" + C);rn else rn System.out.println(A[i]);rn rnrn rn System.out.println("------------------------------");rn System.out.println("输入0返回");rn this.returnMain(); //调用返回块rnrn rnrn public void returnMain() //返回块rn Scanner input = new Scanner(System.in);rn int startMenu = input.nextInt();rn if (startMenu == 0) rn this.startMenu();rn rn rnrn public void lend() //借出块rn System.out.println("MyDVD Mgr 1.0 ---> 借出DVD\n");rn System.out.println("请输入要借的DVD名称");rn Scanner input = new Scanner(System.in);rn String[] name = new String[50];rn rn rn for (int i = 0; i < name.length; i++) rn name[i]=input.next();rn int[]state = dvd.state;rn if (dvd.name.equals(name)&& state[i]==dvd.state[i]&&dvd.state[i]==1 ) rn dvd.state[i] = 0;rn System.out.println("操作成功!");rn break;rnrn else rn System.out.println("操作不成功!");rn rn break;rnrn rn rn rn rnrn System.out.println("-----------------");rn System.out.println("输入0返回");rn this.returnMain();rn rnrn public static void main(String[] args) //主方法,测试rn DVDMgr mgr = new DVDMgr(); // 调用DVDMgr类rn mgr.setDate();rn mgr.startMenu();rn rnrnDVDMgr类rnrn现在的问题是,我运行到借出,也就是len()类的时候,总是直接跳到操作不成功,请哪位高手帮忙看下,万谢! 论坛

没有更多推荐了,返回首页