作为统一的一部分,我们正在创建可以由用户购买的保险单。
所有不同类型的保险都可以根据一项保单进行投保。大多数基本代码已提供给我们。

我已经创建了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行,显示“找不到符号方法getPrice()”。我想要的是为此从CoverType类引用此方法

    public int getPrice()
    {
        return price;
    }

最佳答案

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

09-10 13:53