Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submit Ch3 HW 10 #802

Open
wants to merge 1 commit into
base: Chapter3/Homework/10
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Src/BootCamp.Chapter/Car.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace BootCamp.Chapter
{
// This should have the following attribute
// [Textable]
[TextTable]
public class Car
{
public string Brand { get; }
Expand Down
2 changes: 1 addition & 1 deletion Src/BootCamp.Chapter/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace BootCamp.Chapter
{
// This should have the following attribute
// [TextTable(1, '=', 'x', '*')]
[TextTable(1, '=', 'x', '*')]
public class Person
{
public string Name { get; }
Expand Down
90 changes: 85 additions & 5 deletions Src/BootCamp.Chapter/TextBoxPrinter.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,92 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace BootCamp.Chapter
{
public static class TextBoxPrinter
{
// TODO: Implement.
public static string Print(object obj) => "";
}
public static class TextBoxPrinter
{
public static string Print(object obj)
{
//Get obj type
Type objType = obj.GetType();
//Get TextTableAttribute from obj
TextTableAttribute textTableAttribute = objType.GetCustomAttribute<TextTableAttribute>();

//If there's no attribute attached then just return the obj's ToString result
if (textTableAttribute == null) { return obj.ToString(); }

//Get obj text per line
string[] textLines = obj.ToString().Split(Environment.NewLine);

//Build the text box
StringBuilder sb = new StringBuilder();
string corner = textTableAttribute.Corner.ToString();//To not call ToString multiple times
string sideLeft = textTableAttribute.SideLeft.ToString();//To not call ToString multiple times

//Get the max width of the object's text
int maxWidth = 0;
foreach (string textLine in textLines)
{
int width = textLine.Length;
if (width > maxWidth) { maxWidth = width; }
}
int spacePaddingLength = (maxWidth + (2 * textTableAttribute.Padding));//Length is max width + 2x padding (on both sides)
string linePaddingString = new string(' ', spacePaddingLength);

//Build top/bottom part of the box
StringBuilder sbTopBottom = new StringBuilder();
sbTopBottom.Append(corner);
sbTopBottom.Append(new string(textTableAttribute.SideTop, spacePaddingLength));
sbTopBottom.Append(corner);

//Add top to box
sb.Append(sbTopBottom);
sb.AppendLine();//AppendLine() doesn't take StringBuilder, but Append() does

//Build vertical padding line
StringBuilder sbVerticalPadding = new StringBuilder();
sbVerticalPadding.Append(sideLeft);
sbVerticalPadding.Append(linePaddingString);
sbVerticalPadding.AppendLine(sideLeft);

//Duplicated for each number of padding
StringBuilder sbVerticalPaddingLines = new StringBuilder();
for (int i = 0; i < textTableAttribute.Padding; i++)
{
sbVerticalPaddingLines.Append(sbVerticalPadding);
}

//Add Top padding
sb.Append(sbVerticalPaddingLines);

//Build middle sections
string paddingString = new string(' ', textTableAttribute.Padding);//Padding spaces
foreach (string textLine in textLines)
{
//Left wall
sb.Append(sideLeft);
//Padding
sb.Append(paddingString);
//Text
sb.Append(textLine);
//Padding
sb.Append(paddingString);
//Extra padding if the line doesn't have the max width
int extraPaddingLength = maxWidth - textLine.Length;
if (extraPaddingLength > 0) { sb.Append(new string(' ', extraPaddingLength)); }
//Right wall
sb.AppendLine(sideLeft);
}

//Add bottom padding
sb.Append(sbVerticalPaddingLines);

//Add bottom of the box
sb.Append(sbTopBottom);

return sb.ToString();
}
}
}
21 changes: 21 additions & 0 deletions Src/BootCamp.Chapter/TextTableAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BootCamp.Chapter
{
public class TextTableAttribute : Attribute
{
public int Padding { get; }
public char SideTop { get; }
public char SideLeft { get; }
public char Corner { get; }
public TextTableAttribute(int padding = 0, char sideTop = '-', char sideLeft = '|', char corner = '+')
{
Padding = padding;
SideTop = sideTop;
SideLeft = sideLeft;
Corner = corner;
}
}
}