Pergunta

I have csv file like this:

1392249600;EUR;CHF;USD;JPY;GBP
1392163200;GBP;JPY;USD;CHF;EUR
1392076800;GBP;CHF;EUR;JPY;USD
1391990400;JPY;USD;EUR;CHF;GBP
1391904000;GBP;EUR;CHF;USD;JPY
1391731200;GBP;EUR;CHF;JPY;USD
1391644800;EUR;CHF;USD;JPY;GBP
1391558400;JPY;USD;EUR;CHF;GBP

There can be over 15 000 rows in that file. I am trying to write code that could do such thing:

1.Takes first row saves it as parent. Then takes next 3 days as that childs.

2.Counts how often and which combination off childs with that parent are inside this file.

3.It creates something like summary for that so I could read todays combination and script shows the only the most frequent child combinations for next 3 days.

I don't have mathematical thinking so I have big problems to find solution myself.

What I think first I need script that generates all posible combinations of these colums made of EUR,CHF,USD,JPY,GBP so there is posible 5*4*3*2*1 = 120 combinations. Because they cant repeat in single row.

It will be like this.

First parent will be combination from first row like this: EUR;CHF;USD;JPY;GBP

Then 3 childs would be
  GBP;JPY;USD;CHF;EUR 
  GBP;CHF;EUR;JPY;USD
  JPY;USD;EUR;CHF;GBP

It saves this combination off parent and child elements. Then again it starts from begining of the file, but moves one row below(like iteration +1). then next all childs would be

 GBP;CHF;EUR;JPY;USD
   JPY;USD;EUR;CHF;GBP
   GBP;EUR;CHF;USD;JPY

And again it saves these combinations for counting and make some frequency results. And this cycle repeats for all rows on csv file.

Is there maybe some tips I should consider how to create this type of programm ?

Any tip would be great !

Thank You Very Much! BB

Foi útil?

Solução

Can you please clarify whether first value in a row in your file is date/time? 1392249600;EUR;CHF;USD;JPY;GBP

If yes, are you expecting that there will total 4 rows with the same date/time?

Or else you just need to go sequentially and use Line-1 as parent and then Line-2, Line-3, Line-4 as child and goes on... so that Line-5 becomes parent again?

To check whether country code is equivalent or not, you can use below kind of code. I am not 100% sure about your requirement, please correct me if you think this is not what you are looking for and I will try to answer you in other way:

package com.collections;

public class CountryCodeComparison {
    public static void main(String[] args) {
        //Read every row and sequentially insert value in CountryCode object.
        //For ex. your row is: 1392163200;GBP;JPY;USD;CHF;EUR
        String s1 = "1392163200;GBP;JPY;USD;CHF;EUR";
        String [] array1 = s1.split(";");
        CountryCode  cc1 = new CountryCode(array1[1], array1[2], array1[1], array1[4], array1[5]);

        //For ex. your row is: 1392076800;GBP;CHF;EUR;JPY;USD
        String s2 = "1392076800;GBP;CHF;EUR;JPY;USD";
        String [] array2 = s2.split(";");
        CountryCode  cc2 = new CountryCode(array2[1], array2[2], array2[1], array2[4], array2[5]);

        if(cc1.equals(cc2)) {
            System.out.println("Both CountryCode objects are equal.");
        } else {
            System.out.println("Both CountryCode objects are NOT equal.");
        }
    }
}

class CountryCode {
    private String countryCode1;
    private String countryCode2;
    private String countryCode3;
    private String countryCode4;
    private String countryCode5;
    public CountryCode(String countryCode1, String countryCode2,
            String countryCode3, String countryCode4, String countryCode5) {
        this.countryCode1 = countryCode1;
        this.countryCode2 = countryCode2;
        this.countryCode3 = countryCode3;
        this.countryCode4 = countryCode4;
        this.countryCode5 = countryCode5;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((countryCode1 == null) ? 0 : countryCode1.hashCode());
        result = prime * result
                + ((countryCode2 == null) ? 0 : countryCode2.hashCode());
        result = prime * result
                + ((countryCode3 == null) ? 0 : countryCode3.hashCode());
        result = prime * result
                + ((countryCode4 == null) ? 0 : countryCode4.hashCode());
        result = prime * result
                + ((countryCode5 == null) ? 0 : countryCode5.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        CountryCode other = (CountryCode) obj;
        if (countryCode1 == null) {
            if (other.countryCode1 != null)
                return false;
        } else if (!countryCode1.equals(other.countryCode1))
            return false;
        if (countryCode2 == null) {
            if (other.countryCode2 != null)
                return false;
        } else if (!countryCode2.equals(other.countryCode2))
            return false;
        if (countryCode3 == null) {
            if (other.countryCode3 != null)
                return false;
        } else if (!countryCode3.equals(other.countryCode3))
            return false;
        if (countryCode4 == null) {
            if (other.countryCode4 != null)
                return false;
        } else if (!countryCode4.equals(other.countryCode4))
            return false;
        if (countryCode5 == null) {
            if (other.countryCode5 != null)
                return false;
        } else if (!countryCode5.equals(other.countryCode5))
            return false;
        return true;
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top