Java:在给定参数时选择对象
我是一名初级Java程序员 现在我有一个问题: 我有一个名为Java:在给定参数时选择对象,java,class,object,bluej,Java,Class,Object,Bluej,我是一名初级Java程序员 现在我有一个问题: 我有一个名为Producttype的类,其字段名为name。 我还有一个名为Main的类。现在在类Main中,我想通过询问名称从类Producttype中选择一个对象 我该怎么做 提前谢谢 Producttype的代码: import java.util.*; import javax.swing.*; public class Producttype { //velden private String naam; pri
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);
}
我想要的是方法printformOverProducType()中的“字符串信息”执行方法drukInfo()从名称等于字符串类型的对象的“Producctype”类中,您的
Main
类需要能够访问ProductType
的映射,其中键是名称string
:
public class Main {
private Map<String, ProductType> products = new ConcurrentHashMap<String, ProductType>();
public ProductType findProductTypeByName(String name) {
return this.products.get(name);
}
}
公共类主{
私有映射产品=新的ConcurrentHashMap();
public ProductType findProductTypeByName(字符串名称){
返回此.products.get(名称);
}
}
请发布Main/Producttype类的源代码。您可能需要告诉我们更多信息,因为您的问题似乎不完整,我无法回答。您是否有一个数组或集合,例如ProductType对象的ArrayList,并且您是否正在尝试选择一个名称与特定字符串匹配的数组或集合?只需阅读Java初学者书籍,或从一开始就阅读有关Java的任何教程。您似乎想将Java程序视为访问数据库。Java不是这样工作的。如果要创建对象,则创建它;如果要查找集合,则按键或索引选择它。