开发者

Why is me Set working as an array list in Java programming?

Hello again fellow programmers.

so I'm creating a program that allows users to specify the type of insurance cover they want as part of an insurance policy.

Since their is only 4 types of cover to choose from I want to use a set or hash set to keep track of the types of cover offered to users.

The problem is that is does not seem to get rid of duplicates it is treating it like an arraylist and when I print out the contents of the set i get duplicates which should not happen in a set.

This is the code I used to pass the created insurance type objects to the insurance policy class.After they have been instantiated.

CoverType theInsuranceType = new CoverType("Fire",250);
theInsurancePolicy.includeCover(theInsuranceType);

and this is the code I used in the insurance policy class to keep track of the type of insurance that a user has taken out.

    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 int totalPolicyCost;

        private static String name = null;

        private static int price = 0;

        private int amount = 0;

        private static int total = 0, claims = 0;


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


        public static boolean includeCover(CoverType which)
        {

            TheCoverType.add(which);
            System.out.println(which.getName()+" Has ben added to your insurance    policy");
            System.out.println(" ");
            System.out.println(" ");
            System.out.println("-----------------------");  
            return true;

        }

I have done this as part of the the insurance policy class to iterate over the set and pull out and print values of each object for the user and that is where im getting the duplicates.

    Iterator<CoverType> iter = TheCoverType.iterator();
        int cash =0;
        while (iter.hasNext())
        {
            CoverType type = iter.next();
             cash =  type.getPrice();
            amount = amount + cash;
            System.out.println("This one Cost. 开发者_开发技巧 "+ cash);
                        // int cost = TheCoverType.getPrice().next();
//          if theCover     


        }
        amount = amount+ 100;
        String message = "So all up total cost of the policy is. "+cash; 
        return amount;

Any help will be greatly appreciated.


Your objects that you want to put in a set need to implement equals and hashcode.

Even if all fields are "equal", you won't get object equality (.equals()) in java unless you override those methods.


The type held by the set I assume is "CoverType", right? If so, do you overload equals and hashCode() for this class?


Have you implemented .equals and .hashCode for your CoverType class?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜