2011-03-28 62 views
0

我想要一些幫助。首先,當我編譯時,我的產品編號和價格變得混亂起來。爲什麼?其次,爲什麼產品類型總是返回null?我也想合併所有的消息框,但是我所做的每一次嘗試都失敗了。如果有人能帶領我走向正確的方向,我將不勝感激。這裏是我的代碼:爲什麼我編譯時混淆了數據?

主要

package inventory4; 
import java.util.Scanner; 
import java.util.Arrays; //Needed to include data for arrays 
import javax.swing.JOptionPane; //JOptionPane import tool 

public class RunApp 
{ 
public static void main(String[] args) 
    { 

    Scanner input = new Scanner(System.in); 

    ItemDetails theItem = new ItemDetails(); 

    int number; 
    String Name = ""; 
    String Type = ""; 

    String sNumber = JOptionPane.showInputDialog(null,"How many items are to be put into inventory count?: "); 
    number = Integer.parseInt(sNumber); 

    ItemDetails[] inv = new ItemDetails[number]; 


    for (int count = 0; count < inv.length; ++count) 
    { 
    Name = JOptionPane.showInputDialog(null,"What is item " + (count + 1) + "'s name?"); 

    theItem.setName(Name); 

    Type = JOptionPane.showInputDialog(null,"Enter " + Name + "'s product type"); 

    String spNumber = JOptionPane.showInputDialog(null,"Enter " + Name + "'s product number"); 
    double pNumber = Double.parseDouble(spNumber); 
    theItem.setpNumber(pNumber); 

    String sUnits = JOptionPane.showInputDialog(null,"How many " + Name + "s are there in inventory?"); 

    double Units = Double.parseDouble(sUnits); 
    theItem.setUnits(Units); 

    String sPrice = JOptionPane.showInputDialog(null,Name + "'s cost"); 
    double Price = Double.parseDouble(sPrice); 
    theItem.setPrice(Price); 


     inv[count] = new ItemDetails(Name, Price, Units, pNumber); 

    } 




    for (int i = 0; i < inv.length; ++i) 
    { 

    JOptionPane.showMessageDialog(null, "Product Name: " + inv[i].getName()); 
    //Why can't I use this instead of having multiple boxes?: 
    //JOptionPane.showMessageDialog(null, "Product Name: \nProduct Type: \nProduct Number: \nUnits in Stock: \nPrice Per Unit: " + inv[i].getName() + inv[i].getUniqueType() + inv[i].getpNumber() + inv[i].getUnits(), + inv[i].getPrice()); 

    JOptionPane.showMessageDialog(null, "Product Type: " + inv[i].getUniqueType()); 

    JOptionPane.showMessageDialog(null, "Product Number: " + inv[i].getpNumber()); 

    JOptionPane.showMessageDialog(null, "Amount of Units in Stock: " + inv[i].getUnits()); 

    JOptionPane.showMessageDialog(null, "Price per Unit: " + inv[i].getPrice()); 

JOptionPane.showMessageDialog(null, String.format("Total cost for %s in stock: $%.2f", inv[i].getName(), inv[i].calculateTotalPrice())); 

    JOptionPane.showMessageDialog(null,String.format("Restocking fee for %s is $%.2f", inv[i].getName(), inv[i].calculateRestock())); 

    String combinedData = inv[i].toString(); 

    if(i == (inv.length -1)){ 

     String lastItem = String.format("\nTotal Cost for all items entered: $%.2f\n", Items.getCombinedCost(inv)); 

     combinedData = combinedData + lastItem; //combine total value to the end of the last object output 
     JOptionPane.showMessageDialog(null, combinedData); //Show Message 

    }else{ 
     JOptionPane.showMessageDialog(null, combinedData); //Show Message 
    } 


    } //end for 


    } //end main 


} //end class 

ITEMS

package inventory4; 


public class Items implements Comparable 
{ 
    private String Name; 
    public double pNumber, Units, Price; 
    String allInfo; 
    public Items() 
    { 
     Name = ""; 
     pNumber = 0.0; 
     Units = 0.0; 
     Price = 0.0; 
    } 

    public int compareTo(Object item) 
    { 

     Items tmp = (Items)item; 

     return this.getName().compareTo(tmp.getName()); 
    } // end compareTo method 


    public Items(String productName, double productNumber, double unitsInStock, double unitPrice) 
    { 
     Name = productName; 
     pNumber = productNumber; 
     Units = unitsInStock; 
     Price = unitPrice; 

    } 

    public String toString() 
    { 
     StringBuffer allInfo = new StringBuffer(); 


    allInfo.append(String.format("Name: %s\n pNumber: %.0f \n Units: %.0f \n Price: %.2f\n", 
    getName(),getpNumber(),getUnits(),getPrice())); 

    return allInfo.toString(); 
    } 


    //setter methods 
    public void setName(String n) 
    { 
     Name = n; 
    } 

    public void setpNumber(double no) 
    { 
     pNumber = no; 
    } 

    public void setUnits(double u) 
    { 
     Units = u; 
    } 

    public void setPrice(double p) 
    { 
     Price = p; 
    } 

    //getter methods 
    public String getName() 
    { 
     return Name; 
    } 

    public double getpNumber() 
    { 
    return pNumber; 
    } 

    public double getUnits() 
    { 
     return Units; 
    } 

    public double getPrice() 
    { 
     return Price; 
    } 

    public double calculateTotalPrice() 
    { 
     return (Units * Price); 
    } 

    public static double getCombinedCost(Items[] item) //This is used to set up the method 
    { 
     double combined = 0; //Loops through array after array is complete 

     for (int i = 0; i < item.length; ++i) 
     { 
     combined = combined + item[i].calculateTotalPrice(); //Sets up to combine all TotalPrice 
     //calculations in array 
     } //end loop 

     return combined; 
    } //end method 

} //end class 

項目詳細

package inventory4; 

public class ItemDetails extends Items 
{ 
    private String UniqueType; 

    public ItemDetails() 
    { 
     super(); 
    } 

    public ItemDetails(String productName, double productNumber, double unitsInStock, double unitPrice) 
    { 
    super(productName,productNumber,unitsInStock,unitPrice); 
    } 

public String enterUniqueType() 
    { 
     return UniqueType; 
    } 

public String setUniqueType() 
    { 
     return UniqueType; 
    } 

public String getUniqueType() 
    { 
     return UniqueType; 
    } 

public double calculateRestock() //setting up to calculate the restocking fee at 5% 
    { 
     return (Price * .05); 
    } 
} 
+0

參見[Java編程語言代碼規範(http://www.oracle.com/technetwork/java/codeconv-138413.html)。 – trashgod 2011-03-28 04:25:33

回答

1
// getter??? 
public String setUniqueType() { 
    return UniqueType; 
} 

應該是:

//setter 
public void setUniqueType(String type) { 
    UniqueType = type; 
} 

inv[count] = new ItemDetails(Name, Price, Units, pNumber); 

應該是:

inv[count] = new ItemDetails(Name, pNumber, Units,Price);//look at the order 
inv[count].setUniqueType(Type);//you have not set it. 
+0

這很有幫助。謝謝 – g3n3rallyl0st 2011-03-28 04:47:48

1
  1. 首先,當我編譯,我的產品編號和價格得到混合向上。爲什麼?

    你創建與呼叫一個新ItemDetails對象

    new ItemDetails(Name, Price, Units, pNumber); 
    

    ,但你對ItemDetails構造函數是

    ItemDetails(String productName, double productNumber, double unitsInStock, double unitPrice) 
    

    即它需要的產品數量第一和最後的價格,而不是反過來

  2. 其次,爲什麼產品類型總是返回null?

    你從來沒有真正設置你的類型,你的設置和獲取方法都做同樣的事情!該setter正在返回一個值應該是一個警告!

    public String setUniqueType() 
    { 
         return UniqueType; 
    } 
    
    public String getUniqueType() 
    { 
         return UniqueType; 
    } 
    

    這是它應該是

    public void setUniqueType(String type) 
    { 
         this.UniqueType = type; 
    } 
    
+0

謝謝你,我解決了第一個問題,但我仍然停留在獨特的類型上。闡述? – g3n3rallyl0st 2011-03-28 04:35:58

+0

@ g3n3rallyl0st既然你已經接受了答案,我猜你已經明白了。約翰在上面的答案也解釋了這一點。 – 2011-03-28 15:15:43

相關問題