package cn.zc.com; import java.util.Date; public class DVD { public int id; public String nema; public boolean stste=true; public Date date; }
package cn.zc.com; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class DVDMgr { public DVDMgr() { init();// 初始化方法 } SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Scanner input=new Scanner(System.in); // DVD[] dvds = new DVD[100]; public void init() { String[] name = new String[] { "赌神", "赌圣", "赌侠", "邪恶力量", "地心引力" }; //给dvd初始化值 数组名 赋值 for (int i = 0; i < name.length; i++) { dvds[i] = new DVD(); dvds[i].id = i; dvds[i].nema = name[i]; } } public void menu() { p("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); p("++++++++++++++++++欢迎你使用迷你版DVD++++++++++++++++++"); p("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); p("1.新曾DVD"); p("2.查看DVD"); p("3.删除DVD"); p("4.借出DVD"); p("5.归还DVD"); p("6.退出DVD"); p("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); p("请选择:"); } public void show() { while (true) { menu(); int ch = input.nextInt(); switch (ch) { case 1: p("1.新曾DVD"); add(); break; case 2: p("2.查看DVD"); showDVd(); break; case 3: p("3.删除DVD"); del(); break; case 4: p("4.借出DVD"); borrow(); break; case 5: p("5.归还DVD"); revert(); break; case 6: p("6.退出DVD"); return; default: p("输入错误"); break; } p("输入0返回"); input.next(); } } private void revert() { p("请输入归还dvd的编码:"); int id=input.nextInt(); for (int i = 0; i < dvds.length; i++) { if(dvds[i]!=null&& dvds[i].id==id){ if(!dvds[i].stste){ dvds[i].stste=true; int day=(int)Math.ceil((System.currentTimeMillis()-dvds[i].date.getTime())*1.0/1000/60/60/24); System.out.println("归还成功!"); p("借出日期:"+sdf.format(dvds[i].date)); p("归还日期"+sdf.format(new Date())); p("应付租金"+day*1.0+"¥"); }else{ p("不能归还"); } return; } } p("归还失败"); } private void borrow() { showDVd(); System.out.println("请输入要借的dvd编码:"); int id=input.nextInt(); for (int i = 0; i < dvds.length; i++) { if(dvds[i]!=null&& dvds[i].id==id){ if(dvds[i].stste){ dvds[i].stste=false; dvds[i].date=new Date(); System.out.println("借出成功!"); }else{ p("不能借出"); } return; } } p("还不能借出"); } private void del() { showDVd(); System.out.println("请输入要删除dvd编码:"); int id=input.nextInt(); for (int i = 0; i < dvds.length; i++) { if(dvds[i]!=null&& dvds[i].id==id){ if(dvds[i].stste){ dvds[i]=null; System.out.println("删除成功!"); }else{ p("抱歉,不能删除"); } return; } } p("删除失败"); } // 新增 private void add() { for (int i = 0; i < dvds.length; i++) { if (dvds[i] == null) { dvds[i] = new DVD(); p("请输入要增加的dvd的名称"); dvds[i].nema = input.next(); dvds[i].id = getId(); p("是否继续y/n"); char a = input.next().toLowerCase().charAt(0); if (a == 'n') { break; } } } } private int getId() { int max = 0; for (DVD dvd : dvds) { // 如果当前元素不为空 并且当前元素>id最大值 给最大的值赋值 if (dvd != null && dvd.id > max) { max = dvd.id; } } return max + 1; } // 查看 三元表达式 private void showDVd() { p("序号\t名称\t\t状态\t借出日期"); for (DVD dvd : dvds) { if (dvd != null) { // 三元表达式 String date = dvd.date == null ? "" :sdf.format(dvd.date) ; String s = dvd.stste ? "可借" : "对不起,已借出"; p(dvd.id + "\t" + dvd.nema + "\t\t" + s + "\t" + date); } } } public void p(Object obj) { System.out.println(obj); } }
package cn.zc.com; public class TsetDVDMgr { public static void main(String[] args) { DVDMgr dvd=new DVDMgr(); dvd.show(); } }
DVD
最新推荐文章于 2024-07-10 23:24:39 发布