2016-03-16 120 views
0

我有一些問題的代碼,我從書中拉出來,不應該說非靜態變量或方法需要是靜態的。該變量的錯誤沒有我改變任何代碼。在我添加代碼之後,該方法中的錯誤出現了,但現在,即使我將代碼拿出來,它仍然會給我帶來錯誤。貨幣語句格式中,getFormattedPrice方法中的Product.java中存在可變錯誤。當我調用writeProducts時,方法錯誤在主要方法中。方法和變量有非靜態需要是靜態錯誤

public class Product 
{ 
    private String code; 
    private String description; 
    private double price; 

    public Product(String code, String description, double price) 
    { 
     this.code = code; 
     this.description = description; 
     this.price = price ; 
    } 

    public Product() 
    { 
     this("", "", 0); 
    } 

    public void setCode(String code) 
    { 
     this.code = code; 
    } 

    public String getCode(){ 
     return code; 
    } 

    public void setDescription(String description) 
    { 
     this.description = description; 
    } 

    public String getDescription() 
    { 
     return description; 
    } 

    public void setPrice(double price) 
    { 
     this.price = price; 
    } 

    public double getPrice() 
    { 
     return price; 
    } 

    public static String getFormattedPrice() 
    { 
     NumberFormat currency = NumberFormat.getCurrencyInstance(); 
     return currency.format(price); 
    } 

    public String getFormattedPrice(double price) 
    { 
     NumberFormat currency = NumberFormat.getCurrencyInstance(); 
     return currency.format(price); 
    } 

    public boolean equals(Object object) 
    { 
     if (object instanceof Product) 
     { 
      Product product2 = (Product) object; 
      if 
      (
       code.equals(product2.getCode()) && 
       description.equals(product2.getDescription()) && 
       price == product2.getPrice() 
      ) 
       return true; 
     } 
     return false; 
    } 

    public String toString() 
    { 
     return "Code:  " + code + "\n" + 
       "Description: " + description + "\n" + 
       "Price:  " + this.getFormattedPrice() + "\n"; 
    } 
} 

主要方法

public class XMLTesterApp 
{ 
    private static String productsFilename = "products.xml"; 

    public static void main(String[] args) 
    { 
     System.out.println("Products list:"); 
     ArrayList<Product> products = readProducts(); 
     printProducts(products); 

     for(Product p2 : products){ 
      System.out.println(p2.getPrice()); 
     } 
     Product p1 = new Product("test", "XML Tester", 77.77); 
     products.add(p1); 
     writeProducts(products); 
     System.out.println("XML Tester has been added to the XML document.\n"); 


     System.out.println("Products list:");   
     products = readProducts(); 
     printProducts(products); 


     products.remove(2); 
     writeProducts(products); 
     System.out.println("XML Tester has been deleted from the XML document.\n"); 


     System.out.println("Products list:"); 
     products = readProducts(); 
     printProducts(products); 

    } 

    private static ArrayList<Product> readProducts() 
    { 
     ArrayList<Product> products = new ArrayList<>(); 
     Product p = null; 
     XMLInputFactory inputFactory = XMLInputFactory.newFactory(); 
     try{ 
      FileReader fileReader = new 
       FileReader("C:\\Users\\AndrewSpiteri\\Documents\\Classes\\Baker\\CS 242\\java\\netbeans\\ex_starts\\ch19_ex1_XMLTester\\products.xml"); 
      XMLStreamReader reader = inputFactory.createXMLStreamReader(fileReader); 

      while(reader.hasNext()){ 
       int eventType = reader.getEventType(); 
       switch(eventType){ 
        case XMLStreamConstants.START_ELEMENT: 
         String elementName = reader.getLocalName(); 
         if(elementName.equals("Product")){ 
          p = new Product(); 
          String code = reader.getAttributeValue(0); 
          p.setCode(code); 
         } 
         if(elementName.equals("Description")){ 
          String description = reader.getElementText(); 
          p.setDescription(description); 
         } 
         if(elementName.equals("Price")){ 
          String priceString = reader.getElementText(); 
          double price = Double.parseDouble(priceString); 
          p.setPrice(price); 
         } 
         break; 
        case XMLStreamConstants.END_ELEMENT: 
         elementName = reader.getLocalName(); 
         if(elementName.equals("Product")) 
          products.add(p); 
         break; 
        default: 
         break; 
       } 
       reader.next();     
      } 
     } 
     catch(IOException | XMLStreamException e){ 
      System.out.println(e); 
     } 
     // add code that reads the XML document from the products.xml file 

     return products; 
    } 

    private void writeProducts(ArrayList<Product> products) 
    { 
     XMLOutputFactory outputFactory = XMLOutputFactory.newFactory(); 
     try{   
      FileWriter fileWriter = new 
       FileWriter("C:\\Users\\AndrewSpiteri\\Documents\\Classes\\Baker\\CS 242\\java\\netbeans\\ex_starts\\ch19_ex1_XMLTester\\products.xml"); 
      XMLStreamWriter writer = outputFactory.createXMLStreamWriter(fileWriter); 
      writer.writeStartDocument("1.0"); 
      writer.writeStartElement("Products"); 
      for(Product product : products){ 
       writer.writeStartElement("Product"); 
       writer.writeAttribute("Code", product.getCode()); 
       writer.writeStartElement("Description"); 
       writer.writeCharacters(product.getDescription()); 
       writer.writeEndElement(); 
       writer.writeStartElement("Price"); 
       double price = product.getPrice(); 
       writer.writeCharacters(Double.toString(price)); 
       writer.writeEndElement(); 
       writer.writeEndElement(); 
      } 
      writer.writeEndElement(); 
      writer.flush(); 
      writer.close(); 
     } 
     catch(IOException | XMLStreamException e){ 
      System.out.println(e); 
     } 
    } 

    private static void printProducts(ArrayList<Product> products) 
    { 
     for (Product p : products) 
     { 
      printProduct(p); 
     } 
     System.out.println(); 
    } 

    private static void printProduct(Product p) 
    { 
     String productString = 
      StringUtils.padWithSpaces(p.getCode(), 8) + 
      StringUtils.padWithSpaces(p.getDescription(), 44) + 
      p.getFormattedPrice(); 

     System.out.println(productString); 
    } 
} 
+0

如果您從靜態環境調用它,例如主要的方法,它將需要是靜態的,除非你的範圍內有一個類的實例。我不確定你想要解決這個問題。你想說這本書是錯的嗎? –

+0

'writeProducts'不是靜態的。錯誤很明顯。 –

回答

2

從該方法中刪除static

public static String getFormattedPrice() 
{ 
    NumberFormat currency = NumberFormat.getCurrencyInstance(); 
    return currency.format(price); 
} 

你得到,因爲price錯誤在一個實例變量,而不是一個靜態類變量。

並將static添加到private void writeProducts以便從另一個靜態方法調用writeProducts