2014-02-22 2 views
1

특정 요구 사항이있는 프로그램을 빌드하려고합니다. 주요 클래스가 있고, 그런 다음 기능을 추가하는 서브 클래스를 만듭니다. 클래스 DVD를 만든 다음 하위 클래스를 만듭니다.서브 클래스 및 수퍼 클래스

나는 목록에 연도를 추가하는 방법과 인쇄되는 최종 재고 값에 추가되는 재입고 수수료를 추가하고 있습니다. 우선 서브 클래스를 구축 해, 오버라이드 (override) 한 메소드를 작성 했습니다만, 표시된 출력에 추가되지 않습니다. 뿐만 아니라 입력 연도를 잘못된 위치에 놓습니다. 어떤 오류도 발생하지 않고, DVD 클래스가 메서드 중 일부를 재정의한다고하더라도 하위 클래스가 존재하지 않는 것처럼 동작합니다.

나는 새 메소드를 호출해야하는 곳에서 뭔가 빠져 있어야한다고 생각하고있다. 아마도 잘못된 리소스를 읽을 수도 있지만, DVD 클래스를 호출하기 만하면된다. 자동으로 재정의됩니다. 이 정보를 수퍼 클래스에 추가하는 것을 선호하지만, 할당에 대한 요구 사항입니다.

그래서 이러한 새로운 기능을 추가해야 할 때 이러한 재정의 메서드를 실제로 호출하는 방법에 대해 궁금합니다. 리소스를 만드는 방법을 알려주지 만 실제로 구현하지는 않습니다.

내 주된 방법에서 dvd 클래스를 호출 한 다음 인쇄합니다. 그러나 제품 ID가 있어야하는 위치에 연도를 추가하는 것 외에는 원래 DVD 클래스에있는 것만 인쇄합니다.

public class DVD { 



String name; 
int id; 
int items; 
double cost; 

//default constructor 
public DVD() { 
    name = ""; 
    id = 0; 
    items = 0; 
    cost = 0.0; 
}//end default constructor 

//constructor to initialize object 
public DVD(String dvdName, int itemNum, int quantity, double price) { 
    name = dvdName; 
    id = itemNum; 
    items = quantity; 
    cost = price; 
}//end constructor 


//method to calculate value 
public double getInventoryValue() { 
     return items * cost; 
} 

//method to set name 
public void setdvdName(String dvdName){ 
    this.name = dvdName; 
} 

//method to get name 
public String getName(){ 
    return name; 
} 

//method to set id 
public void setitemNum(int itemNum){ 
    this.id = itemNum; 
} 

//method to get id 
public int getId(){ 
    return id; 
} 

//method to set items 
public void setquantity(int quantity){ 
    this.items = quantity; 
} 

//method to get items 
public int getItems(){ 
    return items; 
} 

//method to set cost 
public void setprice(double price){ 
    this.cost = price; 
} 

//method to get cost 
public double getCost(){ 
    return cost; 
} 

/** 
* 
* @return 
*/ 

public String toString() { 

    return "DVD Name: " + getName() + 
      "ID: " + getId() + 
      "Items: " + getItems() + 
      "Cost: " + getCost() + 
      "Total Value: " +getInventoryValue(); 
} 
} 

-

public class ExtendedDVD extends DVD{ 
double restockFee; 
int year; 

public ExtendedDVD(){ 
    year = 0; 
} 
public ExtendedDVD(int year) { 
    this.year = year; 
} 

public void setRestockFee(){ 
    this.restockFee = 0.05; 
} 

public double getRestockFee(){ 
    return restockFee; 
} 


public void setYear(){ 
    this.year = 0; 
} 

public int getYear(){ 
    return year; 
} 

@Override 
public double getInventoryValue(){ 
    double value1 = super.getInventoryValue(); 
    double value = restockFee * value1; 
    double totalInventoryValue = value + super.getInventoryValue(); 
    return totalInventoryValue; 
} 

@Override 
public String toString(){ 
    return super.toString() + "Year" + getYear(); 
} 
} 

}

public class Inventory { 

DVD[] inventory = new DVD[5]; 
int current = 0; 
private int len; 

public Inventory(int len){ 
    inventory = new DVD[len]; 

} 

public double calculateTotalInventory() { 
    double totalValue = 0; 
    for (int j = 0; j < inventory.length; j++) 
     totalValue += inventory[j].getInventoryValue(); 
    return totalValue; 
} 

/** 
* 
* @param dvd 
* @throws Exception 
*/ 
public void addDVD(DVD dvd) throws Exception { 
    if (current < inventory.length) { 
     inventory[current++]=dvd; 
    }else { 
     Exception myException = new Exception(); 
     throw myException; 

    } 
    } 
void sort() { 
     for (DVD inventory1 : inventory) { 
     len = current; 
    } 

     for (int i=0; i<len;i++) { 
     for(int j=i;j<len;j++) { 
      if (inventory[i].getName().compareTo(inventory[j].getName())>0) { 
       DVD temp = inventory[j]; 
       inventory[j] = inventory[i]; 
       inventory[i] = temp; 
      } 
     } 
    } 
} 




    public int getNumberOfItems() { 
    return current; 
} 


    public void printInventory() { 
    System.out.println("Current Inventory:"); 
    for(int i=0;i<current;i++) { 
     System.out.println(inventory[i]); 
    } 
    System.out.println("The total value of the inventory is:"+calculateTotalInventory()); 
    } 
} 

-

public class inventoryprogram1 { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args){ 


    boolean finish = false; 
    String dvdName; 
    int itemNum; 
    int quantity; 
    double price; 
    int year = 0; 

    Inventory inventory = new Inventory(5); 
    while (!finish) { 
     Scanner input = new Scanner(System.in); // Initialize the scanner 
     System.out.print("Please enter name of DVD: "); 
     dvdName = input.nextLine(); 
     if (dvdName.equals("stop")) { 
      System.out.println("Exiting Program"); 
      break; 
     } else { 
      System.out.print("Please enter Product Number: "); 
      itemNum = input.nextInt(); 
      System.out.print("Please enter units: "); 
      quantity = input.nextInt(); 
      System.out.print("Please enter price of DVD: "); 
      price = input.nextDouble(); 
      System.out.print("Please enter production year: "); 
      itemNum = input.nextInt(); 

      DVD dvd= new DVD(dvdName,itemNum,quantity,price); 

      try { 
       inventory.addDVD(dvd); 
      }catch(Exception e) { 
       System.out.println("Inventory is full."); 
       break; 
      } 

      System.out.println("DVD: " + dvd); 

     }//end else 

    } 
     inventory.sort(); 
     inventory.printInventory(); 
} 
} 
+3

만약 당신이 어떤 ExtendedDVD도 만들지 않는다면, 분명히, e ExtendedDVD 클래스가 호출됩니다. –

답변

1

의 다른 생성자를 할 수 있습니다, 분명히

DVD dvd= new ExtendedDVD(year); 

와 같은 무언가에 의해

DVD dvd= new DVD(dvdName,itemNum,quantity,price); 

교체 그 클래스를 인스턴스화 할 필요가 있으면 여전히 원래의 DVD 클래스를 호출하므로 해당 메소드를 여전히 얻을 수 있습니다.예를 들어

DVD dvd = new DVD(dvdName, itemNum, quantity, price); 

DVD Dvd = new ExtendedDVD(dvdName, itemNum, quantity, price); 

은 왜 당신에게를 보여주는 다른 두 가지

또한 당신은이 두 번 itemNum이를 할당하는 기본 방법에 보면

은 년

+0

잡기를위한 감사합니다, 나는 몇몇 선을 베끼고 더 빠를 것을 완전하게 놓쳤다. 여기가 내가 혼란스러워하는 곳입니다. 오라클 워드 프로세서에서도 인스턴스화하는 방법을 설명하지 못했고, 슈퍼 클래스와 함께 호출되었는지 또는 특별히 호출해야하는지 확실하지 않았습니다. – Laterali

+0

코드를 작성한 사람이라면 누구나 볼 수 있습니다. –

1

의 주요 방법 당신은 단지 DVD 객체가 아닌 ExtendedDVD 객체를 인스턴스화합니다. 당신은 당신이 ExtendedDVD 당신을 쓴 새로운 방법을 사용하려면

당신이 ExtendedDVD

관련 문제