Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is me Set working as an array list in Java programming?
    primarykey
    data
    text
    <p>Hello again fellow programmers.</p> <p>so I'm creating a program that allows users to specify the type of insurance cover they want as part of an insurance policy. </p> <p>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. </p> <p>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. </p> <p>This is the code I used to pass the created insurance type objects to the insurance policy class.After they have been instantiated.</p> <pre><code>CoverType theInsuranceType = new CoverType("Fire",250); theInsurancePolicy.includeCover(theInsuranceType); </code></pre> <p>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.</p> <pre><code> import java.util.*; // Class: InsurancePolicy // Purpose: To represent a particular customer's insurance agreement public class InsurancePolicy { //ArrayList&lt;Product&gt; orderList = new ArrayList&lt;Product&gt;(); 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&lt;CoverType&gt; TheCoverType = new HashSet&lt;CoverType&gt;(); 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; } </code></pre> <p>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.</p> <pre><code> Iterator&lt;CoverType&gt; 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; </code></pre> <p>Any help will be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload