In the current article, I will show how to resolve with VBA an entry exam for programming in HackBulgaria. The problems could be resolved in any language and I will use VBA for this last task. The program is actually the development of a cipher, thus it takes string as input and codes it as per the cipher. This is how the task looks like:
Make an interface, called MyLogger
with only 1 method – log(level, message)
The two arguments should be:
level
– an integer, from 1 to 3.- 1 means that you are logging with
INFO
level. - 2 means that you are logging with
WARNING
level. - 3 means that you are logging with
PLSCHECKFFS
level. message
is a string, that you are logging.
Make 3 different classes, that implement the interface MyLogger
:
ConsoleLogger
The ConsoleLogger
should log the messages directly to the console.
FileLogger
The FileLogger
should log the messages to a given file.
HTTPLogger
The HTTPLogger
shoud log the messages via a POST request to a given HTTP url.
What I did?
Something like this:
3 Classes, 1 interface with the method log, and one method called GenerateString in the class ConsoleLogger. Probably its place was not there, but it worked. Anyway, here comes the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Net; using System.Collections.Specialized; using System.Web; using System.Diagnostics; class SimpleLogger { public static void Main() { var log1 = new ConsoleLogger(); var log2 = new FileLogger(); var log3 = new HTTPLogger(); log1.log(1, "Hello World"); log1.log(2, "Hello World"); log1.log(3, "Hello Beautiful World"); log2.log(2, "Hello FileLogger"); log3.log(3, "Hello HTTP Logger"); } } interface IMyLogger { void log(int iLevel, string sMessage); } public class ConsoleLogger : IMyLogger { public void log(int iLevel, string sMessage) { Console.WriteLine(GenerateString(iLevel, sMessage)); } public string GenerateString(int ilevel, string sMessage) { string sTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssK"); string sColumns = "::"; string sLevel = ""; switch (ilevel) { case 1: sLevel = "INFO"; break; case 2: sLevel = "WARNING"; break; case 3: sLevel = "PLSCHECKFFS"; break; default: break; } return sLevel + sColumns + sTime + sColumns + sMessage; } } class FileLogger : IMyLogger { public void log(int iLevel, string sMessage) { ConsoleLogger Text = new ConsoleLogger(); string sMessageInside = Text.GenerateString(iLevel, sMessage); File.WriteAllText(@"GivenFile.txt", sMessageInside); } } class HTTPLogger : IMyLogger { public void log(int iLevel, string sMessage) { ConsoleLogger Text2 = new ConsoleLogger(); string postData = Text2.GenerateString(iLevel, sMessage); //Taken from: //http://stackoverflow.com/questions/1502500/how-to-use-webrequest-to-post-data-and-get-response-from-a-webpage // Create a request using a URL that can receive a post. WebRequest request = WebRequest.Create("https://www.vitoshacademy.com"); // Set the Method property of the request to POST. request.Method = "POST"; // Create POST data and convert it to a byte array. byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded"; // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. WebResponse response = request.GetResponse(); // Display the status. Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. Console.WriteLine(responseFromServer); // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); } } |
Enjoy it! 🙂