User Story Instructions:
The following exercise has one user story to implement. Your goal is to have a working .NET program that implements the use case.
You can assume the input is valid. You don't need to handle unexpected characters or invalid spacing in the input file.
Guidelines:
We are looking for:
- Working code.
- Unit Tests.
- Readable code.
- Simple code.
Keep your code simple, don't over engineer. Good luck!
User Story 1 - Parse invoice numbers
We have recently decided to digitise our old invoice archives. Since finding a
volunteer for such an arduous task was impossible, an employee was selected at
random and instructed to type in all invoice numbers into a text file.
Little did we know that the employee we picked is an aspiring ASCII artist.
Instead of handing us a file containing a set of numbers, we ended up with
7-segment display representations of the invoice numbers.
This is where you come in. Write a simple .NET program that allows the user to upload a text file of 7-segment invoice numbers, and outputs a list with the parsed invoice numbers.
Input:
-
A text file containing several hundreds of invoice numbers in the following
form:
_ _ _ _ _ _ _ (line 1)
| _| _||_||_ |_ ||_||_| (line 2)
||_ _| | _||_| ||_| _| (line 3)
(line 4)
_ _ _ _ _ _ _ (line 5)
|_||_|| ||_||_ | | ||_ (line 6)
| _||_||_||_| | | | _| (line 7)
(line 8)
Invoice number format:
- Each invoice number is constructed of 9 digits [0..9]
- Invoice number is written using _ and | characters.
- Invoice number input takes 4 lines.
- The first 3 lines contain 27 characters.
- The fourth line is blank.
Note: You can use input_user_story_1.txt
to feed your program.
Output:
User Story 2 - Top Secret
During your in-person interview, you will be asked to modify your code to handle User Story 2. So be sure your code can be easily modified. More details will be given during the interview.
Input:
Output:
Solution of user story in c# code
Use below code for solution. Create a class ParseInvoiceNumbers and paste below code in it.
public class ParseInvoiceNumbers
{
public List ReadInvoiceFile()
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"TestData\input_user_story_1.txt");
string[] files = File.ReadAllLines(path);
return Converrt7SegmentToNumber(files);
}
private List Converrt7SegmentToNumber(string[] list)
{
List invoiceList = new List ();
for (int i = 0; i < list.Length; i += 4)
{
string invoiceNo = "";
string line = list[i];
string line2 = list[i + 1];
string line3 = list[i + 2];
int sum = 0;
int counter = 0;
bool isLeagal = true;
for (int j = 0; j < line.Length;)
{
counter++;
if (!string.IsNullOrWhiteSpace(line[j].ToString()))
sum += (int)Math.Pow(2, counter);
if (!string.IsNullOrWhiteSpace(line2[j].ToString()))
sum += (int)Math.Pow(2, (counter + 2));
if (!string.IsNullOrWhiteSpace(line3[j].ToString()))
sum += (int)Math.Pow(2, counter + 5);
j++;
if (j > 0 && j % 3 == 0)
{
int res = ReturnNumber(sum);
if (res < 0)
isLeagal = false;
Console.Write((res < 0) ? "?" : res.ToString());
invoiceNo += (res < 0) ? "?" : res.ToString();
sum = counter = 0;
}
}
if (!isLeagal)
{
Console.Write(" ILLEGAL");
invoiceNo += " ILLEGAL";
}
invoiceList.Add(invoiceNo);
isLeagal = true;
Console.WriteLine();
}
return invoiceList;
}
private int ReturnNumber(int product)
{
switch (product)
{
case 492:
return 0;
case 288:
return 1;
case 244:
return 2;
case 436:
return 3;
case 312:
return 4;
case 412:
return 5;
case 476:
return 6;
case 292:
return 7;
case 508:
return 8;
case 444:
return 9;
default:
return -1;
}
}
}
Call ParseInvoiceNumbers form the main program.
public class Program
{
static void Main(string[] args)
{
ParseInvoiceNumbers invoice = new ParseInvoiceNumbers();
invoice.ReadInvoiceFile();
Console.Read();
}
}
For any question please comment below.