欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DVD案例(对象数组)

程序员文章站 2022-06-09 12:49:27
...

DVDSet(Java bean)

public class DVDSet {
	private int id;
	private String name;
	private int state;
	private String date;
	private int count;

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

}

DVDMgr(方法类)

import java.util.Date;
import java.awt.geom.Dimension2D;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

public class DVDMgr {

	DVDSet[] dvdSets = new DVDSet[10];

	// 数组初始化赋值--------------------
	public void initial() {

		for (int i = 0; i < dvdSets.length; i++) {
			DVDSet dvdSet = new DVDSet();
			dvdSets[i] = dvdSet;
			dvdSets[i].setId(i + 1);
		}

		dvdSets[0].setName("罗马假日");
		dvdSets[1].setName("风声鹤唳");
		dvdSets[2].setName("浪漫满屋");
		dvdSets[0].setState(0);
		dvdSets[1].setState(1);
		dvdSets[2].setState(1);
		dvdSets[0].setDate("2010-7-1");
		dvdSets[0].setCount(15);
		dvdSets[1].setCount(12);
		dvdSets[2].setCount(30);

	}

	// 查看DVD-----------------------
	public void search() {
		System.out.println("--->  查看DVD");
		System.out.println("序号" + "\t" + "状态" + "\t" + "名称" + "\t" + "借出日期");
		String state = "";
		for (int i = 0; dvdSets[i].getName() != null; i++) {
			switch (dvdSets[i].getState()) {
			case 0:
				state = "借出";
				break;
			case 1:
				state = "可借";
				break;

			default:
				break;
			}
			if (dvdSets[i].getDate() == null) {
				dvdSets[i].setDate("");
			}
			System.out.println(
					dvdSets[i].getId() + "" + '\t' + dvdSets[i].getName() + '\t' + state + '\t' + dvdSets[i].getDate());

		}

	}

	// 新增DVD----------------------
	public void add() {
		System.out.println("---》新增DVD");
		Scanner input = new Scanner(System.in);
		System.out.println("请输入DVD名称");
		String name1 = input.next();
		for (int i = 0; i < dvdSets.length; i++) {
			if (dvdSets[i].getName() == null) {
				dvdSets[i].setName(name1);
				dvdSets[i].setState(1);
				break;
			}
		}

		System.out.println("新增" + name1 + "成功");
		System.out.println("********************************");

	}

	// 删除DVD----------------------
	public void delete() {
		System.out.println("---》删除DVD");
		Scanner input = new Scanner(System.in);
		System.out.println("请输入要删除的DVD名称");
		String name = input.next();
		int i = 0;
		boolean x = false;
		
		
		for (; dvdSets[i].getName() != null; i++) {
			if (dvdSets[i].getName().equals(name)) {
				x = true;
				switch (dvdSets[i].getState()) {
				case 0:
					System.out.println(name + "为借出状态,不能删除");
					break;
				case 1:
					dvdSets[i] = dvdSets[i + 1];
					System.out.println(name + "删除成功");
					break;

				}

			} else {
				x = false;
			}

		}
		if (x == false) {
			System.out.println("该书不存在");
		}

	}

	// 借出DVD----------------------
	public void lend() {
		System.out.println("----》借出DVD");
		
		Scanner input = new Scanner(System.in);
		System.out.println("请输入DVD名称:");
		String name = input.next();
		boolean x = false;
		for (int i = 0; dvdSets[i].getName() != null; i++) {
			if (dvdSets[i].getName().equals(name) && dvdSets[i].getState() == 1) {
				x = true;
				int c =dvdSets[i].getCount();
				c++;
			    dvdSets[i].setCount(c);
				dvdSets[i].setState(0);
				System.out.println("请输入借出日期(年-月-日):");
				String date = input.next();
				dvdSets[i].setDate(date);
			} else {
				x = false;

			}

		}
		if (x == true) {

			System.out.println(name + "借出成功");
			
		} else {
			System.out.println(name + "不存在");
		}

	}

	// 归还DVD--------------------
	public void returnDVD() {
		System.out.println("---->归还DVD");
		Scanner input = new Scanner(System.in);
		System.out.println("请输入DVD名称:");
		String name = input.next();
		System.out.println("请输入归还日期:");
		String date = input.next();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		boolean x = false;
		Date d1 = null;
		Date d2 = null;

		for (int i = 0; dvdSets[i].getName() != null; i++) {
			if (dvdSets[i].getName().equals(name) && dvdSets[i].getState() == 0) {
				dvdSets[i].setState(1);
				System.out.println(name + "归还成功");
				System.out.println("借出日期为:" + dvdSets[i].getDate());
				System.out.println("归还日期为:" + date);

				try {
					d1 = simpleDateFormat.parse(dvdSets[i].getDate());

				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					d2 = simpleDateFormat.parse(date);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

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

				x = true;

			}
		}
		if (x == true) {
			long charge = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000);
			double sal = charge * 10;
			System.out.println("应付租金(元):" + sal);

		}

	}
	//借出排行榜
	public void list(){
		for(int i = 0;i<dvdSets.length;i++){
			for(int j = 0;j<dvdSets.length-1;j++){
				if(dvdSets[j].getCount()<dvdSets[j+1].getCount()){
					DVDSet temp = dvdSets[j+1];
					dvdSets[j+1] = dvdSets[j];
					dvdSets[j] = temp;
							
				}
			}
		}
		System.out.println("次数"+'\t'+"名称");
		for(int i =0;dvdSets[i].getName()!=null;i++){
			System.out.println(dvdSets[i].getCount()+""+'\t'+dvdSets[i].getName());
		}
	}

}

Start(测试类)

import java.util.Scanner;
public class Start {
	DVDMgr dvdMgr = new DVDMgr();

	public void startMenu() {
		System.out.println("欢迎使用迷你DVD管理器");
		System.out.println("-------------------------");
		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.退出");
		System.out.println("0.借出排行榜");
		System.out.println("-------------------------");
		System.out.println("请选择");
		Scanner input = new Scanner(System.in);
		int choice = input.nextInt();
		switch (choice) {
		case 1:// 新增DVD
			dvdMgr.add();
			returnMain();
			break;
		case 2:// 查看DVD
			dvdMgr.search();
			returnMain();
			break;
		case 3:// 删除DVD
			dvdMgr.delete();
			returnMain();
			break;
		case 4:// 借出DVD
			dvdMgr.lend();
			returnMain();
			break;
		case 5:// 归还DVD
			dvdMgr.returnDVD();
			returnMain();
			break;
		case 6:// 退出
			System.out.println("谢谢使用");
			break;
		case 0://排行榜
			dvdMgr.list();
			returnMain();
		default:
			break;
		}

	}

	public void returnMain() {
		Scanner input = new Scanner(System.in);
		boolean b1 = true;
		int go;
		do {
			System.out.println("输入0返回");
			go = input.nextInt();
			if (go == 0) {
				startMenu();

			} else {
				System.out.println("输入有误");
				System.out.println("是否继续输入0返回   y/n");
				String yn = input.next();
				if (yn.equals("y")) {
					b1 = true;
					break;
				}
			}
		} while (b1);
	}

	public static void main(String[] args) {

		Start start = new Start();
		start.dvdMgr.initial();
		start.startMenu();

	}
}



相关标签: 案例