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.
Related
I am trying to write a program that lets me enter a list of names with their respective film rating into an array (Would do a list but the course material wants me to use array). Before I add the names to the array I want to make sure that a valid rating has been entered.
I am currently using a for statement that cycles through he array length and lets the user enter each movie to the list that way. This happens in a while loop to make them re-enter the name if the rating is invalid before the name is committed to Array. I check the names by calling a method with a temporary string assigned with the current entered name and rating which will do some conditional checks and return either false or true depending on the outcome. But the way I am doing it is not working at all..
Problem is that I have no idea how to make practical use of the bool statement my method returns:
string[] filmNames = new string[ArrayLength];
for (int i = 0; i < filmNames.Length; i = i + 1)
{
bool ratingFail = true;
int displayNumber = i + 1;
while (ratingFail)
{
Console.Write($"> Enter the Name and Rating of film number {displayNumber} of {ArrayLength}: ");
string checkRating = Console.ReadLine();
CheckRating(checkRating); // currently just does "return false;" for testing purposes
if (true) // this statement is clearly not effected by whatever the return value is from above method. Why? What to do?
{
ratingFail = true;
}
else
{
filmNames[i] = checkRating; // this bit is marked as unreachable, which is is.
ratingFail = false;
}
}
}
my test method:
public static bool CheckRating(string checkRating)
{
return false;
}
I am VERY (a week) new to programming and C# so please keep in mind when answering that I may not understand particular lingo referring to programming terms outside of the scope of what you can see here within, but I will Google and research to the best of my abilities if there is no way to simplify what you want to say. Thank you for your time and effort.
if (true) // this statement is clearly not effected by whatever the return value is from above method. Why? What to do?
{
ratingFail = true;
}
This will always be true, because you're creating a variable that is always true. You want this:
ratingFail = CheckRating(checkRating)
if (!ratingFail) {
The rating is valid, do stuff here.
}
If ratingFail is true, the loop will continue.
This assumes that CheckRating returns true if the input is invalid, and false if it is valid. The variable naming here is pretty confusing, and I recommend you refactor.
I would do it this way, assuming CheckRating returns true if valid:
for (int i = 0; i < filmNames.Length; i = i + 1)
{
bool ratingValid; // Defaults to false
int displayNumber = i + 1;
while (!ratingValid)
{
Console.Write($"> Blablabla: ");
string input = Console.ReadLine();
ratingValid = CheckRating(input);
}
// Do stuff if rating is valid here. If you got here, rating is valid.
}
// edit by iluvpancakes //
I decided to add a comment made by #Sinatr since that was the (version of the) solution I personally ended up using:
if(CheckRating(checkRating))
{
[do stuff and things]
}
bool ret = CheckRating(checkRating); // currently just does "return false;" for testing purposes
if (ret)
{
[...]
}
Or, like Sinatr's comment:
if(CheckRating(checkRating))
{
[...]
}
I have a battleship like terminal game, the user enters a coordinate like e2, and the program checks one of the instance variables of my object Box, it checks whether hasShip is true, if its true then it will make the coordinate e2 false, and give the output "Ship destroyed"
The problem is that all my objects are called a1,a2,a3,a4,a5,b1,b2 and so on.
I have created 25 instances of the Box class. All names as such.
Once the program gets input, either e4 ,e5 etc. I want to convert that string into an object.
For example( I want to do something like this )
target = Console.ReadLine();
target.hasShip == true;
I want to convert target into an object, then use target to use the methods of the Box class.
Because the other approach requires me to make loads of if statements, which isn't clean code, doesn't look good, and is a waste if you ask me.
Thanks in advance,
New Answer: use an Array
I am slow. I did not pay attention that you are making a battleship-like game, and that we know that the "boxes" make a rectangle. We can store this efficiently in an array.
Why I did not catch up to this fact earlier? I guess I need to wake up properly.
So, use an array:
var board = new Box[5, 5];
Now, to populate it, we can do a double for loop:
for(var indexRow = 0; indexRow < 5; indexRow++)
{
for(var indexCol = 0; indexCol < 5; indexCol++)
{
board[indexRow, indexCol] = new Box();
}
}
Note: pay attention that the indexes go from 0 to 4. For a total of 5 values: {0, 1, 2, 3, 5}.
And to query from it, we will need the indexes...
Addendum on populating the array
In comments, OP has said that each Box has an id and the ship positions are picked at random.
We can give the id in the loop:
for(var indexRow = 0; indexRow < 5; indexRow++)
{
for(var indexCol = 0; indexCol < 5; indexCol++)
{
var box = new Box();
box.vhID = (((char)(((int)'a') + indexRow))).ToString() + ((char)(((int)'1') + indexCol)).ToString();
board[indexRow, indexCol] = box;
}
}
What I am doing here is constructing the id from the indexes. Basically taking the value of 'a' and adding the indexRow will give us 'a' when indexRow is 0, 'b' when it is 1 and so on. Similarly, we get the digit that represents the column.
Note: We convert the char to int, do the addition, then convert back to char... and then from char to string. Once we have string, we can concatenate them.
I do not think we need this id. But, hey, you can do it like this.
OP also mentions that he will pick 4 ship positions at random. Fair enough:
var random = new Random();
for (var ships = 0; ships < 4; ships++)
{
board[random.Next(0, 4), random.Next(0, 4)].hasShip = true;
}
Since the user inputs an string, I suggest to create a function to convert it to the index pair:
var input = Console.ReadLine();
if (TryGetCoordinates(input, out int irow, out int icol))
{
var target = board[irow, icol];
}
else
{
Console.WriteLine("The cell {0} does not exist.", input);
}
// ...
bool TryGetCoordinates(string cell, out int indexRow, out int indexCol)
{
// ...
}
Start by validating null:
bool TryGetCoordinates(string cell, out int indexRow, out int indexCol)
{
indexRow = -1;
indexCol = -1;
if (cell == null)
{
return false;
}
// ...
}
Note: Feel free to use Trim, ToUpper or ToUpperInvariant.
We know that must be a letter followed by a digit, we can validate the length:
bool TryGetCoordinates(string cell, out int indexRow, out int indexCol)
{
indexRow = -1;
indexCol = -1;
if (cell == null)
{
return false;
}
if (cell.Length != 2)
{
return false;
}
// ...
}
We extract the characters and from them the coordinates. Noting that the first one is a letter, and the other a digit. We can also validate they are withing bounds.
bool TryGetCoordinates(string cell, out int indexRow, out int indexCol)
{
indexRow = -1;
indexCol = -1;
if (cell == null)
{
return false;
}
if (cell.Length != 2)
{
return false;
}
indexRow = (int)cell[0] - (int)'a';
indexCol = (int)cell[1] - (int)'1';
return indexRow < 5 && indexRow >= 0 && indexCol < 5 && indexCol >= 0;
}
And of course, you can do a loop of the validation similar to what was explained in the old answer.
Note: the issue with value types I describe in the old answer still applies with the array.
Old Answer: Use a Dictionary
I believe you do not want to convert the string to an object (the string is an object by the way), you want to pick the Box object you previously created based on the string. And you want to do it without using if statements. What you need is a dictionary.
So, you would have Dictionary<string, Box> meaning that it is a dictionary that you can query by string and stores Box.
Addendums:
In this case, string is the key type, by which we will access the dictionary. When we add an object to the dictionary we identify it with a key, and when we retrieve it, we also use the key. The key does not have to be string, you can choose a different type. string is convenient in this case because it is what you get from Console.ReadLine().
You can create the dictionary to store whatever type you need. If you do not need Box, you can create a dictionary that stores something else.
Creating and populating the Dictionary
Then, you add to the Dictionary all your Box objects, like this:
var dict = new Dictionary<string, Box>();
// ...
dict.Add("a1", CreateBoxA1());
Where CreateBoxA1 represents whatever means you have to create the object. No, you do not need to create a method for each Box... you can do it like this:
dict.Add("a1", new Box());
Or whatever. I do not know how you create them, so consider that a placeholder, ok? ok.
Querying and retrieving values from the Dictionary
Once you have all your Box instances in your dictionary, you can get the one you need using the string:
Console.WriteLine("Enter the name of the Box:");
var name = Console.ReadLine();
var target = dict[name];
Addendum: The value you get from dict[name] is the value that you added to the dictionary with that key. So, if the user typed "a1" it dict[name] will be the value that we added with "a1" (dict.Add("a1", new Box());). Again, if what you need is not Box you can create a dictionary to store a different type.
Input validation
You can also use the Dictionary to validate if the string corresponds to a Box that exists, for example:
Console.WriteLine("Enter the name of the Box:");
var name = Console.ReadLine();
if (dict.KeyExists(name))
{
var target = dict[name];
// ...
}
else
{
Console.WriteLine("The Box {0} does not exist", name);
}
It goes without saying, but... you can make a loop based on that, for example:
Box target = null;
while(true)
{
Console.WriteLine("Enter the name of the Box:");
var name = Console.ReadLine();
if (dict.KeyExists(name))
{
target = dict[name];
break;
}
Console.WriteLine("The Box {0} does not exist", name);
}
Also, it goes without saying, but... you can add your own validations and sanitation steps. For example using ToUpper, ToUpperInvariant or Trim. And I would remind you that changing strings to lower or upper case is culture sensitive.
See also: Best Practices for Using Strings in .NET.
Editing an removing objects from the dictionary
Once you have the object you retrieved from the Dictionary...
var target = dict[name];
We can use it, and even modify it:
var target = dict[name];
if (target.hasShip) // no need for "== true" if hasShip bool
{
target.hasShip = false;
Console.WriteLine("Ship Destroyed");
}
An special note must be done if Box is value type. For a custom type that means that it is not a class but a struct. The problem with value types is that they are copied on assignment, meaning that when you do var target = dict[name]; with a value type, you get a copy. You must then update the dictionary once you manipulated it:
var target = dict[name];
if (target.hasShip) // no need for "== true" if hasShip bool
{
target.hasShip = false;
dict[name] = target;
Console.WriteLine("Ship Destroyed");
}
Note: As I said above, this is only needed for value types.
And you can even remove the Box from the dictionary if that is necesary:
dict.Remove(name);
public class call
{
DateTime time = DateTime.Now;
public void check()
{
int count=0;
if(time < time.AddSeconds(30))
{
count ++;
if(count == 5)
{
Console.WriteLine(count);
}
}
public class Program
{
public static void Main(string[] args)
{
int input;
string sinput;
call one = new call();
call two = new call();
sinput = Console.ReadLine();
input = int.Parse(sinput);
do{
switch(input)
case 1: one.check();
break;
case 2: two.check();
break;
default: Console.WriteLine(":::");
break;
}while(input > 9)
}
}
}
I am new to programming...
I tried to print the number If it occurred 5 time within 30 sec...
I give input 1 to 9.. if the same input occurred 5 times with in 30 sec I want to print that..
You have a few bugs:
You'll probably want to replace:
if(time < time.AddSeconds(30))
With something the compares to the current time such as:
if(DateTime.Now < time.AddSeconds(30))
You are also incrementing count twice in your check method, not sure if that was intentional.
Inside of your do loop your switch body needs to be inside of {} and you probably should be reading a new input each time or doing something else to change the input or your loop will run forever.
You should also always validate user input. In this case if someone enters something other than a number your application will crash form this code:
sinput = Console.ReadLine();
input = int.Parse(sinput);
Instead lookup the int.TryParse method.
You create something like a class that logs the data for a certain input value (that is, stores the dates at which they are entered) and a class that binds those. Like, in pseudo-code:
class SingleInputLogger {
List<Date> dates
void addDate(Date date){
push date in dates
}
unsigned int currentSize(){
remove all entries from dates which are too old
return size of dates
}
}
class InputLogger {
Array<SingleInputLogger> singleInputLoggers of size 10 (or 9 if only 1..9, but then mind the offset)
//adds an input and also returns true if the input has a count of more than five
void addInput(int input){
singleInputLoggers[input].addDate(currentTime())
}
bool checkInput(int input){
if(singleInputLoggers[input].currentSize() >= 5){
return true
}
return false
}
Then the main routine becomes
InputLogger logger
while(get input){
logger.addInput(input)
if(logger.checkInput(input)){
display message "input " + input + " was entered more than five times in 30s"
}
}
(List was used to indicate a linked list in order to be able to efficiently remove front entries, Array was used to indicate a structure of static size for fast access)
Remember to let classes like this do the job for you. Try to use as less functions as possible and rather go with methods.
If someone has better names for those classes (I admit that my names are not that great), feel free to edit my answer.
So I have this graph with books I'm iterating through and printing them out.
public class Books : IBookFinder
{
private Books(Books next, string book)
{
Next = next;
Book = book;
}
public Books Next { get; }
public string Book { get; }
public Books Previous(string book)
{
return new Books(this, book);
}
public static Books Create(string book)
{
return new Books(null, book);
}
public string FromLeft(Books books, int numberFromLeft)
{
for (int i = 1; i < numberFromRight; i++)
{
books = books?.Next; //Go through the books and return null if books is null.
}
return books.book; //Should probably check for null here as it crashes if the input number is out of book range (something else than 1-4)
}
public string FromRight(Books books, int numberFromRight
{
//How to implement this bad boy?
}
}
All is well and good, but I want to implement a method FromRight so that I can write out the name of the book from it's placement in the graph, given a number input. For example, if inputting "3", it should output "Lord of the Rings". How would I go about doing that? Any hints greatly appreciated.
class Program
{
static void Main(string[] args)
{
var curr = Books
.Create("Harry Potter")
.Previous("Lord of the Rings")
.Previous("Twilight")
.Previous("Da Vinci Code");
while (curr != null)
{
if (curr.Next != null)
{
Console.Write(curr.Book + " --- ");
}
else
{
Console.WriteLine(curr.Book);
}
curr = curr.Next;
}
Console.WriteLine("Input number to pick a book");
var bookNumber = Console.ReadLine();
int n;
if (int.TryParse(bookNumber, out n)) //Checking if the input is a #
{
}
else
{
Console.WriteLine("Input was not a number!");
}
Console.WriteLine(bookNumber);
Console.ReadLine();
}
}
UPDATE:
I've managed to figure out a way to do it, without having to make a doubly linked list, even though that is of course probably the optimal solution for this problem.
I've made a helper function Count(), which takes the list and counts entries:
private int Count(Books books)
{
int count = 1;
while (books.Next != null)
{
books = books.Next;
count++;
}
return count;
}
I then use the return value of this method to select books from the right:
public string FromRight(Books books, int numberFromRight)
{
var bookCount = Count(books); //Getting the amount of books.
for (int i = numberFromRight; i < bookCount; i++)
{
books = boos?.Next;
}
return books.Book;
}
Since you are creating this like a linked list, to go backwards you need to create a doubly linked list: https://en.wikipedia.org/wiki/Doubly_linked_list
Your current code has no representation of the previous node. Set the previous node as that of the current while creating the next, then its iterating is just like you did originally.
Iterate until there is no Next, then print until there is no previous.
A single-linked list can only be iterated in a single direction, so it’s not possible to go backwards.
To get the nth element from the left, you simply need to go next n times. But to figure out elements from the right, you first need to get to the end. So to get the last element, you need to go next as often as possible. To get the next to last element, you need to go to the end and return the element before that.
You may see where this is going: In order to get the nth element from the right, you need to remember the last n elements while iterating through the linked list. This also means that in the worst case, you remember every item when getting the first element from the left—but from the right.
Implementing this is not too difficult, you can do this very easily with a list of length numberFromRight but since you are inserting at the end and removing elements from the beginning you would be shifting elements all the time. So you better use a fixed-length queue. Or you use a array with a variable pointer which avoids having to shift anything. A FromRight method could look like this:
public Element FromRight(int numberFromRight)
{
// increment the offset by one, so that `0` means the last element,
// and `1` means the one before last, etc.
numberFromRight++;
// create an array with `numberFromRight` slots
Element[] arr = new Element[numberFromRight];
// add the current item to the array
arr[0] = this;
// `i` is the index where to add the next element
int i = 1 % numberFromRight;
// iterate through all elements until the very end
Element current = this;
while (current.Next != null)
{
current = current.Next;
// add the current element to the array
arr[i] = current;
// increment the index by one, overflowing back to the beginning of the array
i = (i + 1) % numberFromRight;
}
// the element at position `i` is the nth element from the right.
return arr[i];
}
(I’ve chosen to implement this without your Books type since you mixed Books and Customers in a confusing way and I didn’t get that. This works for arbitrary linked lists.)
If you find yourself accessing elements from the end more regularly, you should consider using a doubly linked list instead which also maintains a pointer to the left element. This means that more references need be be maintained (increasing the complexity for list operations a bit) but results in a way better performance (since iterating from the end is the same as iterating from the beginning).
In general, you should always choose a data structure that fits your access pattern. No data structure is perfect for everything, so it’s important to understand the differences and to know what you really need in your application so you can choose well.
I am trying to use bubble sort to sort a list of items. I am aware that this is not the most effective method of sorting; however, I require this method to work before moving on to better things. My current code partially sorts a list, but I cannot see what I am doing wrong.
public class ListComponents
{
public int Priority;
public string Name;
public ListComponents Next;
}
public void bubblesort()
{
ListComponents Current = Head;
int temp = 0;
bool swapped = true;
while (swapped)
{
Print(); // Debug Function to print each swap
Current = Head.Next; // RESET CURRENT TO HEAD + 1 ON EACH ITERATION?
swapped = false;
for (int sort = 0; sort < (size - 1); sort++) //Size defined as # of items in list
{
if (Current != null &&
Current.Next != null &&
Current.Priority> Current.Next.Priority)
{
temp = Current.Priority;
Current.Priority= Current.Next.Priority;
Current.Next.Priority= temp;
Current = Head; // POTENTIAL ISSUE?
swapped = true;
}
}
}
}
My debug print function shows the following, showing how the values are almost in order:
List: 4 2 1 3 (Inital List Order)
List: 1 4 2 3 (First Swap)
List: 1 2 4 3 (Second Swap)
The issue seems to be with setting the 'Current' value, although I cannot see where this is not working.
My advice: start over. This is a mess.
Here's how I would tackle problems like this when I was a beginner. Start by writing the code in pseduo-code:
void BubbleSort(ListComponent head)
{
if the list is of zero length then it is already sorted, so return
if the list is of length one then it is already sorted, so return
Otherwise, the list is of length two or more. Therefore we know that
a first item exists and a second-last item exists.
Our strategy is: run down the list starting with the first item
and ending with the second-last item. If at any time the current
item and the one following it are out of order, swap their elements.
If we made no swaps then the list is sorted, return.
If we made one or more swaps then the list might not be sorted, so
run down the list again.
}
OK, now we can start translating that into code.
void BubbleSort(ListComponent head)
{
// if the list is of zero length then it is already sorted, so return
if (head == null) return;
// if the list is of length one then it is already sorted, so return
if (head.Next == null) return;
// Otherwise, the list is of length two or more. Therefore we know that
// a first item exists and a second-last item exists.
// Our strategy is: run down the list starting with the first item
// and ending with the second-last item.
for (ListComponent current = head;
current.Next != null;
current = current.Next)
{
If at any time the current item and the one following it
are out of order, swap their elements.
}
If we made no swaps then the list is sorted, return.
If we made one or more swaps then the list might not be sorted, so
run down the list again.
}
OK, I've translated some of that English into code. Can you translate the rest?
Another example with a simple class with 2 properties. This is NOT for arrays but for a simple class simulating pointers... Made just for fun !
class MyLinkedList
{
MyLinkedList nextNode;
int data;
public void OrderListBubbleAlgoritm(ref MyLinkedList head)
{
bool needRestart = true;
MyLinkedList actualNode = head; //node Im working with
int temp;
while (needRestart)
{
needRestart = false;
actualNode = head;
while (!needRestart && actualNode.nextNode != null)
{
if (actualNode.nextNode.data >= actualNode.data) //is sorted
{
actualNode = actualNode.nextNode;
}
else
{
//swap the data
temp = actualNode.data;
actualNode.data = actualNode.nextNode.data;
actualNode.nextNode.data = temp;
needRestart = true;
}
}
}
}
}
Remember to use bubble sorting just with small quantity of data.
It's performance is: O(n^2)