|
| 1 | +// <InputData> |
| 2 | +string bankRecords = """ |
| 3 | + DEPOSIT, 10000, Initial balance |
| 4 | + DEPOSIT, 500, regular deposit |
| 5 | + WITHDRAWAL, 1000, rent |
| 6 | + DEPOSIT, 2000, freelance payment |
| 7 | + WITHDRAWAL, 300, groceries |
| 8 | + DEPOSIT, 700, gift from friend |
| 9 | + WITHDRAWAL, 150, utility bill |
| 10 | + DEPOSIT, 1200, tax refund |
| 11 | + WITHDRAWAL, 500, car maintenance |
| 12 | + DEPOSIT, 400, cashback reward |
| 13 | + WITHDRAWAL, 250, dining out |
| 14 | + DEPOSIT, 3000, bonus payment |
| 15 | + WITHDRAWAL, 800, loan repayment |
| 16 | + DEPOSIT, 600, stock dividends |
| 17 | + WITHDRAWAL, 100, subscription fee |
| 18 | + DEPOSIT, 1500, side hustle income |
| 19 | + WITHDRAWAL, 200, fuel expenses |
| 20 | + DEPOSIT, 900, refund from store |
| 21 | + WITHDRAWAL, 350, shopping |
| 22 | + DEPOSIT, 2500, project milestone payment |
| 23 | + WITHDRAWAL, 400, entertainment |
| 24 | + """; |
| 25 | +// </InputData> |
| 26 | + |
| 27 | +await IsTextValueExample(bankRecords); |
| 28 | +Console.WriteLine(); |
| 29 | +await IsEnumValueExample(bankRecords); |
| 30 | +Console.WriteLine(); |
| 31 | +await SwitchEnumValueExample(bankRecords); |
| 32 | +Console.WriteLine(); |
| 33 | +await ExampleProgram.Main(); |
| 34 | +Console.WriteLine(); |
| 35 | + |
| 36 | +static async Task IsTextValueExample(string inputText1) |
| 37 | +{ |
| 38 | + // <IsOnTextValue> |
| 39 | + double currentBalance = 0.0; |
| 40 | + var reader = new StringReader(inputText1); |
| 41 | + |
| 42 | + string? line; |
| 43 | + while ((line = await reader.ReadLineAsync()) != null) |
| 44 | + { |
| 45 | + if (string.IsNullOrWhiteSpace(line)) continue; |
| 46 | + // Split the line based on comma delimiter and trim each part |
| 47 | + string[] parts = line.Split(','); |
| 48 | + |
| 49 | + string? transactionType = parts[0]?.Trim(); |
| 50 | + if (double.TryParse(parts[1].Trim(), out double amount)) |
| 51 | + { |
| 52 | + // Update the balance based on transaction type |
| 53 | + if (transactionType?.ToUpper() is "DEPOSIT") |
| 54 | + currentBalance += amount; |
| 55 | + else if (transactionType?.ToUpper() is "WITHDRAWAL") |
| 56 | + currentBalance -= amount; |
| 57 | + |
| 58 | + Console.WriteLine($"{line.Trim()} => Parsed Amount: {amount}, New Balance: {currentBalance}"); |
| 59 | + } |
| 60 | + } |
| 61 | + // </IsOnTextValue> |
| 62 | +} |
| 63 | + |
| 64 | +static async Task IsEnumValueExample(string inputText1) |
| 65 | +{ |
| 66 | + // <IsEnumValue> |
| 67 | + double currentBalance = 0.0; |
| 68 | + |
| 69 | + await foreach(var transaction in TransactionRecords(inputText1)) |
| 70 | + { |
| 71 | + if (transaction.type == TransactionType.Deposit) |
| 72 | + currentBalance += transaction.amount; |
| 73 | + else if (transaction.type == TransactionType.Withdrawal) |
| 74 | + currentBalance -= transaction.amount; |
| 75 | + Console.WriteLine($"{transaction.type} => Parsed Amount: {transaction.amount}, New Balance: {currentBalance}"); |
| 76 | + } |
| 77 | + |
| 78 | + static async IAsyncEnumerable<(TransactionType type, double amount)> TransactionRecords(string inputText) |
| 79 | + { |
| 80 | + var reader = new StringReader(inputText); |
| 81 | + string? line; |
| 82 | + while ((line = await reader.ReadLineAsync()) != null) |
| 83 | + { |
| 84 | + string[] parts = line.Split(','); |
| 85 | + |
| 86 | + string? transactionType = parts[0]?.Trim(); |
| 87 | + if (double.TryParse(parts[1].Trim(), out double amount)) |
| 88 | + { |
| 89 | + // Update the balance based on transaction type |
| 90 | + if (transactionType?.ToUpper() is "DEPOSIT") |
| 91 | + yield return (TransactionType.Deposit, amount); |
| 92 | + else if (transactionType?.ToUpper() is "WITHDRAWAL") |
| 93 | + yield return (TransactionType.Withdrawal, amount); |
| 94 | + } |
| 95 | + yield return (default, 0.0); |
| 96 | + } |
| 97 | + } |
| 98 | + // </IsEnumValue> |
| 99 | +} |
| 100 | + |
| 101 | +static async Task SwitchEnumValueExample(string inputText1) |
| 102 | +{ |
| 103 | + // <SwitchEnumValue> |
| 104 | + double currentBalance = 0.0; |
| 105 | + |
| 106 | + await foreach (var transaction in TransactionRecords(inputText1)) |
| 107 | + { |
| 108 | + currentBalance += transaction switch |
| 109 | + { (TransactionType.Deposit, var amount) => amount, |
| 110 | + (TransactionType.Withdrawal, var amount) => -amount, |
| 111 | + _ => 0.0 |
| 112 | + }; |
| 113 | + Console.WriteLine($"{transaction.type} => Parsed Amount: {transaction.amount}, New Balance: {currentBalance}"); |
| 114 | + } |
| 115 | + |
| 116 | + static async IAsyncEnumerable<(TransactionType type, double amount)> TransactionRecords(string inputText) |
| 117 | + { |
| 118 | + var reader = new StringReader(inputText); |
| 119 | + string? line; |
| 120 | + while ((line = await reader.ReadLineAsync()) != null) |
| 121 | + { |
| 122 | + string[] parts = line.Split(','); |
| 123 | + |
| 124 | + string? transactionType = parts[0]?.Trim(); |
| 125 | + if (double.TryParse(parts[1].Trim(), out double amount)) |
| 126 | + { |
| 127 | + // Update the balance based on transaction type |
| 128 | + if (transactionType?.ToUpper() is "DEPOSIT") |
| 129 | + yield return (TransactionType.Deposit, amount); |
| 130 | + else if (transactionType?.ToUpper() is "WITHDRAWAL") |
| 131 | + yield return (TransactionType.Withdrawal, amount); |
| 132 | + } |
| 133 | + yield return (default, 0.0); |
| 134 | + } |
| 135 | + } |
| 136 | + // </SwitchEnumValue> |
| 137 | +} |
| 138 | + |
| 139 | +// <FinalProgram> |
| 140 | +public static class ExampleProgram |
| 141 | +{ |
| 142 | + static string bankRecords = """ |
| 143 | + DEPOSIT, 10000, Initial balance |
| 144 | + DEPOSIT, 500, regular deposit |
| 145 | + WITHDRAWAL, 1000, rent |
| 146 | + DEPOSIT, 2000, freelance payment |
| 147 | + WITHDRAWAL, 300, groceries |
| 148 | + DEPOSIT, 700, gift from friend |
| 149 | + WITHDRAWAL, 150, utility bill |
| 150 | + DEPOSIT, 1200, tax refund |
| 151 | + WITHDRAWAL, 500, car maintenance |
| 152 | + DEPOSIT, 400, cashback reward |
| 153 | + WITHDRAWAL, 250, dining out |
| 154 | + DEPOSIT, 3000, bonus payment |
| 155 | + WITHDRAWAL, 800, loan repayment |
| 156 | + DEPOSIT, 600, stock dividends |
| 157 | + WITHDRAWAL, 100, subscription fee |
| 158 | + DEPOSIT, 1500, side hustle income |
| 159 | + WITHDRAWAL, 200, fuel expenses |
| 160 | + DEPOSIT, 900, refund from store |
| 161 | + WITHDRAWAL, 350, shopping |
| 162 | + DEPOSIT, 2500, project milestone payment |
| 163 | + WITHDRAWAL, 400, entertainment |
| 164 | + """; |
| 165 | + |
| 166 | + public static async Task Main() |
| 167 | + { |
| 168 | + double currentBalance = 0.0; |
| 169 | + |
| 170 | + await foreach (var transaction in TransactionRecordType(bankRecords)) |
| 171 | + { |
| 172 | + currentBalance += transaction switch |
| 173 | + { |
| 174 | + Deposit d => d.Amount, |
| 175 | + Withdrawal w => -w.Amount, |
| 176 | + _ => 0.0 |
| 177 | + }; |
| 178 | + Console.WriteLine($" {transaction} => New Balance: {currentBalance}"); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + public static async IAsyncEnumerable<object?> TransactionRecordType(string inputText) |
| 183 | + { |
| 184 | + var reader = new StringReader(inputText); |
| 185 | + string? line; |
| 186 | + while ((line = await reader.ReadLineAsync()) != null) |
| 187 | + { |
| 188 | + string[] parts = line.Split(','); |
| 189 | + |
| 190 | + string? transactionType = parts[0]?.Trim(); |
| 191 | + if (double.TryParse(parts[1].Trim(), out double amount)) |
| 192 | + { |
| 193 | + // Update the balance based on transaction type |
| 194 | + if (transactionType?.ToUpper() is "DEPOSIT") |
| 195 | + yield return new Deposit(amount, parts[2]); |
| 196 | + else if (transactionType?.ToUpper() is "WITHDRAWAL") |
| 197 | + yield return new Withdrawal(amount, parts[2]); |
| 198 | + } |
| 199 | + yield return default; |
| 200 | + } |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +public enum TransactionType |
| 205 | +{ |
| 206 | + Deposit, |
| 207 | + Withdrawal |
| 208 | +} |
| 209 | + |
| 210 | + |
| 211 | +public record Deposit(double Amount, string description); |
| 212 | +public record Withdrawal(double Amount, string description); |
| 213 | + |
| 214 | +// </FinalProgram> |
0 commit comments