2016-09-23 54 views
-1

這是我必須實現繼承,多態和io的項目。
希望有人能幫助我。我有一個帶有行李數據的文本文件。將輸入文件讀入抽象超類java

地方;揹包;紐約;黑色; 45.00; 5
地方;揹包;瑞士 馬球;灰色; 59.00; 1
地方;揹包;比爾·基思;藍50.00; 2
進口;手袋; Padini ;黃色; 120.00; 3
進口;手提包; PDI;紅色; 170.00; 2
導入;手袋,P &CO;綠色; 200.00; 1

我有構造函數,訪問和toString方法的抽象超其由ITEMTYPE,bagType,品牌,顏色,價格和數量的根據數據分別爲

我也有2個子類,導入和本地這是從超類包擴展,從超類繼承相同的屬性。

我有問題將數據讀入抽象超類的數組,因爲它會說抽象類不能實例化。是的,我知道這一點。那麼我該怎麼做才能將數據讀入數組?

這是我的抽象超Bag

public abstract class Bag 
 
{ 
 
    private String bagType; 
 
    private String colour; 
 
    private String brand; 
 
    private int quantity; 
 
    private double price; 
 
    
 
    //default constructor 
 
    public Bag() 
 
    { 
 
     bagType = " "; 
 
     colour = " "; 
 
     brand = " "; 
 
     quantity = 0; 
 
     price = 0.00; 
 
    } 
 

 
    //normal constructor 
 
    public Bag(String bagType, String brand, String colour, double price, int quantity) 
 
    { 
 
     this.bagType = bagType; 
 
     this.brand = brand; 
 
     this.colour = colour; 
 
     this.price = price; 
 
     this.quantity = quantity; 
 
    } 
 

 
    //accessor method 
 
    public String getColour() 
 
    { 
 
     return colour; 
 
    } 
 
    public String getBrand() 
 
    { 
 
     return brand; 
 
    } 
 
    public int getQuantity() 
 
    { 
 
     return quantity; 
 
    } 
 
    public double getPrice() 
 
    { 
 
     return price; 
 
    } 
 
    
 
    //mutator method 
 
    public void setBagType(String bt) 
 
    { 
 
     bagType = bt; 
 
    } 
 
    public void setColour(String c) 
 
    { 
 
     colour = c; 
 
    } 
 
    public void setBrand(String b) 
 
    { 
 
     brand = b; 
 
    } 
 
    public void setQuantity(int q) 
 
    { 
 
     quantity = q; 
 
    } 
 
    public void setPrice(double p) 
 
    { 
 
     price = p; 
 
    } 
 

 
    //abstract method 
 
    public abstract double calcPrice(); 
 

 
    //toString method 
 
    public String toString() 
 
    { 
 
     return 
 
     "\nBag Type  : " + bagType + 
 
     "\nItem Colour : " + colour + 
 
     "\nItem Brand : " + brand + 
 
     "\nItem Quantity : " + quantity + 
 
     "\nPrice   : " + price; 
 
    } 
 
}

這是我的測試類:

import java.util.*; 
 
import java.io.*; 
 

 
public class TestBag 
 
{ 
 
    public static void main(String [] args) 
 
    { 
 
     try 
 
     { 
 
      BufferedReader in = new BufferedReader(new FileReader("Bag.txt")); 
 

 
      PrintWriter Import = new PrintWriter(new BufferedWriter(new FileWriter("Import.txt"))); 
 
      PrintWriter Local = new PrintWriter(new BufferedWriter(new FileWriter("Local.txt"))); 
 
      
 
      String data = null; 
 
      int size = 0; 
 
      while((data = in.readLine()) != null)/**read data to count the no of records*/ 
 
      { 
 
       StringTokenizer input = new StringTokenizer(data,";"); 
 
       size++; 
 

 
      } 
 
      in.close(); 
 
      
 
      /**reopen the input file*/ 
 
      in = new BufferedReader(new FileReader("Bag.txt")); 
 
      Bag b [] = new Bag[size]; 
 
      int index = 0; 
 
      while((data = in.readLine()) != null) //read the contents of file and process the data 
 
      { 
 
       StringTokenizer inData = new StringTokenizer(data,";"); 
 
       
 
       String type = inData.nextToken(); 
 
       String bagType = inData.nextToken(); 
 
       String brand = inData.nextToken(); 
 
       String colour = inData.nextToken(); 
 
       double price = Double.parseDouble(inData.nextToken()); 
 
       int quantity = Integer.parseInt(inData.nextToken()); 
 
       
 
       if(type.equalsIgnoreCase("Import")) //Import 
 
       { 
 
        b[index] = new Import(bagType, brand, colour, price, quantity); 
 
        System.out.println(b[index].toString()); 
 
        
 
       } 
 
       else if(type.equalsIgnoreCase("Local")) 
 
       { 
 
        b[index] = new Local(bagType, brand, colour, price, quantity); 
 
        System.out.println(b[index].toString()); 
 
       } 
 
       
 
       index++; 
 
      } 
 
      
 
      // 
 
      
 
      in.close(); 
 
      Import.close(); 
 
      Local.close(); 
 
      
 
     } 
 
     //end of try 
 
     catch(FileNotFoundException fnfe) 
 
     { System.out.print(fnfe.getMessage());} 
 
     catch(IOException io) 
 
     { System.out.print(io.getMessage());} 
 
     catch(Exception e) 
 
     { System.out.print(e.getMessage());} 
 
    } 
 

 
}

對不起。我還是新來的Java。

+0

我可以在您發佈的代碼中看不到抽象類。 –

+0

抽象類是在超類中的Bag類中,這是測試我的代碼的類testBag。 – Nabella

+0

對不起,我的水晶球壞了,所以除非你展示它,否則我看不到你的'Bag'類。 –

回答

-1

如果您的Bag是抽象的,那麼不可能調用new Bag()

您將需要檢查bagType並根據它創建一個類。

if(type.equals("import")){ 
    b[index] = new ImportBag (type, bagType, brand, colour, price, quantity); 
} else { 
    b[index] = new LocalBag (type, bagType, brand, colour, price, quantity); 
} 

Arraylist可能比Array更有用。

+0

在您的拼寫錯誤和您對@OP所需/想要的假設之間,這是一個不合格的答案 – ControlAltDel

+0

對於拼寫錯誤,我很抱歉。英語不是我的mothertonuge,我用我的智能手機寫這個。你能告訴我如何改善我的答案嗎? – noCma

+0

我已經爲子類創建了數組,但是當我嘗試編譯時,出現'For input string:「5」' – Nabella

0

首先,在這裏閱讀關於抽象類:Abstract Methods and Classes,我也沒有看到任何承包商在你的代碼...你需要自己創建它們嗎?

+0

抽象類在類Bag中,它是超類,這是類testBag,在那裏測試我的代碼。所有構造函數都在超類Bag和子類Import,Local上。 – Nabella