-
-
Save cvpfus/b8500365633f67a0bf39f675905394b2 to your computer and use it in GitHub Desktop.
using System.Text; | |
namespace FoobarPattern; | |
public class Foobar | |
{ | |
private int _patternLength; | |
private Dictionary<int, string> _patternDict = new(); | |
private StringBuilder _sb = new(); | |
public Foobar(int patternLength) | |
{ | |
CheckNegativeLength(patternLength); | |
_patternDict = new Dictionary<int, string>(){{3, "foo"}, {5, "bar"}}; | |
_patternLength = patternLength; | |
} | |
public Foobar(Dictionary<int, string> patternDict, int patternLength) | |
{ | |
CheckNegativeKey(patternDict); | |
CheckNegativeLength(patternLength); | |
_patternDict = patternDict; | |
_patternLength = patternLength; | |
} | |
public void SetPatternLength(int patternLength) | |
{ | |
CheckNegativeLength(patternLength); | |
_patternLength = patternLength; | |
} | |
public void Add(int key, string value) | |
{ | |
CheckNegativeKey(key); | |
_patternDict.Add(key, value); | |
} | |
public void Add(Dictionary<int, string> patternDict) | |
{ | |
CheckNegativeKey(patternDict); | |
foreach (var kvp in patternDict) | |
{ | |
_patternDict.Add(kvp.Key, kvp.Value); | |
} | |
} | |
public void Update(int key, string newValue) | |
{ | |
if (_patternDict.ContainsKey(key)) _patternDict[key] = newValue; | |
else throw new KeyNotFoundException(); | |
} | |
public void Delete(int key) | |
{ | |
if (_patternDict.ContainsKey(key)) _patternDict.Remove(key); | |
else throw new KeyNotFoundException(); | |
} | |
private void CheckNegativeKey(Dictionary<int, string> patternDict) | |
{ | |
foreach (var kvp in patternDict) | |
{ | |
if (kvp.Key < 0) throw new NegativeNumberException(); | |
} | |
} | |
private void CheckNegativeKey(int key) | |
{ | |
if (key < 0) throw new NegativeNumberException(); | |
} | |
private void CheckNegativeLength(int patternLength) | |
{ | |
if (patternLength < 0) throw new NegativeNumberException(); | |
} | |
public string GetPattern() | |
{ | |
_sb.Clear(); | |
for (int i = 0; i <= _patternLength; i++) | |
{ | |
string pattern = ""; | |
bool hasPattern = false; | |
if (i == 0) _sb.Append("0, "); | |
else | |
{ | |
foreach (var kvp in _patternDict) | |
{ | |
if (i % kvp.Key == 0) | |
{ | |
pattern += kvp.Value; | |
hasPattern = true; | |
} | |
} | |
if (!hasPattern) | |
{ | |
pattern = i.ToString(); | |
} | |
pattern += ", "; | |
_sb.Append(pattern); | |
} | |
} | |
return _sb.Replace(", ", ".", _sb.Length - 2, 2).ToString(); | |
} | |
} |
namespace FoobarPattern; | |
public class NegativeNumberException : Exception | |
{ | |
public NegativeNumberException(string message = "Number can't be negative.") : base(message) | |
{ | |
} | |
} |
using FoobarPattern; | |
public class Program | |
{ | |
static void Main() | |
{ | |
Foobar foobar = new Foobar(new Dictionary<int, string>(){{3, "foo"}, {5, "bar"}}, 15); | |
Console.WriteLine(foobar.GetPattern()); | |
foobar.Add(7, "fizz"); | |
Console.WriteLine(foobar.GetPattern()); | |
foobar.Update(5, "buzz"); | |
Console.WriteLine(foobar.GetPattern()); | |
foobar.Delete(3); | |
Console.WriteLine(foobar.GetPattern()); | |
/* | |
This code produces the following output: | |
0, 1, 2, foo, 4, bar, foo, 7, 8, foo, bar, 11, foo, 13, 14, foobar. | |
0, 1, 2, foo, 4, bar, foo, fizz, 8, foo, bar, 11, foo, 13, fizz, foobar. | |
0, 1, 2, foo, 4, buzz, foo, fizz, 8, foo, buzz, 11, foo, 13, fizz, foobuzz. | |
0, 1, 2, 3, 4, buzz, 6, fizz, 8, 9, buzz, 11, 12, 13, fizz, buzz. | |
*/ | |
} | |
} |
@kinarajv tampilan lain itu maksudnya seperti di windows form, web app, android dll kah mas? berarti solusinya perlu dijadikan library / class terpisah ya mas?
Ini business logicnya dijadiin library aja, supaya dia tidak coupled dengan console writeline, usernya (Program.cs) mau pakai WPF, Web, Terminal, tetap bisa digunakan librarynya seperti itu
Lalu ada masukan, coba pikirkan ketika user akan menambahkan kondisi tambahan selain foo 3 dan 5 bar diatas, misal menambah 7 sebagai fizz, atau mengubah 5 menjadi buzz, apakah program kamu saat ini sudah support dengan penambahan kondisi tersebut
Lalu ada masukan, coba pikirkan ketika user akan menambahkan kondisi tambahan selain foo 3 dan 5 bar diatas, misal menambah 7 sebagai fizz, atau mengubah 5 menjadi buzz, apakah program kamu saat ini sudah support dengan penambahan kondisi tersebut
Sudah saya tambahkan mas, bisa dicek misal perlu saran lagi 😁
programnya sudah saya update ya mas, sudah saya pisah dengan class