日期和状态
package DVD;
import java.text.SimpleDateFormat;
import java.util.*;
public class Dvd {
private int xuhao;
private String name;
private Date date;
private int state;
private int zj;
public Dvd(int xuhao, String name) {
this.xuhao = xuhao;
this.name = name;
this.date = date;
this.state=state;
this.zj=zj;
}
public int getNum() {
return xuhao;
}
public void setNum(int num) {
this.xuhao = xuhao;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return date;
}
public void getState(){
this.state=state;
}
public void getzj(){
this.zj=zj;
}
public int getAge() {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
c.setTime(date);
return year - c.get(Calendar.YEAR);
}
@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return String.format("序号:%d,状态:%d名称:%s,借出日期:%s",
this.xuhao,this.state,this.name,this.date);
}
}
package DVD;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Dvd2 {
String dstr1 =null;
String dstr2 =null;
Scanner sc =new Scanner(System.in);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Dvd[] emps = new Dvd[100];
int empCount=0;
public Dvd2() throws ParseException{
emps[0] = new Dvd(1,"哪吒传奇");
emps[1] = new Dvd(2,"黑猫警长");
emps[5] = new Dvd(3,"猫和老鼠");
empCount =3;
}
public void printAll(){
for (Dvd e : emps) {
if(e!=null)
System.out.println(e);
}
}
int getEmptyIndex(){
for (int i = 0; i < emps.length; i++) {
if(emps[i]==null)
return i;
}
return -1;
}
public void add() throws ParseException{
int i = getEmptyIndex();
System.out.println("请输入新增DVD序号:");
int xh = sc.nextInt();
System.out.println("请输入新增DVD名称:");
String n = sc.next();
if(i>=0){
emps[i] =new Dvd(xh, n);
empCount++;
}
else
System.out.println("数组已经没有空间存储新增DVD信息");
}
int getEmpIndexByNum(int num){
for (int i = 0; i < emps.length; i++) {
if(emps[i] != null && emps[i].getNum() == num)
return i;
}
return -1;
}
public void delete(){
System.out.println("请输入要删除的DVD序号:");
int n = sc.nextInt();
int index = getEmpIndexByNum(n);
if(index>=0){
emps[index]=null;
empCount--;
}
else
System.out.println(String.format("未找到序号为:%d 的DVD信息",n));
}
public void printgh(){
System.out.println("请输入要归还的DVD名称:");
String name= sc.next();
System.out.println("请输入归还日期:");
dstr2=sc.next();
}
public void jc() {
System.out.println("请输入借出DVD的名称: ");
String name=sc.next();
System.out.println("请输入借出DVD的状态:0可借,1借出");
int state=sc.nextInt();
System.out.println("请输入DVD借出日期");
dstr1=sc.next();
}
public void zj() throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d1=sdf.parse(dstr1);
Date d2=sdf.parse(dstr2);
long charge = (d2.getTime()-d1.getTime())/(24*60*60*1000);
double zj=charge*2.5;
System.out.println(zj);
}
}
package DVD;
import java.util.Scanner;
import java.text.ParseException;
import java.util.Date;
public class Test {
public static void main(String[] args) throws ParseException {
Dvd2 manager = new Dvd2();
System.out.println("欢迎使用迷你DVD管理器:");
Object Dvd2 = null;
menu(manager);
System.out.println("再见");
}
static void menu(Dvd2 em) throws ParseException{
Scanner sc =new Scanner(System.in);
int order = 0;
do{
System.out.println("0:退出 1:查看DVD 2:新增DVD 3:删除DVD 4:借出DVD 5:归还DVD 6:租金");
order = sc.nextInt();
switch(order){
case 1:
em.printAll();
break;
case 2:
em.add();
break;
case 3:
em.delete();
break;
case 4:
em.jc();
break;
case 5:
em.printgh();
break;
case 6:
em.zj();
break;
}
}while(order !=0);
}
}
请大神解答!日期和状态有点问题。