Refactoring to make one statement

The following piece of code here will take a text file and divide it into two groups: Apollo and Sabre, then it will tell me how many times "processed" appears and how many times savings were found, as well as give me a percentage. In an effort to delete the amount of code you all have to read, I did not include the code for sabre, since they are the samething. I am new to refactoring, but I know that the overall goal of the process is to eliminate lines of code but still make the program work. However whenever I try to make this smaller, it doesn't seem to work anymore. Could someone please help me with this?

int? apolloStartIndex = null; int? apolloEndIndex = null; int? sabreStartIndex = null; int? sabreEndIndex = null; apolloStartIndex = FindIndex(lines, "BEGIN PROCESSING APOLLO PNRS"); apolloEndIndex = FindIndex(lines, "END PROCESSING APOLLO PNRS"); sabreStartIndex = FindIndex(lines, "BEGIN PROCESSING SABRE PNRS"); sabreEndIndex = FindIndex(lines, "END PROCESSING SABRE PNRS"); ApolloandSabre(lines, apolloStartIndex, apolloEndIndex, sabreStartIndex, sabreEndIndex); if (apolloStartIndex.HasValue && apolloEndIndex.HasValue) { List<string> groupApollo = lines.Skip(apolloStartIndex.Value).Take(apolloEndIndex.Value - apolloStartIndex.Value).ToList(); if (groupApollo.Any(item => item.Contains("Savings found:"))) { Console.WriteLine("\n\nApollo\n"); var groupApolloPNRSThatWereProcessed = groupApollo.Count(line => line.Contains("Processed")); Console.WriteLine("There were " + groupApolloPNRSThatWereProcessed + " PNRS that were processed in apollo\n"); //code to find number of savings int groupApolloPNRSWhereSavingsWereFound = groupApollo.Count(line => line.Contains("Savings found:")); Console.WriteLine("In Apollo, there were " + groupApolloPNRSWhereSavingsWereFound + " PNRS that had savings\n"); //code to find percentage for apollo decimal groupApolloPercentageOfPNRSWithSavings = ((decimal)groupApolloPNRSWhereSavingsWereFound / groupApolloPNRSThatWereProcessed * 100); decimal result1 = Math.Round(groupApolloPercentageOfPNRSWithSavings, 3); Console.WriteLine("The percentage of PNRS in Apollo that had savings were " + result1 + "%\n"); Console.WriteLine("The following are all the lines that contain savings in Apollo:"); GroupPercentageForApollo(groupApollo); } } else { Console.WriteLine("Apollo data not found in file."); } } private static void ApolloandSabre(string[] lines, int? apolloStartIndex, int? apolloEndIndex, int? sabreStartIndex, int? sabreEndIndex) { GroupApollo(lines, apolloStartIndex, apolloEndIndex); GroupSabre(lines, sabreStartIndex, sabreEndIndex); } private static int? FindIndex(string[] lines, string textToFind) { int? index = null; int indexCounter = 0; foreach (string line in lines) { if (line.Contains(textToFind)) { index = indexCounter; break; } indexCounter += 1; } return index; }

Category:c# Views:1 Time:2019-03-13
Tags: refactoring

Related post

  • How to use designer view in Visual Studio to design more than one state of the form? 2010-07-02

    Is this a stupid question? I am trying to navigate the mystical world of C# Visual Studio. I want to be able to replace a panel at some point in run time, but the designer view only has a static view of a single state of the form. For instance there

  • The need of at least one state of the art project 2010-01-21

    I want to talk to one of my current employers about the need of having, at least one state of the art project, keeping in mind that this company have some very bad projects, bad when you think about the archtecture, bad in usability, bad when you thi

  • Using .NET CodeDOM to declare and initialize a field in one statement 2008-08-27

    I want to use CodeDOM to both declare and initialize my static field in one statement. How can I do this? // for example public static int MyField = 5; I can seem to figure out how to declare a static field, and I can set its value later, but I can't

  • How to update two tables in one statement? 2009-12-09

    UPDATE table1, tmpList SET table1.ts = tmpList.ts_value WHERE table1.id = tmpList.id UPDATE table2, tmpList SET table2.ts = tmpList.ts_value WHERE table2.id = tmpList.id I'm using MySQL --------------Solutions------------- Assuming every id appears i

  • How to update two tables in one statement in SQL Server 2005? 2010-01-11

    I want to update two tables in one go. How do i do that in SQL Server 2005? UPDATE Table1, Table2 SET Table1.LastName = 'DR. XXXXXX' ,Table2.WAprrs = 'start,stop' FROM Table1 T1, Table2 T2 WHERE T1.id = T2.id and T1.id = '010008' --------------Soluti

  • jQuery selector performance - One statement vs two 2010-02-06

    Is there any performance difference between using two separate calls to hide two elements, or would it be more/less efficient to use only 1 statement? Is there any practical difference? $('#container1').hide(); $('#container2').hide(); vs: $('#contai

  • Minimal number of steps needed to turn all binary bits to one state 2010-02-25

    There is an array of M binary numbers and each of them is in state '0' or '1'. You can perform several steps in changing the state of the numbers and in each step you are allowed to change the state of exactly N sequential numbers. Given the numbers

  • How do I make one nsi section depend on another? 2010-10-30

    In NSI, how can I make one section depend on another? Our installer has four visible sections (components): * Client * Database * Server * Interface All components depend on Client, so it is mandatory. In addition, Server depends on Database and will

  • mysql - do a top5 of two (not related) tables in one statement? (with the use of "order by") 2010-11-21

    I want to do a top5 of two tables that are not related with each other. And this all should be in one statement. I'm wondering if this possible cause I'm trying it now for hours. Google didn't help me after all... My Code: SELECT a.id AS "link_id", a

  • Joining 3 fast udfs to make one slow query 2011-04-06

    I'm trying to make a select statement that joins results from three in-line table functions (SQL Server 2005). The first two functions join as follows: SELECT Client_no ,Portfolio_no ,Sum(Nominal * Price) AS Value FROM Dbo.Udf_bulkportfolioholdings (

  • PDO bindParam into one statement? 2011-04-16

    Is there a way I can put these bindParam statements into one statement? $q = $dbc -> prepare("INSERT INTO accounts (username, email, password) VALUES (:username, :email, :password)"); $q -> bindParam(':username', $_POST['username']); $q -> b

  • One Statement object, multiple executeUpdates 2011-06-15

    Is recommended to create just one Statement object and execute multiple executeUpdate() on it? // Vantages/drawbacks of this solution? conn.setAutocommit(false); Statement stmt = conn.createStatement(); stmt.executeUpdate('[ANY_INSERT_SQL]'); stmt.ex

  • HTML/Javascript/jQuery page has a feature that works in one state but not another state 2011-11-10

    I have a strange thing happening with a part of a page working at one time, and not working in a different state of the page. Take a look at this page: http://www.problemio.com/problems/problem.php?problem_id=216 If you click on "Mobile App That Help

  • I never made a restore cd, Can i make one now that my computer is a few years old? 2012-06-30

    I think my computer may have been accessed by a remote program and i want to wipe the machine clean to the origional state. I never made a restore disk and want to know if i can now make one seeing how my machine is a few years old, or will it make t

  • Can you access the auto increment value in MySQL within one statement? 2009-01-22

    I have a MySQL database which contains a table of users. The primary key of the table is 'userid', which is set to be an auto increment field. What I'd like to do is when I insert a new user into the table is to use the same value that the auto incre

  • C# Make one picture using various pictures 2009-02-24

    Imagine that I have a matrix of 2x2 or 3x3 pictures and I want to make one big picture using these 4 or 9 pictures. I want to show this picture on a pictureBox. I'm developing a Windows Mobile App. How can I do this? Edit: Moved comments to question

  • Handling same event in more than one states in Workflow Foundation 2009-05-16

    I'm using StateMachine Workflow. There are scenarios where same event can be fired in more than one states in workflow. I want to know if i can handle same event in more than one states? --------------Solutions------------- You can listen for the sam

  • What can I do if two methods call each other and I don't want to make one of them public in the header file? 2009-05-19

    I have two methods -a and -b. -a calls sometimes -b, and -b sometimes calls -a. Both methods are intended to be private, and not called from outside. But I had to make one of them public in the .h file, because otherwise the compiler would go crazy a

  • SQL: Insert multiple sets of values in one statement? 2009-05-30

    Is it possible to insert multiple sets of values to a SQLite table in one statement? I was trying: INSERT INTO the_table VALUES (1,2,'hi'),(2,0,'foo'); with the different ()s representing different insert sets, but I get an error. --------------Solut

  • Which of the following allows more than one statement to appear on a single text Line? 2009-06-11

    Which of the following allows more than one statement to appear on a single text Line? a) Colon ( : ) b) Semicolon ( ; ) c) Space + Underscore ( _ ) d) Underscore + space ( _ ) --------------Solutions------------- The Visual Basic .NET Language Speci

  • How to make one widget from multiple widgets in designer 2009-09-01

    I have multiple edit boxes and buttons on my dialog form in qt designer. I want to make one widget from my edit boxes, something like this : <widget class="QWidget" native="1" name="widget_2" > <item> <widget class="QLineEdit" name="li

  • mysql muliple queries in one statement 2009-10-29

    I've looked around on stackoverflow for a similar question, but haven't found exactly what I was looking for, so here goes. In myPHPAdmin you can have mulitple queries in one statement and it executes it for you, eg:' UPDATE `test` WHERE `test2` = 4;

  • Multiple bindings expressions in one statement 2010-04-25

    Does WPF support multiple binding expressions in one statement? Something along the lines of the following: <TextBlock Text="{Binding Path=OrderID} shipped on {Binding Path=OrderDate}"/> I'm guessing that it does but I think I just don't have t

  • Can we make one of the Existing PVOBs a ADMIN VOB? 2010-05-20

    Our ClearCase structure, as it exists now, has several PVOBs. But they do not share a ADMIN VOB. Is there a way to make one of them an Admin VOB, so that all the PVOBs can share components? Any help is appreciated. --------------Solutions------------

  • Can we make a state diagram from a sequence diagram? 2010-05-27

    Can we make a state diagram from a sequence diagram? --------------Solutions------------- Can you translate sequence of interactions using messages with changes of state? Hmm, it is like trying to translate apples to pears...

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.203 (s). 11 q(s)