I am working on a project that I have to check if some(500) reports, generated from a system, are passed or failed.
I face 2 problems.
report example :
Ticket sales
Movies Full_seats Sales Empty_seats
Movie 1 Monday 100 500 20
Tuesday 120 600 0
Wednesday 80 400 40
Thursday 100 500 20
Friday 100 500 20
Movie 2
Monday 100 500 20
Tuesday 120 600 0
Wednesday 80 400 40
Thursday 100 500 20
Friday 100 500 20
and so on...
end of report.
What can I do in order to map the values from the report, and save them into java collection. Is there a more clever way that to create 500 different parsing methods?
report.grossSales & sales.getDay(18_9_2016).grossSales();
the above will mean for the automation to run:
if(report.grossSales != sales.getDay(18_9_2016).grossSales()){
System.out.print("failed");
}
Feel free to suggest other ways, more clever and flexible. Thank you
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadFile {
public static void main(String[]args) throws IOException{
FileReader in = new FileReader("C:/test.txt");
BufferedReader br = new BufferedReader(in);
String line = "";
String wholeReportText="";
while ((line = br.readLine()) != null) {
wholeReportText += line;
}
in.close();
Pattern pattern = Pattern.compile("Movies(.*?)");
Matcher matcher = pattern.matcher(wholeReportText);
// check all occurance
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Pattern daypat = Pattern.compile("Tuesday (.*?)");
Matcher daymat = daypat.matcher(wholeReportText);
// check all occurance
while (daymat.find()) {
System.out.println(matcher.group(1));
}
}
}
Your Problem needs to be broke in sub problems as in case of algorithms
Step 1) To list all the files which you need to parse (File API in java FileFilter in java NIO and Paths API in java7)
Step 2) Read and extract data from file one by one (sample code above)
Step 3) Store the extracted data in some HashMap or ArrayList
Step 4) Use collections as per your requirement
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With