2011-03-28 7 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 

항목

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

답변

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

1
  1. 무엇보다도, 내가 컴파일 할 때, 내 제품 번호와 가격이 혼합 수 쪽으로. 왜?

    당신은

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

    에 전화로 새 ItemDetails 객체를 생성하고 있지만 ItemDetails에 대한 생성자

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

    예입니다, 그것은 제품의 첫 번째 숫자와 가격 마지막으로,하지 소요 다른 방향으로

  2. 두 번째로, 제품 유형 항상 null을 반환합니까?

    당신은 실제로 타입을 설정하지 않습니다. 세트와 메소드 모두 같은 일을합니다! 세터가 값을 반환한다는 것은 경고 였을 것입니다!

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

    이것은, 예를 들어

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

첫 번째 문제를 해결해 주었지만 여전히 고유 한 유형을 고수하고 있습니다.정교한? – g3n3rallyl0st

+0

@ g3n3rallyl0st 답변을 수락했음을 표시 했으므로 알아 냈을 것입니다. 위의 요한의 대답은 이것에 대해서도 설명합니다. –

1

나는 또한 모든 메시지 상자를 결합하고 싶습니다 ...

JOptionPaneTest을 볼 것입니다.

관련 문제