-2

I have a list that lists transactions of a bank account, the strings are formatted as:

("Withdrawn" + amount)
("Deposited" + amount)

they look like

Withdrawn 200
Deposited 200 etc..

In the list, I'm meant to created a method that finds the suspicious activity which is defined as withdrawing between 100 and 150, so I use .Contain("withdrawn") to find each element with the word withdrawn in but then how do I find which have the amount between 100 to 150?

They're string types so how do I do something like If ( 100 > amount > 150) when they're string types?

Do I try to trim the string and parse/.convertall the digit string part to Int?

Any help?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Give us some example input and what you have tried so far. – DavidG Apr 17 '17 at 21:22
  • I suggest you use a `List>` it would be much simpler to handle and to retrieve information. – I.B Apr 17 '17 at 21:27
  • Could you provide a code example? And try to clarify your problem, please. – MetaColon Apr 17 '17 at 21:42
  • Sounds like a homework question, which is fine but should be specified. It also sounds like you're on the right track with your "Do I..." question - try it out, and if you get stuck, update this question with the details of what you have tried. – Wonko the Sane Apr 20 '17 at 13:25

2 Answers2

0

If the string is Withdrawn 200 then use Replace() method and then check like

string str = "Withdrawn 200";
int val = str.Contains("Withdrawn")
           ? Convert.ToInt32(str.Replace("Withdrawn","").Trim())
           : 0;

bool result = val >= 100 && val <= 150;
Rahul
  • 76,197
  • 13
  • 71
  • 125
0

I assume that there is a model called Transaction with ID, and Details properties. Then, I have applied the following query to get the required list.

static void Main ( string [ ] args )
{
    List<Transaction> accounts = new List<Transaction>
    {
        new Transaction { ID = 1, Details =  "Withdrawn 100"} ,
        new Transaction { ID = 2, Details =  "Withdrawn 200"} ,
        new Transaction { ID = 3, Details =  "Deposited 100"} ,
    };

    var query = accounts.
            Where ( x => x.Details.Contains ( "Withdrawn" ) && Convert.ToDecimal ( x.Details.Replace ( "Withdrawn" , "" ).Trim () ) > 100 ).
            ToList ();

    ReadKey ();
}

public class Transaction
{
    public int ID { get; set; }
    public string Details { get; set; }
}
Reda Ramadan
  • 31
  • 1
  • 3
  • You make a model with a Transaction ID but you don't split up the actin and the ammount? – Scott Chamberlain Apr 20 '17 at 20:51
  • I have defined this model just to simulate the case in question, I did not want to split up the **Details** string into action and amount. I just provide the **Transaction ID** as an identifier for the transaction record, and only query on the **Details** of the transaction to return the required results. – Reda Ramadan Apr 22 '17 at 16:55