2011-12-04 54 views
0

我是一個初級Java程序員的Java:選擇對象時指定參數

現在我有這樣的問題: 我有一個名爲Producttype與命名name領域類。 我也有一個類叫Main。現在在類Main中,我想通過詢問名稱來從類Producttype中選擇一個對象。

我該怎麼做?

在此先感謝Producttype的

代碼:

import java.util.*; 
import javax.swing.*; 

public class Producttype 
{ 
    //velden 
    private String naam; 
    private String beschrijving; 
    private double aankoopprijs; 
    private double verkoopprijs; 
    private int aantalGekocht; 
    private int aantalVerkocht; 

    //constructor 
    /** 
    * Constructor 
    */ 
    public Producttype(String naam, String beschrijving, double aankoopprijs, double verkoopprijs){ 
     this.naam = naam; 
     this.beschrijving = beschrijving; 
     this.aankoopprijs = aankoopprijs; 
     this.verkoopprijs = verkoopprijs; 
     aantalGekocht = 0; 
     aantalVerkocht = 0; 
    } 
    public void drukInfo(){ 
     System.out.println("-----------------------"); 
     System.out.println("Naam van het product: " + naam); 
     System.out.println("Beschrijving van het product: " + beschrijving); 
     System.out.println("Aankoopprijs: " + aankoopprijs + " euro"); 
     System.out.println("Verkoopprijs: " + verkoopprijs + " euro"); 
     System.out.println("Aantal gekocht: " + aantalGekocht + " stuks"); 
     System.out.println("Aantal verkocht: " + aantalVerkocht + " stuks"); 
     System.out.println(""); 
     System.out.println("Aantal stuks in stock: " + berekenAantalInStock()); 
     System.out.println(""); 
     System.out.println("Omzet momenteel: " + berekenOmzet() + " euro"); 
     System.out.println("Winst momenteel: " + berekenWinst() + " euro"); 
    } 

在我的 '主',我得到這個:

private void printInfoOverProducttype() 
    { 
     String type = JOptionPane.showInputDialog("Give the name of the producctype you want info of."); 

     String info = ?????????????????????????? 

     System.out.println(info); 
    } 

我想的是, 「字符串信息」 中的方法printInforOverProducctype()執行名稱等於字符串類型的對象中類「Producctype」的方法drukInfo()

+1

請發佈源代碼。 – dbf

+2

您可能需要告訴我們更多的問題,因爲您的問題對我來說似乎不完整且無法解決。你有一個數組或集合,例如ProductType對象的ArrayList,並且你是否試圖選擇一個名稱與特定字符串匹配的名稱? –

+0

只需從頭閱讀Java初學者的書籍或任何Java教程。 – talnicolas

回答

2

Main類需要能夠訪問ProductTypeMap其中關鍵是名稱String:主/ Producttype類

public class Main { 
    private Map<String, ProductType> products = new ConcurrentHashMap<String, ProductType>(); 

    public ProductType findProductTypeByName(String name) { 
     return this.products.get(name); 
    } 
}