2016-03-28 14 views
0

我试图使用从小说代码这是书和书的孩子的价值也有另一个孩子非小说。以下是我到目前为止。我需要在bookTest文件中使用儿童的东西。从孩子访问数据并将其用于父测试课程。

public class Fiction extends Book 
{ 
private String fictionCode; 
private boolean signedByAuthor; 

public boolean isSignedByAuthor() 
{ 
    return signedByAuthor; 
} 

public void setSignedByAuthor(boolean signedByAuthor) 
{ 
    if (signedByAuthor == true) 
    { 
     this.signedByAuthor = signedByAuthor; 
    } 
    else 
    { 
     return; 
     } 
} 

public Fiction() 
{ 
    super(); 
    setFictionCode(""); 
} 

public Fiction(String title, String author, String isbn, Publisher publisher, double price, String fictionCode, int quantity,Date datePublished) 
{ 
super(title, author,isbn, publisher, price, quantity,datePublished); 
setFictionCode(fictionCode); 
} 

public void setFictionCode(String fc) 

{ 
    fictionCode = fc; 
} 

public String getFictionCode() 

{ 
    return fictionCode; 
} 


public double calculateCharge() 
{ 
    double charge = this.getPrice(); 
    if (signedByAuthor == true) 
    { 
     charge = this.getPrice()+ 20 ; 
    } 
    else 
    { 
     return charge; 
    } 

    return charge; 

} 


public String toString() 

{ 
return(super.toString() + " Fiction Code " + fictionCode); 
} 


public String printInvoice() 
{ 
    return null; 
} 

} 

BookTest。 林在这里有一个问题,循环不迭代,我认为问题在这里,但idk如何解决。

Fiction f = (Fiction) book; 

if(f.isSignedByAuthor()) 

BookTest的java

import java.io.*; 
import java.io.FileNotFoundException; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Formatter; 
    import java.util.Scanner; 

import javax.swing.JOptionPane; 

public class BookTest 
{ 

private static final Fiction Book = null; 


public static void main (String[] args) 
{ 

ArrayList <Book>list = createInstances(); 

writeFile(list); 
} 


public static ArrayList<Book> createInstances() 
{ 
ArrayList<Book> bookArray = new ArrayList<Book>(); 
String inputArray[] = new String [10]; 
int i = 0; 
Scanner input; 
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 

// Read the text file and stores into inputArray where each line is stored as String. 
try 
{ 
    input = new Scanner(new File("book.txt")); 
    input.useDelimiter("\n"); 
    while (input.hasNext()){ 
     inputArray[i]=input.next(); 
     i++; 
    } 

    // dataArray defines the two dimensional array that store all the values in the line. 
    String dataArray [] [] = new String [10] [11]; 

    for (int k =0; k<inputArray.length; k++) 
    { 
     String getLine = inputArray[k]; 
     String[] eachLine =getLine.split(" "); 
     int length = eachLine.length; 


    for (int j=0; j<length;j++) 
      { 
      dataArray [k][j]= eachLine[j]; 
     } 
    } 

    for (int l = 0; l < 10; l++) 

     { 
      if (dataArray[l][0].equals("Fiction")) 


      { 
       Publisher p = new Publisher(dataArray[l][3], dataArray[l][4]); 

       String[] dateSplit = (dataArray[l][10]).split("/"); // splits the date (eg. 01/1/2015 to array containg 01, 1, 2015 
       Date date = new Date(Integer.parseInt(dateSplit[0]), Integer.parseInt(dateSplit[1]),Integer.parseInt(dateSplit[2])); 

       bookArray.add(new Fiction(dataArray[l][1], dataArray[l][2], dataArray[l][5], 
       p, Double.parseDouble(dataArray[l][6]), dataArray[l][7], l, date)); 
      } 

      else 
      { NonFiction.CategoryCode categoryCode = NonFiction.CategoryCode.valueOf(dataArray[l][7]); 
       Publisher p = new Publisher(dataArray[l][3], dataArray[l][4]); 
       String[] dateSplit = (dataArray[l][9]).split("/"); 
       Date date = new Date(Integer.parseInt(dateSplit[0]), Integer.parseInt(dateSplit[1]),Integer.parseInt(dateSplit[2])); 
       bookArray.add(new NonFiction(dataArray[l][1], dataArray[l][2],dataArray[l][5], 
       p, Double.parseDouble(dataArray[l][6]), categoryCode, l,date)); 
      } 
     } 

} 

catch (FileNotFoundException e) 
{ 

    e.printStackTrace(); 
} 
return bookArray; 
} 


public static void writeFile(ArrayList<Book> arrayOfBook) 
{ 
Formatter output ; 

try 
{ 
    output = new Formatter("updatebooks.txt"); 

     for (Book t : arrayOfBook) 
     { 
     output.format("%s %s %s %s %s %s %s %s %s %s \n \n","Title:", t.getTitle(),"  Author:", t.getAuthor(),"  ISBN:", t.getIsbn(),"  Publisher:",t.getPublisher(),"  Price:",t.getPrice()); 
     } 
     output.close(); 
     } catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 



int count = 0;   
String message = ""; 
for (Book book : arrayOfBook) 
{ 
    if(Book instanceof Fiction) 
    { 
    Fiction f = (Fiction) book; 

    if(f.isSignedByAuthor()) 
    { 
     count++; 
    } 
    message += String.format("%s %s \n","p ", f.isSignedByAuthor()); 
    } 

} 

JOptionPane.showMessageDialog(null, "Total Signed Fiction : " + count);;  
System.out.println(count); 


} 
} 
+0

我认为你的arrayOfBook有Fiction和NonFiction对象。两者都扩展了Book类。既然你试图将NonFiction强制转换为Fiction类,那么就是错误。在扩展该类之前,您无法输入投射。 –

回答

1

请,也这样做,

以外某处for..loop,

 int count = 0; 

    .... 


    String message = ""; 
    for (Book book : arrayOfBook) 
    { 
     if(book instanceof Fiction){ 
     Fiction f = (Fiction) book; 
      if(f.isSignedByAuthor()){ 
       count++; 
      } 
     message += String.format("%s %s \n","p ", f.isSignedByAuthor()); 
     } 
    } // end of for loop 
    JOptionPane.showMessageDialog(null, "Total Signed Fiction : " + count); 

如果book参考变量是指Fiction对象然后book instanceof Fiction return true,在这种情况下,只有你必须下蹲,以便ClassCastException正确覆盖。

如果你的数组同时包含对象类型Non-fictionFiction那么这instanceof检查将有助于更好地控制ClassCastException

+0

@vishalgajeramy数组既包含小说和非小说的对象,你能指导我如何使用从isSignedByAuthor的小说中获取值。 – jojo

+0

@jojo我已经解决了我的答案。请遵照它。只要添加条件(我提到我的asnwer)到你的for循环。 –

+0

感谢您的帮助,但我得到了不同的问题,我的构造函数和数据为below.private boolean signedByAuthor; \t \t public boolean isSignedByAuthor() \t { \t \t return signedByAuthor; \t} \t公共无效setSignedByAuthor(布尔signedByAuthor) \t { \t \t如果(signedByAuthor == TRUE) \t \t { \t \t \t this.signedByAuthor = signedByAuthor; \t \t} \t \t其他 \t \t { \t \t \t回报; \t \t} 而我正在阅读下面格式的文件。 Fiction Frankenstein Shelley Prescott GA 978-0486282114 7.99 321真实8 2008年5月12日 虚构Dracula Stoker Addison CA 978-0486411095 5.99 145 false 53 2005年8月15日 谢谢 – jojo

相关问题