如何在创建为集合的一部分后引用创建对象中的值

时间:2011-09-13 13:33:01

标签: java

作为uni tute [sic]的一部分,我们正在创建可由用户取出的保险单。 所有不同类型的保险都可以在一项政策下取得。大多数基本代码都已提供给我们。

我创建了一组CoverType类的对象,然后我试图访问该对象中的get方法,以添加在一个策略下取出的所有保险的成本但是它不允许我访问getter。它回复了一个错误"找不到符号方法getPrice"。

这是保险单的代码

import java.util.*;
// Class:   InsurancePolicy
// Purpose: To represent a particular customer's insurance agreement
public class InsurancePolicy {


        //ArrayList<Product> orderList = new ArrayList<Product>();
    private static double totalPolicyCost;

    private static String name = null;

    private static int price = 0;


    private static Set<CoverType> TheCoverType = new HashSet<CoverType>();


    // Each instance will have a unique policy number.
    private int policyNumber;


    private static int nextPolicy = 1;

    public InsurancePolicy()
    {

        this.policyNumber = nextPolicy;
        nextPolicy++;
    }

    // Determine this policy's number
    public int getPolicyNumber()
    {

        return policyNumber;
    }

    // Method:  getPolicyCost
    // Purpose: to report to the caller, the total cost of this policy, based on the covers
    //          that are selected
    public int getPolicyCost()
    {
        // Student must replace the following...

        Iterator<CoverType> iter = TheCoverType.iterator();

        while (iter.hasNext())
        {

            int cash = TheCoverType.getPrice();
            int total = total + cash;
//          int cost = TheCoverType.getPrice().next();
//          if theCover     


        }
        totalPolicyCost = totalPolicyCost + 100;
        return 0;
    }

    // Method:  includeCover
    // Purpose: To allow the caller to specify a type of insurance cover to be included for this policy.
    // Returns: True if the cover is now included; if it was already included or was unable to
    //          be included this time, false is returned.
    public static boolean includeCover(CoverType which)
    {


        //CoverType initialCoverType = new CoverType(name,price);
        //initialCoverType = which(); 
        // Student must replace this following:

        //TheCoverType = which;

        for (CoverType element : TheCoverType)
        {
            if (!TheCoverType.contains(which))
            {
            TheCoverType.add(which);
            return true;
            }
                else
            {
                System.out.println("The specified insurance has already been added");
                return false;
            }
        }       

            System.out.println(TheCoverType);


        return true;

    }

    // Method:  lodgeAnotherClaim
    // Purpose: To increase the counter of how many claims have been made against this policy.
    // Parameter: lodgedType - specifies the type of cover being claimed. But only the types which
    //            have been included so far, will actually be countable.
    public void lodgeAnotherClaim(CoverType lodgedType)
    {
        // Student must complete
            for (CoverType element : TheCoverType)
        {
            if (!TheCoverType.contains(lodgedType))
            {
            TheCoverType.add(lodgedType);
            }
                else
            {
                System.out.println("The specified insurance has already been added");
            }
        }       

            System.out.println(TheCoverType);


    }

    // Method:  toString
    // Purpose: Display a textual summary of this object's state
    @Override public String toString()
    {
        // Student must complete

        return "Something";
    }
}

错误在第50行说&#34;找不到符号方法getPrice()&#34;。我想要的是从CoverType类

引用此方法
    public int getPrice()
    {
        return price;
    }

2 个答案:

答案 0 :(得分:3)

变量TheCoverType的类型为Set。这意味着它是CoverType对象的集合。 getPrice()方法是在CoverType上定义的,而不是在Set上定义的。您需要在迭代器循环中将实际对象从Set中取出。像CoverType type = iter.next()这样的东西。然后,您拨打type.getPrice()而不是TheCoverType.getPrice()

答案 1 :(得分:2)

你也可以考虑做一个简单的foreach循环:

for (CoverType coverType : TheCoverType){
    int cash = coverType.getPrice();
    ...
}

我认为这比直接使用迭代器更具可读性且更不容易出错。