package com.entity; import java.text.SimpleDateFormat; import java.util.Date; public class DVD { private String name; private boolean borrowed; private Date borrowedTime; public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBorrowedTime() { return borrowedTime; } public void setBorrowedTime(Date borrowedTime) { this.borrowedTime = borrowedTime; } public DVD(String name, boolean borrowed, Date borrowedTime) { super(); this.name = name; this.setBorrowed(borrowed); this.borrowedTime = borrowedTime; } public DVD() { super(); } public String toString(){ String status= isBorrowed()?"已借出":"可借"; String date; if(getBorrowedTime()==null){ date=""; }else{ date=new SimpleDateFormat("yyyy-MM-dd").format(getBorrowedTime()); } return "《"+getName()+"》\t"+status+"\t"+date; } public void setBorrowed(boolean borrowed) { this.borrowed = borrowed; } public boolean isBorrowed() { return borrowed; } } package com.entity; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Scanner; public class DVDManager { private ArrayList<DVD> dvdList; private final int price=2;//一张DVD2块钱1天 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Scanner input=new Scanner(System.in); public void setDvdList(ArrayList<DVD> dvdList) { this.dvdList = dvdList; } public ArrayList<DVD> getDvdList() { return dvdList; } public DVDManager(){ } public DVDManager(ArrayList<DVD> dvdList){ this.dvdList=dvdList; } public void initial(){ dvdList=new ArrayList<DVD>(); try { DVD d=new DVD("罗马假日",true,sdf.parse("2013-07-01")); DVD d2=new DVD("风声鹤唳",false,null); DVD d3=new DVD("浪漫满屋",false,null); dvdList.add(d); dvdList.add(d2); dvdList.add(d3); } catch (ParseException e) { e.printStackTrace(); } } public void mainMenu(){ System.out.println("欢迎使用迷你DVD管理器"); System.out.println("-------------------------------------------------------"); System.out.println("" + "1. 新增DVD\n" + "2. 查看DVD\n" + "3. 删除DVD\n" + "4. 借出DVD\n" + "5. 归还DVD\n" + "6. 退出DVD"); System.out.println("-------------------------------------------------------"); System.out.print("请选择:"); input=new Scanner(System.in); String regex="^[1-6]{1}$"; while(!input.hasNext(regex)){ input=new Scanner(System.in); System.out.print("输入有误,请重新选择:"); } int check_1=input.nextInt(); switch(check_1){ case 1: addDVD(); break; case 2: queryAll(); break; case 3: deleteDVD(); break; case 4: borrowDVD(); break; case 5: lend(); break; case 6: System.out.println(); System.out.println("谢谢使用!"); System.exit(0); break; } System.out.println("********************************"); back(); } public void back(){ System.out.print("输入0返回:"); input=new Scanner(System.in); String regex="^0$"; while(!input.hasNext(regex)){ input=new Scanner(System.in); System.out.print("输入有误,输入0返回:"); } mainMenu(); } public void queryAll(){ System.out.println("----> 查看DVD\n"); System.out.println("序号\t 名称 \t\t状态\t 借出日期"); for(int i=0;i<dvdList.size();i++){ System.out.println((i+1)+"\t"+dvdList.get(i)); } } public DVD findDVDByName(String name){ //DVD dvd=null; 与DVD dvd=new DVD();是有不同的 DVD dvd=null; for(int i=0;i<dvdList.size();i++){ if(dvdList.get(i).getName().equals(name)){ dvd=dvdList.get(i); break; } } return dvd; } public void addDVD(){//新增DVD System.out.println("----> 新增DVD\n"); System.out.print("请输入DVD名称:"); String name=input.next(); DVD d=findDVDByName(name); if(d!=null){ System.out.println("该DVD已经存在,不能再次添加!"); return; } d=new DVD(name,false,null); dvdList.add(d); System.out.println("新增《"+d.getName()+"》成功!"); } public void borrowDVD(){ System.out.println("----> 借出DVD\n"); System.out.print("请输入DVD名称:"); String name=input.next(); DVD d=findDVDByName(name); if(d==null){ System.out.println("该DVD不存在,无法借出!"); return; }else if(d.isBorrowed()){ System.out.println("不好意思,该DVD已被借出!"); } System.out.print("请输入借书日期(年-月-日):"); String regex="\\d{4}-(([1][012])|([0][0-9]))-(([012][0-9])|([3][0]))"; while(!input.hasNext(regex)){ input=new Scanner(System.in); System.out.print("借书日期格式有误请重新输入:"); } Date date = null; try { date=sdf.parse(input.next()); } catch (ParseException e) { e.printStackTrace(); } int index=dvdList.indexOf(d); d.setBorrowed(true); d.setBorrowedTime(date); dvdList.set(index, d); System.out.println("借出《"+name+"》成功!"); } public void deleteDVD(){ System.out.println("----> 删除DVD\n"); System.out.print("请输入DVD名称:"); String name=input.next(); DVD d=findDVDByName(name); if(d==null){ System.out.println("该DVD不存在,无法删除!"); return; }else if(d.isBorrowed()){ System.out.println("该DVD已被借出,无法删除"); return; } dvdList.remove(d);//移除第一个,后面自动左移 System.out.println("删除《"+name+"》成功!"); } public void lend(){ System.out.println("----> 归还DVD\n"); System.out.print("请输入DVD名称:"); String name=input.next(); DVD d=findDVDByName(name); if(d==null){ System.out.println("归还失败,该DVD不是我们店里的!"); return; }else if(!d.isBorrowed()){ System.out.println("归还失败,该DVD已经归还,您的DVD不是我们店里的!"); return; } Date borrowDate = d.getBorrowedTime(); Date lendDate = null; long spend; System.out.print("请输入还书日期(年-月-日):"); String regex="\\d{4}-(([1][012])|([0][0-9]))-(([012][0-9])|([3][0]))"; while(true){//感觉还是玩死循环跳出爽 while(!input.hasNext(regex)){ input=new Scanner(System.in); System.out.print("还书日期格式有误请重新输入:"); } try { lendDate=sdf.parse(input.next()); } catch (ParseException e) { e.printStackTrace(); } if(lendDate.before(borrowDate)){//Date的比较前者是否在后者之前的方法 System.out.println("还书日期不可能比借出日期还早,请重新输入"); }else{ break; } } spend=lendDate.getTime()-borrowDate.getTime(); int money=(int) Math.ceil(spend/(24*60*60*1000))*price; int index=dvdList.indexOf(d); d.setBorrowed(false); d.setBorrowedTime(null); dvdList.set(index, d); System.out.println("归还成功!"); System.out.println("应付租金(元):"+money); } } package com.test; import com.entity.DVDManager; public class Test { public static void main(String[] args) { DVDManager dm=new DVDManager(); dm.initial(); dm.mainMenu(); } }
时间: 2024-10-29 19:11:10