User Interaction with chain List Items - c#

I have the following List items and try to find out an algorithm which takes care of user interaction.
tableItems.Add (new TableItem() {Start=1000, End=4000});
tableItems.Add (new TableItem() {Start=4000, End=6000});
tableItems.Add (new TableItem() {Start=6000, End=8000});
For example in the list, if user changes the first item "End" value that affects also second list item's "Start" value. That has to match. It is like a chain. Also, if user wants to change second item "Start" value, then the first item "End" value has to match as well.
What is effective way of solving this chain problem?

void Main()
{
var rangeManager = new RangeManager();
rangeManager.SetRange(3, 6000, 9000);
rangeManager.GetRange(3).Dump();
rangeManager.SetRange(1, 500, 4000);
rangeManager.GetRange(1).Dump();
}
public class RangeManager
{
private List<int> _values;
public RangeManager()
{
_values = new List<int> { 0, 1000, 4000, 6000, 8000 };
}
public Range GetRange(int index)
{
// TODO check for index out of range
return new Range(_values[index], _values[index + 1]);
}
public void SetRange(int index, int start, int finish)
{
// TODO check for index out of range
// TODO make sure start less than finish
// TODO make sure start greater than previous value
// TODO make sure finish less than next + 1 value
_values[index] = start;
_values[index + 1] = finish;
}
}
public class Range
{
public int Start { get; private set; }
public int Finish { get; private set; }
public Range(int start, int finish)
{
Start = start;
Finish = finish;
}
}
To understand what's going on here, start with the Range class. It is simply a holder for Start and Finish.
Next, look at the RangeManager class. It encapsulates the management of your chained ranges. It holds the values (range endpoints) in a list on integers (rather than a list of Range objects). This is a better way to store the data, because you no longer need to update neighboring objects when updating a range. Also, note that it's better to use integers than strings. The RangeManager class also has methods to help you manipulate and output your ranges.
Finally, look at the Main method. This shows an example of how to use the RangeManager and allows you to test whether it is working properly.

Related

Making a simple change function for a database app C#

Sorry if this is an ultra beginner question but, I need to be able to change all field values except the item number by giving its item number not array index. I already have a add and delete feature.
using System;
struct ItemData
{
public int ItemNumber;
public string Description;
public double PricePerItem;
public int QuantityOnHand;
public double OurCostPerItem;
public double ValueOfItem;
}
class Program
{
public static void Main()
{
int invItems = 0;
var items = new ItemData[10];
while (true)
{
Console.Write("1. Add, 2. Change, 3. Delete, 4. List:");
string strx = Console.ReadLine();
var choice = int.Parse(strx);
Console.WriteLine();
switch (choice)
{
This is what I have so far but not sure where to start
case 2: //change items
{
Console.Write("Please enter an item ID No:");
string input = Console.ReadLine();
int changeItemNumber = int.Parse(input);
bool foundItem = false;
for (int x = 0; x < invItems; x++)
{
if (items[x].ItemNumber == changeItemNumber)
{
//code
}
}
if (!foundItem)
{
Console.WriteLine("Item {0} not found", changeItemNumber);
}
break;
}
It's still a little unclear what you're asking so if this is wide of the mark then feel free to let me know.
Start by mapping out the flow of the process and then implement one piece at a time. Here's a basic series of operations you'll want to do, not necessarily in this order:
Input ItemNumber.
Find index of matching record in array.
If not found report error and stop.
Get record from user.
Set ItemNumber in record to entered value.
Store record in array at same index as original.
If you think about the way the rest of the program works you should see that some of those parts - maybe most of them - are already done somewhere in your code. Your add operation probably already has the Get record from user code. Your delete operation probably has Input ItemNumber and Find index of matching record in array done already.
So pull those parts out into methods and reuse them where it makes sense. Anywhere you're writing the same code multiple times with minor changes try to see if you can extract the code into a small method you can call with maybe some parameters. Your "input a number" code for instance - used for operation selection, item number entry and presumably whatever you're doing for delete - can be pulled out to a supporting method like this one:
static int InputNumber(string prompt)
{
Console.Write($"{prompt}: ");
var inputText = Console.ReadLine();
return int.Parse(inputText);
}
Now when you want to change the way you're doing number input you have a central place to make the changes and you don't have to worry about tracking down all the places you wrote the same code.
The same goes for the rest of the pieces. When two operations share some of the same code then pull that code out to supporting methods. Finding the index of an item in the array by some criteria can - and should - be one of those:
// Return array index of matching item or -1 if not found
int ItemIndex(int itemNumber)
{
for (int i = 0; i < invItems; i++)
{
if (items[i].ItemNumber == itemNumber)
return i;
}
return -1;
}
Likewise the code - whatever that looks like - that your add operation uses to get a record from the user.
Once you do that your change item case looks a lot simpler:
case 2: // change items
var itemNumber = InputNumber("Please enter an item ID");
var index = ItemIndex(itemNumber);
if (index == -1)
{
Console.WriteLine($"Item {itemNumber} not found.");
}
else
{
var newItem = InputItemData();
newItem.ItemNumber = itemNumber;
items[index] = newItem;
}
break;
For bonus points you can actually provide the current values to the InputItemData method so the user can re-use them instead of just asking for a complete new record each time.

C# code effectiveness - calling a property tens of times

IDE: Visual Studio 2015 Update 3
Language: C# / .NET 4.5
Situation: Suppose I defined a class and I'm calling its properties tens of times, further let's suppose the class operates over one input given to the constructor and therefore it makes all operations except the first one redundant, because we already managed to calculate the return value the first time we called it.
Example of such property:
// let's call it a Month, because it extracts a month code from a string
private int Month
{
// there is only a getter
get
{
// here's my current strategy
// in the beginning of the class I set fMonth to -1
// it can only have possitive numbers, so if already set, I return it
if (fMonth > -1)
return fMonth;
// and here's the part I don't want to repeat
return fMonth =
Convert.ToInt32(SomeNumberString.Substring(2, 2));
}
}
Question: Is this the right strategy for not repeating the executive code?
Since the value for someNumberString is given to you in the constructor, you can use a readonly property.
ctor(string someNumberString)
{
Month = Convert.ToInt32(someNumberString.Substring(2, 2));
}
public Month { get; }
You are on the right track with using a private backing field fmonth for the property. You can further optimize this by moving the conversion code to an explicit set method. This removes the if check from every get access.
ctor(string someNumberString) {
SetMonth(someNumberString);
}
private int Month { get { return fmonth; } }
// -1 indicates that SetMonth() has never been called
private int fmonth = -1;
public void SetMonth(string someNumberString) {
fmonth = Convert.ToInt32(someNumberString.Substring(2, 2));
}

Looking for something like a HashSet, but with a range of values for the key?

I'm wondering if there is something like HashSet, but keyed by a range of values.
For example, we could add an item which is keyed to all integers between 100 and 4000. This item would be returned if we used any key between 100 and 4000, e.g. 287.
I would like the lookup speed to be quite close to HashSet, i.e. O(1). It would be possible to implement this using a binary search, but this would be too slow for the requirements. I would like to use standard .NET API calls as much as possible.
Update
This is interesting: https://github.com/mbuchetics/RangeTree
It has a time complexity of O(log(N)) where N is number of intervals, so it's not exactly O(1), but it could be used to build a working implementation.
I don't believe there's a structure for it already. You could implement something like a RangedDictionary:
class RangedDictionary {
private Dictionary<Range, int> _set = new Dictionary<Range, int>();
public void Add(Range r, int key) {
_set.Add(r, key);
}
public int Get(int key) {
//find a range that includes that key and return _set[range]
}
}
struct Range {
public int Begin;
public int End;
//override GetHashCode() and Equals() methods so that you can index a Dictionary by Range
}
EDIT: changed to HashSet to Dictionary
Here is a solution you can try out. However it assumes some points :
No range overlaps
When you request for a number, it is effectively inside a range (no error check)
From what you said, this one is O(N), but you can make it O(log(N)) with little effort I think.
The idea is that a class will handle the range thing, it will basically convert any value given to it to its range's lower boundary. This way your Hashtable (here a Dictionary) contains the low boundaries as keys.
public class Range
{
//We store all the ranges we have
private static List<int> ranges = new List<int>();
public int value { get; set; }
public static void CreateRange(int RangeStart, int RangeStop)
{
ranges.Add(RangeStart);
ranges.Sort();
}
public Range(int value)
{
int previous = ranges[0];
//Here we will find the range and give it the low boundary
//This is a very simple foreach loop but you can make it better
foreach (int item in ranges)
{
if (item > value)
{
break;
}
previous = item;
}
this.value = previous;
}
public override int GetHashCode()
{
return value;
}
}
Here is to test it.
class Program
{
static void Main(string[] args)
{
Dictionary<int, int> myRangedDic = new Dictionary<int,int>();
Range.CreateRange(10, 20);
Range.CreateRange(50, 100);
myRangedDic.Add(new Range(15).value, 1000);
myRangedDic.Add(new Range(75).value, 5000);
Console.WriteLine("searching for 16 : {0}", myRangedDic[new Range(16).value].ToString());
Console.WriteLine("searching for 64 : {0}", myRangedDic[new Range(64).value].ToString());
Console.ReadLine();
}
}
I don't believe you really can go below O(Log(N)) because there is no way for you to know immediately in which range a number is, you must always compare it with a lower (or upper) bound.
If you had predetermined ranges, that would have been easier to do. i.e. if your ranges are every hundreds, it is really easy to find the correct range of any number by calculating it modulo 100, but here we can assume nothing, so we must check.
To go down to Log(N) with this solution, just replace the foreach with a loop that will look at the middle of the array, then split it in two every iteration...

C# - How to get item from Generic List with the highest specific parameter?

I've searched a lot but can't find the answer that I understand enough to translate into my project. What is the goal: I need to find an item with the highest armor_class parameter on the list, and add that items' armor_class to character's armor class.
So, we have a list created this way:
public List<Weapon> characterInvWeapon;
public List<Armor> characterInvArmor;
etc.
Here's how class Armor and its properties are created:
public class Armor : Item, IComparable <Armor> {
public string armor_prof;
public string armor_category;
public int armor_class;
public int armor_str_req;
public string armor_stealth_mod;
public Armor (string c_name
, string c_description
, bool c_stackable
, int c_value
, string c_coin_type
, int c_weight
, string c_armor_prof
, string c_armor_category
, int c_armor_class
, int c_armor_str_req
, string c_armor_stealth_mod) : base (c_name, c_description, c_stackable, c_value, c_coin_type, c_weight)
{
armor_prof = c_armor_prof;
armor_category = c_armor_category;
armor_class = c_armor_class;
armor_str_req = c_armor_str_req;
armor_stealth_mod = c_armor_stealth_mod;
}
public int CompareTo(Armor other)
{
if (armor_class == other.armor_class)
return String.Compare (name, other.name); // a < ab < b
else
return other.armor_class - armor_class;
}
}
Armor is a class inheriting from class Item, which has the first 6 properties. Armors are stored in a list specific to Armor - public List<Armor> characterInvArmor;.
Example item:
AddToItemStore(new Armor("Breastplate", "Description.", false, 400, "gp", 20, "Breastplate", "Medium Armor", 14, 0, ""));
Adding script:
public void AddToCharacterInventory(Item it)
{
if (it is Weapon)
{
charInvWeapon.Add((Weapon)it);
charInvWeapon.Sort();
}
else if (it is Armor)
{
charInvArmor.Add((Armor)it);
charInvArmor.Sort();
}
}
Now as I mentioned, I need to find an item on the list charInvArmor with highest armor_class parameter and use its value in other function, which calculates armor class from many variables.
So in other function, characterArmorClass = armorWithHighestArmorClass + otherVariable + someotherVariable; etc.
I suspect there are some handy shortcuts in Linq, but I'd be most thankful for some example without Linq. Linq would be welcome too, but I'm absolutely new to it and also I'm worried about performance and my apps compatibility with iPhone, for example. I've read iOS can cause problems with Linq. This must be fast and compatible calculation.
With LINQ:
int maxArmorClass = characterInvArmor.Max(armor => armor.armor_class);
Without LINQ with Sort:
var list = characterInvArmor.ToList(); // copy list, so we do not break sorted orded
list.Sort((armor1, armor2) => armor2.armor_class.CompareTo(armor1.armor_class));
int maxArmorClass = list[0].armor_class;
And of course you can always write a manual method with a cycle and "max" variable.
BTW, I noticed, that you sort charInvArmor in AddToCharacterInventory method. If array is always sorted, then, based on your CompareTo implementation, an item with maximum armor_class should always be last (or first, I'm not sure). So just take the last (first) element of your list.
You have to loop through the list and check each value since the list is not sorted based on the armor class value and you do not want to use LINQ:
int maxArmorClass = 0;
foreach (var armor in characterInvArmor)
{
// Do a comparison here and see if you found a higher value
// If a higher value is found, store it in maxArmorClass
}
As a side note I recommend the following links:
Public Fields versus Automatic Properties
and
Style guide for c# * In C#, Pascal casing and camel casing are the established conventions.

Access Enumerator within a foreach loop?

I have a List class, and I would like to override GetEnumerator() to return my own Enumerator class. This Enumerator class would have two additional properties that would be updated as the Enumerator is used.
For simplicity (this isn't the exact business case), let's say those properties were CurrentIndex and RunningTotal.
I could manage these properties within the foreach loop manually, but I would rather encapsulate this functionality for reuse, and the Enumerator seems to be the right spot.
The problem: foreach hides all the Enumerator business, so is there a way to, within a foreach statement, access the current Enumerator so I can retrieve my properties? Or would I have to foreach, use a nasty old while loop, and manipulate the Enumerator myself?
Strictly speaking, I would say that if you want to do exactly what you're saying, then yes, you would need to call GetEnumerator and control the enumerator yourself with a while loop.
Without knowing too much about your business requirement, you might be able to take advantage of an iterator function, such as something like this:
public static IEnumerable<decimal> IgnoreSmallValues(List<decimal> list)
{
decimal runningTotal = 0M;
foreach (decimal value in list)
{
// if the value is less than 1% of the running total, then ignore it
if (runningTotal == 0M || value >= 0.01M * runningTotal)
{
runningTotal += value;
yield return value;
}
}
}
Then you can do this:
List<decimal> payments = new List<decimal>() {
123.45M,
234.56M,
.01M,
345.67M,
1.23M,
456.78M
};
foreach (decimal largePayment in IgnoreSmallValues(payments))
{
// handle the large payments so that I can divert all the small payments to my own bank account. Mwahaha!
}
Updated:
Ok, so here's a follow-up with what I've termed my "fishing hook" solution. Now, let me add a disclaimer that I can't really think of a good reason to do something this way, but your situation may differ.
The idea is that you simply create a "fishing hook" object (reference type) that you pass to your iterator function. The iterator function manipulates your fishing hook object, and since you still have a reference to it in your code outside, you have visibility into what's going on:
public class FishingHook
{
public int Index { get; set; }
public decimal RunningTotal { get; set; }
public Func<decimal, bool> Criteria { get; set; }
}
public static IEnumerable<decimal> FishingHookIteration(IEnumerable<decimal> list, FishingHook hook)
{
hook.Index = 0;
hook.RunningTotal = 0;
foreach(decimal value in list)
{
// the hook object may define a Criteria delegate that
// determines whether to skip the current value
if (hook.Criteria == null || hook.Criteria(value))
{
hook.RunningTotal += value;
yield return value;
hook.Index++;
}
}
}
You would utilize it like this:
List<decimal> payments = new List<decimal>() {
123.45M,
.01M,
345.67M,
234.56M,
1.23M,
456.78M
};
FishingHook hook = new FishingHook();
decimal min = 0;
hook.Criteria = x => x > min; // exclude any values that are less than/equal to the defined minimum
foreach (decimal value in FishingHookIteration(payments, hook))
{
// update the minimum
if (value > min) min = value;
Console.WriteLine("Index: {0}, Value: {1}, Running Total: {2}", hook.Index, value, hook.RunningTotal);
}
// Resultint output is:
//Index: 0, Value: 123.45, Running Total: 123.45
//Index: 1, Value: 345.67, Running Total: 469.12
//Index: 2, Value: 456.78, Running Total: 925.90
// we've skipped the values .01, 234.56, and 1.23
Essentially, the FishingHook object gives you some control over how the iterator executes. The impression I got from the question was that you needed some way to access the inner workings of the iterator so that you could manipulate how it iterates while you are in the middle of iterating, but if this is not the case, then this solution might be overkill for what you need.
With foreach you indeed can't get the enumerator - you could, however, have the enumerator return (yield) a tuple that includes that data; in fact, you could probably use LINQ to do it for you...
(I couldn't cleanly get the index using LINQ - can get the total and current value via Aggregate, though; so here's the tuple approach)
using System.Collections;
using System.Collections.Generic;
using System;
class MyTuple
{
public int Value {get;private set;}
public int Index { get; private set; }
public int RunningTotal { get; private set; }
public MyTuple(int value, int index, int runningTotal)
{
Value = value; Index = index; RunningTotal = runningTotal;
}
static IEnumerable<MyTuple> SomeMethod(IEnumerable<int> data)
{
int index = 0, total = 0;
foreach (int value in data)
{
yield return new MyTuple(value, index++,
total = total + value);
}
}
static void Main()
{
int[] data = { 1, 2, 3 };
foreach (var tuple in SomeMethod(data))
{
Console.WriteLine("{0}: {1} ; {2}", tuple.Index,
tuple.Value, tuple.RunningTotal);
}
}
}
You can also do something like this in a more Functional way, depending on your requirements. What you are asking can be though of as "zipping" together multiple sequences, and then iterating through them all at once. The three sequences for the example you gave would be:
The "value" sequence
The "index" sequence
The "Running Total" Sequence
The next step would be to specify each of these sequences seperately:
List<decimal> ValueList
var Indexes = Enumerable.Range(0, ValueList.Count)
The last one is more fun... the two methods I can think of are to either have a temporary variable used to sum up the sequence, or to recalculate the sum for each item. The second is obviously much less performant, I would rather use the temporary:
decimal Sum = 0;
var RunningTotals = ValueList.Select(v => Sum = Sum + v);
The last step would be to zip these all together. .Net 4 will have the Zip operator built in, in which case it will look like this:
var ZippedSequence = ValueList.Zip(Indexes, (value, index) => new {value, index}).Zip(RunningTotals, (temp, total) => new {temp.value, temp.index, total});
This obviously gets noisier the more things you try to zip together.
In the last link, there is source for implementing the Zip function yourself. It really is a simple little bit of code.

Categories

Resources