java迷你DVD管理器源代码

public class DVDSet {

//声明DVD名、状态等属性

String[] name = new String[50];

int[] state = new int[50];

String[] date = new String[50];

int[] count = new int[50];

String[] name1 = new String[50];

}

import java.util.*;

import java.text.SimpleDateFormat;

public class DVDMgr {

DVDSet dvd = new DVDSet();

public void initial() {

dvd.name[0] = "暮光之城";

dvd.state[0] = 1;

dvd.date[0] = "2011-9-17";

dvd.count[0] = 0;

dvd.name[1] = "越狱1-4季";

dvd.state[1] = 1;

dvd.date[1] = "2011-9-17";

dvd.count[1] = 0;

dvd.name[2] = "吸血鬼日记";

dvd.state[2] = 1;

dvd.date[2] = "2011-9-17";

dvd.count[2] = 0;

}

public void startMenu() {

System.out.println("---------------欢迎使用迷你DVD管理器-------------\n");

System.out.println("\t\t0.借出排行榜.");

System.out.println("\t\t1.新增DVD.");

System.out.println("\t\t2.查看DVD.");

System.out.println("\t\t3.删除DVD.");

System.out.println("\t\t4.借出DVD.");

System.out.println("\t\t5.归还DVD.");

System.out.println("\t\t6.退出.\n");

System.out.println("----------------------------------------------");

Scanner input = new Scanner(System.in);

System.out.print("请选择操作,按对应的数字:");

int choice = input.nextInt();

switch (choice) {

case 0:

list();

break;

case 1:

addDVD();

break;

case 2:

chaKan();

break;

case 3:

deleteDVD();

break;

case 4:

lendDVD();

break;

case 5:

returnDVD();

break;

case 6:

System.out.println("感谢使用");

break;

}

}

public void chaKan(){

System.out.println("-------------------------------------->查看DVD\n");

System.out.println("序号\t状态\t名称\t\t借出日期");

int dizhi = 0;

for (int i = 0; i <= dvd.name.length; i++) {

if (dvd.name[i] != null) {

continue;

} else {

dizhi = i - 1;

break;

}

}

for (int i = 0; i <= dizhi; i++) {

if (dvd.state[i] == 0) {

System.out.println(i + "\t" + "已借出" + "\t" + dvd.name[i]

+ "\t\t" + dvd.date[i]);

} else if (dvd.state[i] == 1) {

System.out.println(i + "\t" + "可借" + "\t" + dvd.name[i]

+ "\t\t");

}

}

returnMain();

}

public void returnMain() {

Scanner input = new Scanner(System.in);

System.out.print("输入0返回主菜单:");

if (input.nextInt() == 0) {

startMenu();

} else {

}

}

public void addDVD() {

Scanner input = new Scanner(System.in);

System.out.println("-------------------------------------->新增DVD\n");

System.out.print("请输入DVD名称:");

for (int i = 0; i < dvd.name.length; i++) {

if (dvd.name[i] == null) {

dvd.name[i] = input.next();

System.out.print("请输入日期:");

dvd.date[i] = input.next();

dvd.state[i] = 1;

System.out.println(dvd.name[i] + "新增成功!");

returnMain();

} else {

continue;

}

}

}

public void deleteDVD() {

Scanner input = new Scanner(System.in);

System.out.println("-------------------------------------->删除DVD\n");

System.out.print("请输入要删除的DVD名称:");

String name = input.next();

for (int i = 0; i < dvd.name.length; i++) {

if (dvd.name[i] != null && dvd.name[i].equals(name)

&& dvd.state[i] == 1) {

int j = i;

while (dvd.name[j + 1] != null) {

dvd.name[j] = dvd.name[j + 1];

dvd.state[j] = dvd.state[j + 1];

dvd.date[j] = dvd.date[j + 1];

j++;

}

dvd.name[j] = null;

dvd.date[j] = null;

System.out.println("删除" + name + "成功");

returnMain();

break;

} else if (dvd.name[i] != null

&& dvd.name[i].equalsIgnoreCase(name) && dvd.state[i] == 0) {

System.out.println(dvd.name[i] + "已经借出,无法删除!");

returnMain();

}

}

}

public void lendDVD() {

Scanner input = new Scanner(System.in);

System.out.println("-------------------------------------->借出DVD\n");

System.out.print("请输入要借的DVD名称:");

String lend = input.next();

for (int i = 0; i < dvd.name.length; i++) {

if (dvd.name[i] == null) {

System.out.println("没有找到匹配信息!!!");

returnMain();

break;

} else if (dvd.name[i].equals(lend) && dvd.state[i] == 1) {

System.out.print("请输入借出日期(年-月-日):");

dvd.date[i] = input.next();

dvd.state[i] = 0;

System.out.println(lend + "\t借出成功!");

dvd.count[i]++;

dvd.name1[i] = dvd.name[i];

returnMain();

break;

} else if (dvd.name[i].equals(lend) && dvd.state[i] == 0) {

System.out.println(lend + "\t已借出!");

returnMain();

break;

}

}

}

public void returnDVD() {

Scanner input = new Scanner(System.in);

System.out.println("-------------------------------------->归还DVD\n");

System.out.print("请输入归还的DVD名称:");

String returndvd = input.next();

for (int i = 0; i <= dvd.name.length; i++) {

if (returndvd.equals(dvd.name[i]) && dvd.state[i] == 0) {

System.out.print("请输入归还的日期:");

String date = input.next();

System.out.println(returndvd + "归还成功!");

System.out.println("借出日期为:" + dvd.date[i]);

System.out.println("归还日期为:" + date);

String d1 = dvd.date[i];

String d2 = date;

long day = charge(d2, d1);

System.out.println("应付租金:" + day + "元");

dvd.date[i] = date;

dvd.state[i] = 1;

break;

}else if(i==49){

System.out.println("无该DVD,输入错误。");

break;

}

}

returnMain();

}

public long charge(String dstr1, String dstr2) {

long charge = 0;

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");

try {

Date d1 = sd.parse(dstr1);

Date d2 = sd.parse(dstr2);

charge = (d1.getTime() - d2.getTime())/ (24 * 60 * 60 * 1000);

} catch (Exception e) {

e.printStackTrace();

}

return charge;

}

public void list() {

String[] newName = new String[50];

int[] newcount = new int[50];

for (int i = 0; i < dvd.name.length; i++) {

newName[i] = dvd.name[i];

newcount[i] = dvd.count[i];

}

for (int i = 0; i < newName.length; i++) {

for (int j = i + 1; j < newName.length; j++) {

if (newcount[i] > newcount[j]) {

int zhongjianbianliang = newcount[i];

newcount[i] = newcount[j];

newcount[j] = zhongjianbianliang;

String zhongjianliang = newName[i];

newName[i] = newName[j];

newName[j] = zhongjianliang;

}

}

}

System.out.println("次数\t名称");

for (int i = dvd.name.length - 1; i >= 0; i--) {

if (newName[i] != null) {

System.out.println(newcount[i] + "次" + "\t" + newName[i]);

}

}

returnMain();

}

}

//创建测试类,调用DVDMgr类中的方法initial()和startMenu()

public class TestDVD {

public static void main(String[] args) {

DVDMgr dm = new DVDMgr();

dm.initial();

dm.startMenu();

}

}

Java免费学习资料直播公开课群175161984

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页