這是我必須實現繼承,多態和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。
我可以在您發佈的代碼中看不到抽象類。 –
抽象類是在超類中的Bag類中,這是測試我的代碼的類testBag。 – Nabella
對不起,我的水晶球壞了,所以除非你展示它,否則我看不到你的'Bag'類。 –