Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Validation along token stack feature
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { private const string tokenPrefix = " x "; public static void Main(string[] args) { var itemA = new Item() { Quantity = 1, ObjectId = "Normal Token x 1" }; var itemB = new Item() { Quantity = 2, ObjectId = "Normal Token x 2" }; var itemC = new Item() { Quantity = 3, ObjectId = "Rare Token x 3" }; Handle(itemA, itemB); Handle(itemA, itemC); Handle(itemB, itemC); } private static void Handle(Item itemA, Item itemB) { var prefix = string.Empty; if (ValidateStackable(itemA, itemB, ref prefix)) { StackWrite(itemA.ObjectId, itemB.ObjectId, true); ResultWrite(itemA, itemB, prefix); } else StackWrite(itemA.ObjectId, itemB.ObjectId, false); } private static void StackWrite(string a, string b, bool stackable) { Write(string.Format("'{0}' {1} stack with '{2}'!", a, stackable ? "can" : "cannot", b)); } private static void ResultWrite(Item itemA, Item itemB, string prefix) { Write(string.Format("'{0}'({1}) stacked with '{2}'({3}) results in '{4}'({5}).\n" , itemA.ObjectId, itemA.Quantity.ToString(), itemB.ObjectId, itemB.Quantity.ToString() , prefix, (itemA.Quantity + itemB.Quantity).ToString())); } private static void Write(string message) { Console.WriteLine(message); } private static bool ValidateStackable(Item itemA, Item itemB, ref string prefix) { return itemA.Quantity > 0 && itemB.Quantity > 0 && IsSameKindOfItem(itemA, itemB, ref prefix); } private static bool IsSameKindOfItem(Item itemA, Item itemB, ref string prefix) { var itemAObjId = itemA.ObjectId; var itemBObjId = itemB.ObjectId; if (itemAObjId.Contains(tokenPrefix) && itemBObjId.Contains(tokenPrefix)) { var itemAPrefix = itemAObjId.IndexOf(tokenPrefix); var itemBPrefix = itemBObjId.IndexOf(tokenPrefix); var itemAName = itemAObjId.Substring(0, itemAPrefix); var itemBName = itemBObjId.Substring(0, itemBPrefix); var result = itemAName == itemBName; if (result) prefix = itemAName; else prefix = "invalid"; return result; } else return false; } } public class Item { public int Quantity; public string ObjectId; } }
run
|
edit
|
history
|
help
0
DateTime.Now.Date
3
Intuit // C# // Lecture_4 // Laba_#2 _ZAD#2
Digging Deeper
minimal path for triangle
Qwe
program
Tarkov Time
TrzyKreski
Microsoft Hello World