Java求生之路--迷你DVD管理器

//DVD类
public class DVD {
	boolean flag = false;
	int num;
	Scanner input = new Scanner (System.in);
	String [] names = new String[6];//名称数组
	int [] states = new int[6]; //0可借  1已借出   借阅状态数组
	int [] dates = new int[6];//借阅日期 数组
	int [ ]counts  = new int [6];//借阅次数数组
	//数据初始化
	public void init(){
		//数据初始化 最多6本书
				//DVD :DVD名称  借阅状态  借阅日期 借阅次数 
			
			//boolean flag=true;//标识用户是否要退出系统  true 为不退出
				
			names[0]= "白雪公主";
			dates[0]=5;
			states[0]=1;
			counts [0]= 10;
				
			names[1]= "葫芦娃";
			dates[1]=0;
			states[1]=0;
			counts [1]= 10;
				
			names[2]= "舒克贝塔";
			dates[2]=0;
			states[2]=0;
			counts [2]= 25;
	}
	//开始菜单
	public void StartMenu(){
		
		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.退出系统");
		System.out.println("---------------");
		System.out.println("请选择:");
		int choose = input.nextInt();
		switch(choose){
		case 1:
			LookDVD();
			break;
		case 2:
			addDVD();
			break;
		case 3:
			delDVD();
			break;
		case 4:
			lendDVD();
			break;
		case 5:
			sendDVD();
			break;
		case 6:
			System.out.println("谢谢使用");
			break;
			default:
				System.out.println("谢谢使用");
				break;
		}
		reMenu();
	}
	//返回主菜单继续使用的方法
	public void reMenu(){
		if(!flag)
		{
			//跳出循环
			return;
		}
		else{
			//不结束使用系统 代表用户要返回主菜单继续操作
			System.out.println("输入0返回");
			 num = input.nextInt();
			 if(num ==0){
				 StartMenu();
			 }else{
				 System.out.println("输入错误,程序即将退出");
			 }
			 
		}
	}
	
	//查看DVD
	public void LookDVD(){
		init();
		flag = true;
		System.out.println("DVD信息列表-->");
		System.out.println("序号\t名称\t状态\t借阅日期\t借阅次数");
		//遍历所有图书信息  
		for(int i = 0;i<names.length;i++)
		{
			if(names[i]!=null)
			{
				String state =(states[i]==0)?"可借阅":"已借出";
				String date =(dates[i]==0)?"":dates[i]+"日";
				String count = counts[i]+"次";
				System.out.println((i+1)+"\t"+names[i]+"\t"+state+"\t"+date+"\t"+count);
			}else{
				//遇到第一个为null的名称   意味着后面名称也为null 不用继续输出
				break;
			}
		}
	}
	//新增
	public void addDVD(){
		init();
		flag = true;
		System.out.println("新增DVD-->");
		System.out.print("请输入新增DVD名称:");
		String name = input.next();
		//判断是否能新增DVD
		boolean flagAdd = false;//如果能添加  改为true
		for(int i = 0;i<names.length;i++)
		{
			if(names[i]==null)
			{
				flagAdd = true;
				names[i]= name;
				System.out.println("DVD《"+name+"》添加成功!");
				break;
			}
		}
		if(!flagAdd)
		{
			System.out.println("货架已满 无法添加");
		}
	}
	//删除
	public void delDVD(){
		init();
		flag = true;
		System.out.println("删除DVD-->");
		System.out.print("请输入要删除DVD名称:");
		String deleteBook = input.next();
		//要删除DVD的位置
		int index = -1;
		for(int i = 0;i<names.length;i++)
		{
			if(names[i]==null)
			{
				//没找到
				System.out.println("这不是我们的DVD  不用删除");
				break;
			}
			else if (names[i].equals(deleteBook) && states[i]==1)
			{
				//找到了要删除的DVD 但是DVD已经借出去了
				System.out.println("该DVD已经借出去了  无法删除!");
				break;
			}
			else if (names[i].equals(deleteBook) && states[i]==0)
			{
				//找到了要删除的DVD 但是DVD 没借出去了
				//记录一下该DVD 的位置
				index = i;
				break;
			}
			
		}
		//index 可能依然是-1  也可能是一个正常的位置
		//根据DVD 的位置进行删除  后续位置依次删除 依次往前覆盖
		if(index !=-1)
		{
			//从index 到数组的最后一个DVD 依次往前覆盖 但是最后一DVD 要置空
			for(int i = index;i<names.length;i++)
			{
				if(i!=names.length-1)
				{
					names[i]=names[i+1];
					dates[i]=dates[i+1];
					states[i]=states[i+1];
					counts [i]=counts[i+1];
				}else
				{
					names[names.length-1]=null;
					counts[names.length-1]=0;
					dates[names.length-1] =0;
					states[names.length-1]=0;
				}
			}
			System.out.println("DVD 删除成功!");
			
		}
		else
		{
			System.out.println("无法删除!");
		}
	}
	//借出
	public void lendDVD(){
		init();
		flag = true;
		System.out.println("借阅DVD -->");
		System.out.println("请输入借阅DVD 的名称:");
		String want = input.next();
		for(int i = 0;i<names.length;i++)
		{
			
				if(names[i] == null)
				{
					//没找到要借的DVD 
					
					System.out.println("没有找到这个DVD !");
					break;
				}
				else if(names[i].equals(want) && states[i]==0){
					//找到了要借的DVD   DVD 的状态为可借阅
					System.out.print("请输入借阅日期:");
					dates[i]= input.nextInt();
					while((dates[i]<1 || dates[i]>31))
					{
						System.out.println("数字格式不正确  请输入1-31之间的数字!");
						dates[i]= input.nextInt();
					}
					System.out.println("借出《"+want+"》成功!");
					states[i]=1;//将DVD 的状态修改为已借出 
					counts[i]++;//借阅次数加1
					break;
				}
				else if (names[i].equals(want) && states[i]==0)
				{
					//找到了要借的DVD   DVD 的状态为不可借阅
					System.out.println("该DVD 已经借出去了");
				}
				
			}
			
	}
	//归还
	public void sendDVD(){
		init();
		System.out.println("归还DVD -->");
		System.out.println("请输入归还DVD 的名称:");
		String returnDVD  = input.next();
		flag = true;
		for(int i = 0;i<names.length;i++)
		{
			if(names[i] == null)
			{
				//没找到要还的书
				System.out.println("这不是我们的DVD 不用归还!");
				break;
			}
			else if(names[i].equals(returnDVD ) && states[i]==1){
				//找到了要还   状态为已借出
				System.out.print("请输入归还日期:");
				int date = input.nextInt();
				while(date<1 ||date>31 ||date<dates[i])
				{
					if(date<1 || date>31)
					{
						System.out.println("数字格式不正确  请输入1-31之间的数字!");
						date = input.nextInt();
					}else if(date<dates[i])
					{
						System.out.println("归还日期不可小于借阅日期!");
						date = input.nextInt();
					}
				}
				System.out.println("归还《"+returnDVD +"》成功!");
				states[i]=0;//将DVD 的状态修改为可借阅
				
				//计算租金
				int  money= ((date-dates[i])*1);
				//重新设置借阅日期 
				dates[i]=0;
				System.out.println("租金为:"+money+"元");
				break;
			}
			else if (names[i].equals(returnDVD ) && states[i]==0)
			{
				//找到了要还的DVD   DVD 的状态为可借阅
				System.out.println("无法归还!");
			}
			
			
		}
	}
}




测试类

public class MyDVD {
	public static void main(String[] args) {
		DVD dMar = new DVD();
		dMar.StartMenu();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值