需要有关JAVA中重复记录的帮助

时间:2016-07-28 05:20:40

标签: java class arraylist

Here, I attached image file with my output and expected output. I have a problem for duplicate records.

在这里,我用输出编写了我的代码。我不知道如何使用重复记录。

class Price{
    int productID,totalSold=0,totalPrice=0;
    double price;
    public Price(int productID,double price) {
        this.price= price;
        this.productID=productID;
    }   
}
class Slips{
    int personID, productID, numberOfSold,product1Sold,product2Sold,product3Sold,product4Sold,performance;
    public Slips(int personID,int productID,int numberOfSold)
    {
        this.personID=personID;
        this.productID=productID;
        this.numberOfSold=numberOfSold;
    }    
}

class PriceComparator implements Comparator<Price>{
    public int compare(Price p1,Price p2)
    {
        return Integer.compare(p2.totalPrice, p1.totalPrice);
    }
}

public class PTestDec2009 {

    static ArrayList<Price> ALPrice=new ArrayList<>();
    static ArrayList<Slips> ALSlips=new ArrayList<>();    

    public static void main(String[] args) throws FileNotFoundException, IOException {
        getPriceInfo("C:\\Users\\Mayank Patel\\Documents\\NetBeansProjects\\PTestDec2009\\src\\ptestdec2009\\price.txt");
        getSlipsInfo("C:\\Users\\Mayank Patel\\Documents\\NetBeansProjects\\PTestDec2009\\src\\ptestdec2009\\slips.txt");
        updateSalability();        
        updateSalesPersonPerformance();
    }

    private static void getPriceInfo(String fileName) throws FileNotFoundException, IOException {
        String line=null;
        BufferedReader bufferReader=new BufferedReader(new FileReader(fileName));
        while((line=bufferReader.readLine())!=null)
        {
            StringTokenizer tokenizer=new StringTokenizer(line);
            while(tokenizer.hasMoreTokens())
            {
                ALPrice.add(new Price(Integer.parseInt(tokenizer.nextToken()),Double.parseDouble(tokenizer.nextToken())));
            }
        }       
    }

    private static void getSlipsInfo(String fileName) throws FileNotFoundException, IOException {
        String line=null;
        BufferedReader bufferReader= new BufferedReader(new FileReader(fileName));
        while((line=bufferReader.readLine())!=null)
        {
            StringTokenizer tokenizer=new StringTokenizer(line);
            while(tokenizer.hasMoreTokens())
            {
                ALSlips.add(new Slips(Integer.parseInt(tokenizer.nextToken()),Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken())));                
                //updateSalesPersonPerformance(Integer.parseInt(tokenizer.nextToken()),Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken()));                
            }      
        }
    }

    private static void updateSalesPersonPerformance()
    {        
        for(int plooper=0;plooper<ALPrice.size();plooper++)
        {
            for(int slooper=0;slooper<ALSlips.size();slooper++)
            {
                if(ALPrice.get(plooper).productID == ALSlips.get(slooper).productID)
                { 
                   switch (ALSlips.get(slooper).productID) {
                        case 1:                         
                            ALSlips.get(slooper).product1Sold += ALSlips.get(slooper).numberOfSold;
                            break;
                        case 2:
                            ALSlips.get(slooper).product2Sold += ALSlips.get(slooper).numberOfSold;
                            break;
                        case 3:
                            ALSlips.get(slooper).product3Sold += ALSlips.get(slooper).numberOfSold;
                            break;
                        case 4:
                            ALSlips.get(slooper).product4Sold += ALSlips.get(slooper).numberOfSold;
                            break;
                        default:                            
                            break;
                    }
                    ALSlips.get(slooper).performance += ALPrice.get(plooper).price* ALSlips.get(slooper).numberOfSold;
                }
            }
        }


        System.out.println("\nPERSONID"+"\t"+"PRODUCT1"+"\t"+"PRODUCT2"+"\t"+"PRODUCT3"+"\t"+"PRODUCT4"+"\t"+"PERFORMANCE");
        for(int slooper=0;slooper<ALSlips.size();slooper++)
        {           
           System.out.println(ALSlips.get(slooper).personID+"\t\t"+ALSlips.get(slooper).product1Sold+"\t\t"+ALSlips.get(slooper).product2Sold+"\t\t"+ALSlips.get(slooper).product3Sold+"\t\t"+ALSlips.get(slooper).product4Sold+"\t\t"+ALSlips.get(slooper).performance);
        }        
    }

    private static void updateSalability() {
     for(int plooper=0;plooper<ALPrice.size();plooper++)
        {
            for(int slooper=0;slooper<ALSlips.size();slooper++)
            {
                if(ALPrice.get(plooper).productID == ALSlips.get(slooper).productID)
                {
                    ALPrice.get(plooper).totalSold += ALSlips.get(slooper).numberOfSold;
                    ALPrice.get(plooper).totalPrice += ALPrice.get(plooper).price * ALSlips.get(slooper).numberOfSold;                      
                }               
            }
        }
     Collections.sort(ALPrice,new PriceComparator());
     System.out.println("PRODUCTID"+"\t"+"TotalSold"+"\t"+"TotalPrice");
     for(int plooper=0;plooper<ALPrice.size();plooper++)
     {
        System.out.println(ALPrice.get(plooper).productID+"\t\t"+ALPrice.get(plooper).totalSold+"\t\t"+ALPrice.get(plooper).totalPrice);
     }     
    }
}

所以请帮助我获得预期的输出。我附加了我的输出和预期输出的图像文件。

提前致谢。

3 个答案:

答案 0 :(得分:0)

使用HashMapHashSet

而不是使用ArrayList

此Map的关键是字符串PRODUCTID

目前你是add到一个ArrayList,但是如果你是put到你的HashMap,你可以先尝试get。如果存在,您可以更新其中的值。

一个例子就像是

HashMap myMap <String, Price val> = new HashMap <> ();

Price newPrice = ....; // like your existing code
String prodid = newPrice.getProductId ();
Price oldVal = myMap.get (prodid);
if (oldVal != null) {
  // update newPrice with sum of val
}
myMap.put (prodid, newPrice);  // will add or replace

答案 1 :(得分:0)

如果要消除重复项,可以使用set或hashset而不是arraylist,但如果将对象填充到集合中,则需要重写equals方法,以便hashset基于某些属性而不是其地址将对象视为重复例如,覆盖等于你的问题可以是

@Override
public String eqauls(Slips s)
{
    if(this.personId==s.personId)
          return true;
    else
         return false;
}

这应该放在你的单据课中。 我并不完全清楚你的要求,但这是你可以消除重复的方法

答案 2 :(得分:0)

只需使用:

public static void removeDuplicates(){
    ArrayList<Slips> newRay = new ArrayList<Slips>();
    for(Slips s : ALSlips){
        boolean hasAlready = false;
        for(Slips d: newRay){
            if(d.personID == s.personID){
                d.product1Sold += s.product1Sold;
                d.product2Sold += s.product2Sold;
                d.product3Sold += s.product3Sold;
                d.product4Sold += s.product4Sold;
                d.numberOfSold += s.numberOfSold;
                d.performance += s.performance;
                hasAlready = true;
            }
        }
        if(!hasAlready){
            newRay.add(s);
        }
    }
    ALSlips = newRay;
}

我求求你,请重写,或评论,或者其他!!除非你为你的工作做这件事。然后离开它是怎么回事。他们无法解雇你。