Java:给定参数时选择对象

发布于 2024-12-19 23:38:19 字数 2142 浏览 0 评论 0原文

我是一名初级 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");
    }

在我的“Main”中,我得到了这个:

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

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

        System.out.println(info);
    }

我想要的是方法 printInforOverProducctype() 中的“String info”从名称所在的对象的类“Producctype”中执行方法 drukInfo()等于字符串类型

I'm a beginning Java-programmer

Now I have this problem:
I have a class named Producttype with a field named name.
I also have a class called Main. Now in the class Main I want to select an object from the class Producttype by asking the name.

How can I do this?

Thanks in advance

code of 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");
    }

In my 'Main' I got this:

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

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

        System.out.println(info);
    }

What I want is that "String info" in the method printInforOverProducctype() is executes the method drukInfo() from the class 'Producctype' from the object where the name equals string type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花心好男孩 2024-12-26 23:38:19

您的 Main 类需要能够访问 ProductTypeMap,其中键是名称 String

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

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

Your Main class needs to be able to access a Map of ProductType where the key is the name String:

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

    public ProductType findProductTypeByName(String name) {
        return this.products.get(name);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文