天天看點

DVD管理系統:為某音像店開發一個迷你DVD管理器,實作DVD碟片的管理,包括如下功能:



package com.sxt.oop.dvd;

public class Dvd implements Comparable<Dvd>{

 //存放DVD名

 private String dvdNames ;

 //存放借出狀态的數組,false為可借,true為不可借

 private boolean status ;

 //存放借DVD日期

 private String loanDates ;

 //DVD借出次數

 private int count;

 public Dvd(){

 }

 public Dvd(String dvdNames) {

  this.dvdNames = dvdNames;

 }

 public Dvd(String dvdNames, boolean status, String loanDates) {

  super();

  this.dvdNames = dvdNames;

  this.status = status;

  this.loanDates = loanDates;

 }

 public String getDvdNames() {

  return dvdNames;

 }

 public void setDvdNames(String dvdNames) {

  this.dvdNames = dvdNames;

 }

 public boolean isStatus() {

  return status;

 }

 public void setStatus(boolean status) {

  this.status = status;

 }

 public String getLoanDates() {

  return loanDates;

 }

 public void setLoanDates(String loanDates) {

  this.loanDates = loanDates;

 }

 public int getCount() {

  return count;

 }

 public void setCount(int count) {

  this.count = count;

 }

 @Override

 public int hashCode() {

  final int prime = 31;

  int result = 1;

  result = prime * result

    + ((dvdNames == null) ? 0 : dvdNames.hashCode());

  result = prime * result

    + ((loanDates == null) ? 0 : loanDates.hashCode());

  result = prime * result + (status ? 1231 : 1237);

  return result;

 }

 @Override

 public boolean equals(Object obj) {

  if (this == obj)

   return true;

  if (obj == null)

   return false;

  if (getClass() != obj.getClass())

   return false;

  Dvd other = (Dvd) obj;

  if (dvdNames == null) {

   if (other.dvdNames != null)

    return false;

  } else if (!dvdNames.equals(other.dvdNames))

   return false;

  if (loanDates == null) {

   if (other.loanDates != null)

    return false;

  } else if (!loanDates.equals(other.loanDates))

   return false;

  if (status != other.status)

   return false;

  return true;

 }

 @Override

 public String toString() {

  return   (status?"已借出":"可借")+"\t\t<<"+dvdNames+">>\t\t\t"+(loanDates==null?"":loanDates)+"\t\t"+count+"\n";

 }

 @Override

 public int compareTo(Dvd o) {

  return o.count-this.count;

 }

 public void increment(){

  count++;

 }

}

package com.sxt.oop.dvd;

import java.text.ParseException;

import java.util.Arrays;

import java.util.Date;

public class DvdShop {

 //存放多個dvd對象

 private  Dvd[] dvds;

 private  String name;

 //記錄元素的位置 :top的位置是有内容的

 private int top=-1;

 public DvdShop(){

  dvds = new Dvd[50];

 }

 public DvdShop(String name){

  this();

  this.name = name;

    }

 public DvdShop(int capacity,String name){

  if(capacity<=0){

   throw new RuntimeException("容量不能小于等于0");

  }

  dvds = new Dvd[capacity];

  this.name = name;

 }

 public String getName() {

  return name;

 }

 public void setName(String name) {

  this.name = name;

 }

 @Override

 public int hashCode() {

  final int prime = 31;

  int result = 1;

  result = prime * result + Arrays.hashCode(dvds);

  result = prime * result + ((name == null) ? 0 : name.hashCode());

  result = prime * result + top;

  return result;

 }

 @Override

 public boolean equals(Object obj) {

  if (this == obj)

   return true;

  if (obj == null)

   return false;

  if (getClass() != obj.getClass())

   return false;

  DvdShop other = (DvdShop) obj;

  if (!Arrays.equals(dvds, other.dvds))

   return false;

  if (name == null) {

   if (other.name != null)

    return false;

  } else if (!name.equals(other.name))

   return false;

  if (top != other.top)

   return false;

  return true;

 }

 @Override

 public String toString() {

  return "DvdShop [dvds=" + Arrays.toString(dvds) + ", name=" + name

    + ", top=" + top + "]";

 }

 public int indexOf(String name){

  int index=-1;

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

   if(name.equals(dvds[i].getDvdNames())){

    index= i;

    break;

   }

  }

  return index;

 }

 public boolean isFull(){

  return  top==dvds.length-1;

 }

 public boolean isEmpty(){

  return top==-1;

 }

 public boolean  rent(String name){

  int index= indexOf(name);

  if(index==-1){

   throw new RuntimeException("此 dvd不存在,借出失敗");

  }

  if(dvds[index].isStatus()){

   throw new RuntimeException("此 dvd已借出,借出失敗");

  }

  Dvd  dvd =dvds[index];

  dvd.setStatus(true);

  dvd.setLoanDates(DateHelper.getDateStr());

  //dvd.setCount(dvd.getCount()+1);

  dvd.increment();

  return true;

 }

 public  Dvd  get(int index){

  if(index<0 ||index>top){

   throw new RuntimeException("位置不存在");

  }

  return dvds[index];

 }

 public int size(){

  return top+1;

 }

 public boolean add(String name){

  //如果滿了抛出異常

  if(isFull()){

   throw new RuntimeException("存儲空間已滿");

  }

  //index為新輸入dvd的位置,如果>=0,則dvd存在

  int index = indexOf(name);

  if(index>=0){

   throw new RuntimeException("此dvd已存在");

  }

  //top為-1,是以要先++

  dvds[++top] = new Dvd(name);

  return true;

 }

 public boolean delete(String name){

  //如果為空抛出異常

  if(isEmpty()){

   throw new RuntimeException("存儲空間為空,無法删除");

  }

  //找dvd的位置

  int index = indexOf(name);

  //如果不存在,傳回false

  if(index==-1){

   throw new RuntimeException("此 dvd不存在,借出失敗");

  }

  //如果存在 通過移位來覆寫被删除的元素

  for(int i=index;i<=top;i++){

   dvds[i] =dvds[i+1];

  }

  //改變top

   top--;

   return true;

 }

 public boolean loan(String name) {

  //如果為空抛出異常

  if(isEmpty()){

   throw new RuntimeException("存儲空間為空,無法借出");

  }

  //找dvd的位置

  int index = indexOf(name);

  //如果不存在,傳回false

  if(index==-1){

   throw new RuntimeException("此 dvd不存在,借出失敗");

  }

  if(dvds[index].isStatus()){

   throw new RuntimeException("此 dvd已借出,借出失敗");

  }

  //如果存在 将dvd的借出狀态改為true

  dvds[index].setStatus(true);

  //得到借出日期

  dvds[index].setLoanDates(DateHelper.getDateStr());

  //借出次數加1

  dvds[index].increment();

  return true;

 }

 public  int back(String name) throws ParseException {

  //如果滿了抛出異常

  if(isFull()){

   throw new RuntimeException("存儲空間已滿");

  }

  //找dvd的位置

  int index = indexOf(name);

  //如果不存在

  if(index==-1){

   throw new RuntimeException("此 dvd不存在,歸還失敗");

  }

  if(dvds[index].isStatus()==false){

   throw new RuntimeException("此 dvd未借出,歸還失敗");

  }

  //把借出狀态改為可借

  dvds[index].setStatus(false);

  //儲存日期的字元串,先先原來的資料copy一下,再傳新資料

  String loanDates_bak = dvds[index].getLoanDates();

  //把借出日期改為null

  dvds[index].setLoanDates(null);

  Date  date1 = DateHelper.getStrDate(loanDates_bak);

  Date  date2  = new Date();

  int days=DateHelper.getDays(date1, date2);

  //計算租金  :租金=日期差*(租金/每天)

  int rent = days*1;

  return rent; 

 }

 public void sort(){

  Arrays.sort(dvds,0,top+1);

 }

}

package com.sxt.oop.dvd;

import java.text.ParseException;

import java.util.Scanner;

public class DvdUI {

 private static DvdShop  shop =null;

 static Scanner  input =new Scanner(System.in);

 public DvdUI(String name){

  shop = new DvdShop(name);

 }

 public void init(){

  shop.add("獵場");

  shop.add("77天");

  shop.add("正義聯盟"); 

 }

 public void mainMenu() throws ParseException{

  System.out.println("歡迎使用<<"+shop.getName()+">>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("7.退        出");

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

  System.out.print("請選擇 :");

  int choice = input.nextInt();

  switch(choice) {

  case 1:

   add();

   break;

  case 2:

   show();

   break;

  case 3:

   delete();

   break;

  case 4:

   loan();

   break;

  case 5:

   back();

   break;

  case 6:

   sort();

   break;

  case 7:

   System.out.println("感謝您的使用,程式将在3秒後關閉.....");

   try {

    Thread.sleep(3000);

   } catch (InterruptedException e) {

    e.printStackTrace();

   }

   System.exit(0);

   break;

  }

  System.out.print("輸入任意鍵傳回:");

  input.next();

  mainMenu();

 }

 public  void add(){

  System.out.println("---->添加dvd");

  //1.提示使用者輸入dvd的名字

  System.out.print("請輸入需要入庫的dvd名稱:");

  String name = input.next();

  //3.将dvd的名字放進去

  try{

  shop.add(name);

  //4.提示成功

  System.out.println("添加<<"+name+">>成功");

  }catch (RuntimeException e) {

   System.out.println(e);

  }

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

 }

 public  void show(){

  System.out.println("--->檢視dvd");

  if(shop.isEmpty()){

   System.out.println("沒有任何dvd資訊");

   return;

  }

  System.out.println("序号\t狀态\t\t名稱\t\t\t借出日期\t\t\t借出次數");

  //循環輸出資訊

  for(int i=0;i<shop.size();i++){

      System.out.println(i+"\t"+shop.get(i));

  }

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

 }

 public static void delete() {

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

  System.out.println();

  System.out.print("請輸入DVD名稱:");

  String name = input.next();

  //3.将dvd的名字放進去

  try{

  shop.delete(name);

  //4.提示成功

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

  }catch (RuntimeException e) {

   System.out.println(e);

  }

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

 }

 public static void loan() {

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

  System.out.println();

  System.out.print("請輸入DVD名稱:");

  String name = input.next();

  try{

   shop.loan(name);

   //4.提示成功

   System.out.println("借出《"+name+"》成功!");

   }catch (RuntimeException e) {

    System.out.println(e);

   }

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

 }

 public static void back() throws ParseException {

  System.out.println("------>歸還DVD");

  System.out.println();

  System.out.print("請輸入DVD名稱:");

  String name = input.next();

  try{

   int rent = shop.back(name);

   System.out.println("租金為:"+rent);

   //4.提示成功

   System.out.println("歸還《"+name+"》成功!");

   }catch (RuntimeException e) {

    System.out.println(e);

   }

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

 }

 public  void sort(){

  System.out.println("排序DVD");

  shop.sort();

  show();

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

 }

}

package com.sxt.oop.dvd;

import java.text.ParseException;

public class Start {

  public static void main(String[] args) throws ParseException {

  DvdUI  ui =new DvdUI("僞裝者");

  ui.init();

  ui.mainMenu();

 }

}

package com.sxt.oop.dvd;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class DateHelper {

 public static String getDateStr() {

  //得到系統目前時間

  Date  date =new Date();

  //建構日期格式化對象,用于将字元串轉成日期對象

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

  String str = sdf.format(date);

  return str;

 }

 public static Date getStrDate(String strDate) throws ParseException{

  //建構日期格式化對象,用于将字元串轉成日期對象

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

  Date date = sdf.parse(strDate);

  return date;

 }

 public static int getDays(Date date1,Date date2){

  //擷取日期對應的毫秒數(1970-01-01 0點開始記錄的毫秒數) ,相減得到毫秒差

  long mills= Math.abs(date2.getTime()-date1.getTime());

  //将毫秒轉成天

  int days=(int)(mills/1000/60/60/24);

  return days;

 }

}

繼續閱讀