包括新增,查看,删除,借出和归还,用方法和类的知识
import java.util.Scanner;
//业务逻辑
public class DVDM {
Scanner in = new Scanner(System.in);
//存放DVD
DVD[] dvds = new DVD[6];
//初始化方法
public void init(){
DVD dvd = new DVD();
dvd.dvdName="大话西游";
dvd.zhuangTai=1;
dvd.riQi=20;
dvd.ciShu=100;
DVD dvd1=new DVD();
dvd1.dvdName="大鱼海棠";
dvd1.zhuangTai=0;
dvd1.riQi=0;
dvd1.ciShu=120;
DVD dvd2 = new DVD();
dvd2.dvdName="三人行";
dvd2.zhuangTai=0;
dvd2.riQi=0;
dvd2.ciShu=28;
//给数组赋值
dvds[0]=dvd;
dvds[1]=dvd1;
dvds[2]=dvd2;
}
//查看DVD
public void cha(){
System.out.println("-->查看DVD");
System.out.println("名称\t状态\t日期\t次数");
for(int i=0;i<dvds.length;i++){
if(dvds[i]!=null){
System.out.println(dvds[i].dvdName+"\t"+(dvds[i].zhuangTai==0?"可借":"不可借")+"\t"+dvds[i].riQi+"\t"+dvds[i].ciShu);
}
}
}
// 新增DVD
public void xin(){
System.out.println("-->新增DVD");
System.out.println("请输入名字:");
String zeng=in.next();
for(int i=0;i<dvds.length;i++){
if(dvds[i]==null){
DVD dvd =new DVD();
dvd.dvdName=zeng;
dvd.zhuangTai=0;
dvd.riQi=0;
dvd.ciShu=0;
dvds[i]=dvd;
System.out.println("添加成功!");
break;
}
}
}
//借出DVD
public void jie(){
System.out.println("-->借出DVD");
System.out.println("请输入名字:");
String dvdname=in.next();
for(int i =0;i<dvds.length;i++){
//如果这本书可借;
if(dvds[i]!=null && dvds[i].dvdName.equals(dvdname) && (dvds[i].zhuangTai==0) ){
dvds[i].zhuangTai = 1;
dvds[i].ciShu++;
System.out.println("请输入借出天数:");
dvds[i].riQi=in.nextInt();//天数
System.out.println("借出成功");
break;
}
//如果这本书是借出状态,不可借
if(dvds[i]!=null&& dvds[i].dvdName.equals(dvdname) && (dvds[i].zhuangTai==1)){
System.out.println("该书已借出,不可再借!");
break;
}
//没有这本书
if(i==dvds.length-1){
System.out.println("非常抱歉,没有您要找的书~");
}
}
}
//删除DVD
public void shan(){
System.out.println("-->删除DVD");
System.out.println("请输入名字:");
String dalname=in.next();
for(int i =0;i<dvds.length;i++){
//如果这书已被借出,则不能被删除
if(dvds[i]!=null && dvds[i].dvdName.equals(dalname) && (dvds[i].zhuangTai==1)){
System.out.println("该书已被借出,不能被删除!");
break;
}
//该书没有被借出,可以被删除
if(dvds[i]!=null && dvds[i].dvdName.equals(dalname) && (dvds[i].zhuangTai==0)){
dvds[i]=null;
System.out.println("删除成功!");
break;
}
//没有找到此书。无法删除
if(i==dvds.length-1){
System.out.println("没有找到此书,无法删除~~");
}
}
}
//归还DVD
public void gui(){
System.out.println("-->归还DVD");
System.out.println("请输入名字:");
String guiname=in.next();
for(int i =0;i<dvds.length;i++){
//如果该书在店里,不用归还
if(dvds[i]!=null && dvds[i].dvdName.equals(guiname) && dvds[i].zhuangTai==0){
System.out.println("该书在它的床上躺着呢~~");
break;
}
//如果该书被借出,可以归还
if(dvds[i]!=null && dvds[i].dvdName.equals(guiname) && dvds[i].zhuangTai==1){
dvds[i].zhuangTai=0;
int tian=in.nextInt();
//如果还书天数大于借出天数
if(i>dvds[i].riQi){
System.out.println("请输入天数:");
System.out.println("找你钱:"+(tian-dvds[i].riQi)+"¥");
}else if(i<dvds[i].riQi){
System.out.println("请补钱:"+(dvds[i].riQi-tian)+"¥");
}else{
System.out.println("请慢走~~");
}dvds[i].zhuangTai=0;
System.out.println("还书成功~~");
}
//没有此书
if(i==dvds.length-1){
System.out.println("没有此书,抱歉~");
}
}
}
//退出DVD
//公共返回方法
public boolean gong(){
System.out.println("按0返回:");
int gong=in.nextInt();
if(gong==0){
return true;
}else{
return false;
}
}
}
建个DVD类
public class DVD {
//dvd的属性
String dvdName;
int riQi;//日期
int zhuangTai;//状态 0 可借 1 不可借
int ciShu;//次数
}
DVD的测试类
import java.util.Scanner;
public class Dome {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
//初始化值
DVDM mag = new DVDM();
mag.init();
do{
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.退出");
int xuan=in.nextInt();
switch (xuan) {
case 1:
mag.cha();
break;
case 2:
mag.xin();
mag.cha();
break;
case 3:
mag.jie();
mag.cha();
break;
case 4:
mag.shan();
mag.cha();
break;
case 5:
mag.gui();
mag.cha();
break;
case 6:
System.out.println("退出系统!");
return;
default:
System.out.println("输入有误~请重新输入");
break;
}
}while(mag.gong());
}
}