簡體   English   中英

我想通過 ID 號在對象的數組列表中搜索特定的付款並顯示它

[英]i want to search for a specific payment in arraylist of object by the id number and display it

在主要

Invoice r = new Invoice();
r.addpayment(new  Payment("cash", 333, 20000.0,date2, "villa"));
r.addpayment(new Payment("cash",9000, 30000.0,date2, "apartment"));
r.getPayment(333);

這里我應該使用這個方法返回所有支付信息的對象

public class Invoice
{
    ArrayList<Payment> payments = new ArrayList<Payment>();
    public Payment getPayment(int paymentId)
    {
        for (Payment temp: payments)
        {
            if (temp.getPaymentId() == paymentId)
            {
                return temp;
            }
        }
        return null;
    }
}

但它沒有返回任何東西:(

addpayment方法向系統添加新的付款:

public void addpayment(Payment payment)
{
    payments.add(payment);

    System.out.println("payment has been added ");
    for (int i = 0; i < payments.size(); i++)
    {
        System.out.println("payment Type : " + payments.get(i).getPaymentType() + " \npayment id : " + payments.get(i).getPaymentId() +
                " \npaymen price : " + payments.get(i).getPrice() + "\npayment description: " + payments.get(i).getDescription() +
                "\n payment date :" + payments.get(i).getPaymentDate() + "\n");
    }
}

通過檢查 null 打印返回的值並打印它

    import java.util.ArrayList;

    class Payment{
    private String price;
    private String paymentType;
    private String paymentDate;
    private String description;
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getPaymentDate() {
        return paymentDate;
    }
    public void setPaymentDate(String paymentDate) {
        this.paymentDate = paymentDate;
    }
    public Integer getPaymentId() {
        return paymentId;
    }
    public void setPaymentId(Integer paymentId) {
        this.paymentId = paymentId;
    }
    private Integer paymentId;

    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public String getPaymentType() {
        return paymentType;
    }
    public void setPaymentType(String paymentType) {
        this.paymentType = paymentType;
    }
    public Payment( String paymentType,String price,  Integer paymentId, String description) {
        super();
        this.price = price;
        this.paymentType = paymentType;
        this.description = description;
        this.paymentId = paymentId;
    }

        @Override
public String toString() {
    return "Payment [price=" + price + ", paymentType=" + paymentType + ", paymentDate=" + paymentDate
            + ", description=" + description + ", paymentId=" + paymentId + "]";
}


}


public class Invoice
{
    ArrayList<Payment> payments = new ArrayList<Payment>();
    public Payment getPayment(int paymentId)
    {
        for (Payment temp: payments)
        {
            if (temp.getPaymentId() == paymentId)
            {
                return temp;
            }
        }
        return null;
    }

    public void addpayment(Payment payment) {
        payments.add(payment);

       System.out.println("payment has been added ");
       for(int i=0; i<payments.size();i++) {
           System.out.println("payment Type : "+ payments.get(i).getPaymentType()+ " \npayment id : " + payments.get(i).getPaymentId() + 
                   " \npaymen price : " + payments.get(i).getPrice() + "\npayment description: " +payments.get(i).getDescription()+
                   "\n payment date :"+payments.get(i).getPaymentDate()+"\n");
       }


   }

    public static void main(String args[]) {
        Invoice r = new Invoice();
        r.addpayment(new  Payment("cash", "333", 20000, "villa"));
        r.addpayment(new Payment("cash","9000", 30000,"apartment"));
        System.out.println(r.getPayment(20000));
    }

}

問題是您沒有打印返回的Payment對象。

代替

r.getPayment(333);

System.out.println(r.getPayment(333));

確保您在Payment重寫了toString ,例如:

@override
public String toString() {
    return "Payment type: " + paymentType + ", Payment id: " + paymentId + ", Paymen price: " + price + ", Payment description: " + description + ", Payment date: " + paymentDate;
}

以獲得有意義的輸出。

檢查這個以獲取更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM