Java version solving popular question "ingenious machine to assist in reading letters and faxes sent in by branch offices."

/** * */ package cts.demo.bank; /** * @author ravivarma * */ import java.util.regex.Matcher; import java.util.regex.Pattern; public class IngeniousMachineExtractValidator implements IngeniousMachineReaderImplement { @Override public int parseNumber(String accountCatageory) { if (IngeniousMachineInputValues.IngeniousMachine_ONE.equals(accountCatageory)) return 1; if (IngeniousMachineInputValues.IngeniousMachine_TWO.equals(accountCatageory)) return 2; if (IngeniousMachineInputValues.IngeniousMachine_THREE.equals(accountCatageory)) return 3; if (IngeniousMachineInputValues.IngeniousMachine_FOUR.equals(accountCatageory)) return 4; if (IngeniousMachineInputValues.IngeniousMachine_FIVE.equals(accountCatageory)) return 5; if (IngeniousMachineInputValues.IngeniousMachine_SIX.equals(accountCatageory)) return 6; if (IngeniousMachineInputValues.IngeniousMachine_SEVEN.equals(accountCatageory)) return 7; if (IngeniousMachineInputValues.IngeniousMachine_EIGHT.equals(accountCatageory)) return 8; if (IngeniousMachineInputValues.IngeniousMachine_NINE.equals(accountCatageory)) return 9; return -1; } @Override public void validateInputFromFile(String actualAccountNumber) { Pattern actPattern = Pattern .compile(IngeniousMachineInputValues.validFormat); Matcher actMatcher = actPattern.matcher(actualAccountNumber); boolean invalid = actMatcher.find(); if (invalid) throw new IngeniousMachineReaderException( "Bank IngeniousMachine Contains Ivalid Characters"); } } +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /** * */ package cts.demo.bank; /** * @author ravivarma * */ public interface IngeniousMachineInputValues { String IngeniousMachine_ONE = " | | "; String IngeniousMachine_TWO = " _ _||_ "; String IngeniousMachine_THREE = " _ _| _|"; String IngeniousMachine_FOUR = " |_| |"; String IngeniousMachine_FIVE = " _ |_ _|"; String IngeniousMachine_SIX = " _ |_ |_|"; String IngeniousMachine_SEVEN = " _ | |"; String IngeniousMachine_EIGHT = " _ |_||_|"; String IngeniousMachine_NINE = " _ |_| _|"; String validFormat = ".[^\\s_|]"; } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /** * */ package cts.demo.bank; /** * @author ravivarma * */ import java.util.regex.Matcher; import java.util.regex.Pattern; import cts.demo.bank.IngeniousMachineExtractValidator; public class IngeniousMachineReader { IngeniousMachineReaderImplement machineInputValues = new IngeniousMachineExtractValidator(); public String getBankAccountNumber(String actualAccountNumber) { machineInputValues.validateInputFromFile(actualAccountNumber); String bankAcctRegEx = ".{3}"; Pattern fileInputTypeValues = Pattern.compile(bankAcctRegEx); Matcher accValue = fileInputTypeValues.matcher(actualAccountNumber); int start = 0; int mid = 27; int end = 54; String accountCategeory = ""; String accountNum = ""; int returnActNumber = 0; int totalNum = 9; while (totalNum > 0) { if (accValue.find(start)) { accountCategeory += accValue.group(); start += 3; } if (accValue.find(mid)) { accountCategeory += accValue.group(); mid += 3; } if (accValue.find(end)) { accountCategeory += accValue.group(); end += 3; } returnActNumber = machineInputValues.parseNumber(accountCategeory); accountCategeory = ""; accountNum += String.valueOf(returnActNumber); totalNum = totalNum - 1; } return accountNum; } } +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /** * */ package cts.demo.bank; /** * @author ravivarma * */ @SuppressWarnings("serial") public class IngeniousMachineReaderException extends RuntimeException { public IngeniousMachineReaderException(String invalidFormat) { super(invalidFormat); } } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /** * */ package cts.demo.bank; /** * @author ravivarma * */ public interface IngeniousMachineReaderImplement { int parseNumber(String actNumber); void validateInputFromFile(String actualAccountNumber); } +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /** * */ package cts.demo.bank; /** * @author ravivarma * */ import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; public class IngeniousMachineTest { IngeniousMachineReader ingeniousMachineReader; @Before public void setUp() throws Exception { ingeniousMachineReader = new IngeniousMachineReader(); } @After public void tearDown() throws Exception { ingeniousMachineReader = null; } @Test public void shouldScanValidBankAccountNumber() { String expectedAccountNumber = "123456789"; String actualAccountNumber = " _ _ _ _ _ _ _ " + " | _| _||_||_ |_ ||_||_|" + " | |_ _| | _||_| ||_| _|"; assertEquals(expectedAccountNumber, ingeniousMachineReader.getBankAccountNumber(actualAccountNumber)); } @Test(expected = IngeniousMachineReaderException.class) public void shouldThrowExceptionForInValidBankAccountNumber() { String actualAccountNumber = " _ _ _ _ _ _ _ " + " | @| _||_||_ |_ ||_||_|" + " | |_ _| | _||_|? ||_| _|"; ingeniousMachineReader.getBankAccountNumber(actualAccountNumber); } }
When I attended interview in CTS, they asked to create an application for below problem.

You work for a bank, which has recently purchased an ingenious machine to assist in reading letters and faxes sent in by branch offices. The machine scans the paper documents, and produces a file with a number of entries which each look like this:


_ _ _ _ _ _ _
| _| _||_||_ |_ ||_||_|
||_ _| | _||_| ||_| _|


Each entry is 4 lines long, and each line has 27 characters. The first 3 lines of each entry contain an account number written using pipes and underscores, and the fourth line is blank. Each account number should have 9 digits, all of which should be in the range 0-9. A normal file contains around 500 entries.

Your first task is to write a program that can take this file and parse it into actual account numbers.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.