'Advanced' Console Application - c#

I'm not sure if this question has been answered elsewhere and I can't seem to find anything through google that isn't a "Hello World" example... I'm coding in C# .NET 4.0.
I'm trying to develop a console application that will open, display text, and then wait for the user to input commands, where the commands will run particular business logic.
For example: If the user opens the application and types "help", I want to display a number of statements etc etc. I'm not sure how to code the 'event handler' for user input though.
Hopefully this makes sense. Any help would be much appreciated!
Cheers.

You need several steps to achieve this but it shouldn't be that hard. First you need some kind of parser that parses what you write. To read each command just use var command = Console.ReadLine(), and then parse that line. And execute the command... Main logic should have a base looking this (sort of):
public static void Main(string[] args)
{
var exit = false;
while(exit == false)
{
Console.WriteLine();
Console.WriteLine("Enter command (help to display help): ");
var command = Parser.Parse(Console.ReadLine());
exit = command.Execute();
}
}
Sort of, you could probably change that to be more complicated.
The code for the Parser and command is sort of straight forward:
public interface ICommand
{
bool Execute();
}
public class ExitCommand : ICommand
{
public bool Execute()
{
return true;
}
}
public static Class Parser
{
public static ICommand Parse(string commandString) {
// Parse your string and create Command object
var commandParts = commandString.Split(' ').ToList();
var commandName = commandParts[0];
var args = commandParts.Skip(1).ToList(); // the arguments is after the command
switch(commandName)
{
// Create command based on CommandName (and maybe arguments)
case "exit": return new ExitCommand();
.
.
.
.
}
}
}

I know this is an old question, but I was searching for an answer too. I was unable to find a simple one though, so I built InteractivePrompt. It's available as a NuGet Package and you can easily extend the code which is on GitHub. It features a history for the current session also.
The functionality in the question could be implemented this way with InteractivePrompt:
static string Help(string strCmd)
{
// ... logic
return "Help text";
}
static string OtherMethod(string strCmd)
{
// ... more logic
return "Other method";
}
static void Main(string[] args)
{
var prompt = "> ";
var startupMsg = "BizLogic Interpreter";
InteractivePrompt.Run(
((strCmd, listCmd) =>
{
string result;
switch (strCmd.ToLower())
{
case "help":
result = Help(strCmd);
break;
case "othermethod":
result = OtherMethod(strCmd);
break;
default:
result = "I'm sorry, I don't recognize that command.";
break;
}
return result + Environment.NewLine;
}), prompt, startupMsg);
}

This is easy enough, just use the Console.WriteLine and Console.ReadLine() methods. From the ReadLine you get a string. You could have a horrible if statement that validate this against known/expected inputs. Better would be to have a lookup table. Most sophisticated would be to write a parser. It really depends on how complex the inputs can be.

Console.WriteLine Console.ReadLine and Console.ReadKey are your friends. ReadLine and ReadKey waits for user input. The string[] args will have all of your parameters such as 'help' in them. The array is created by separating the command line arguments by spaces.

switch (Console.ReadLine())
{
case "Help":
// print help
break;
case "Other Command":
// do other command
break;
// etc.
default:
Console.WriteLine("Bad Command");
break;
}
If you're looking to parse commands that have other things in them like parameters, for example "manipulate file.txt" then this alone won't work. But you could for example use String.Split to separate the input into a command and arguments.

A sample:
static void Main(string[] args)
{
Console.WriteLine("Welcome to test console app, type help to get some help!");
while (true)
{
string input = Console.ReadLine();
int commandEndIndex = input.IndexOf(' ');
string command = string.Empty;
string commandParameters = string.Empty;
if (commandEndIndex > -1)
{
command = input.Substring(0, commandEndIndex);
commandParameters = input.Substring(commandEndIndex + 1, input.Length - commandEndIndex - 1);
}
else
{
command = input;
}
command = command.ToUpper();
switch (command)
{
case "EXIT":
{
return;
}
case "HELP":
{
Console.WriteLine("- enter EXIT to exit this application");
Console.WriteLine("- enter CLS to clear the screen");
Console.WriteLine("- enter FORECOLOR value to change text fore color (sample: FORECOLOR Red) ");
Console.WriteLine("- enter BACKCOLOR value to change text back color (sample: FORECOLOR Green) ");
break;
}
case "CLS":
{
Console.Clear();
break;
}
case "FORECOLOR":
{
try
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), commandParameters);
}
catch
{
Console.WriteLine("!!! Parameter not valid");
}
break;
}
case "BACKCOLOR":
{
try
{
Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), commandParameters);
}
catch
{
Console.WriteLine("!!! Parameter not valid");
}
break;
}
default:
{
Console.WriteLine("!!! Bad command");
break;
}
}
}
}

This is very simplistic, but might meet your needs.
// somewhere to store the input
string userInput="";
// loop until the exit command comes in.
while (userInput != "exit")
{
// display a prompt
Console.Write("> ");
// get the input
userInput = Console.ReadLine().ToLower();
// Branch based on the input
switch (userInput)
{
case "exit":
break;
case "help":
{
DisplayHelp();
break;
}
case "option1":
{
DoOption1();
break;
}
// Give the user every opportunity to invoke your help system :)
default:
{
Console.WriteLine ("\"{0}\" is not a recognized command. Type \"help\" for options.", userInput);
break;
}
}
}

There is a C# nuget package called 'ReadLine' by 'tornerdo'. The statement ReadLine.Read(" prompt > "); prompts the user within options provided in CustomAutoCompletionHandler.PossibleAutoCompleteValues.
Additionally, you can change the CustomAutoCompletionHandler.PossibleAutoCompleteValues for each prompt. This ensures that the user get to choose an option from available\ supported list of options. Less error prone.
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(" Note! When it prompts, press <tab> to get the choices. Additionally, you can use type ahead search.");
Console.ForegroundColor = ConsoleColor.White;
// Register auto completion handler..
ReadLine.AutoCompletionHandler = new CustomAutoCompletionHandler();
CustomAutoCompletionHandler.PossibleAutoCompleteValues = new List<string> { "dev", "qa", "stg", "prd" };
var env = CoverReadLine(ReadLine.Read(" Environment > "));
Console.WriteLine($"Environment: {env}");
}
private static string CoverReadLine(string lineRead) => CustomAutoCompletionHandler.PossibleAutoCompleteValues.Any(x => x == lineRead) ? lineRead : throw new Exception($"InvalidChoice. Reason: No such option, '{lineRead}'");
public class CustomAutoCompletionHandler : IAutoCompleteHandler
{
public static List<string> PossibleAutoCompleteValues = new List<string> { };
// characters to start completion from
public char[] Separators { get; set; } = new char[] { ' ', '.', '/' };
// text - The current text entered in the console
// index - The index of the terminal cursor within {text}
public string[] GetSuggestions(string userText, int index)
{
var possibleValues = PossibleAutoCompleteValues.Where(x => x.StartsWith(userText, StringComparison.InvariantCultureIgnoreCase)).ToList();
if (!possibleValues.Any()) possibleValues.Add("InvalidChoice");
return possibleValues.ToArray();
}
}

Related

How do you execute a method only if another one has been run?

I have a choice for the user in Main using a switch. Depending on what the user chooses, several choices later, the program will either end or not. At least, that is what I'm trying to accomplish.
//This is in Main
string[] menyVal2 = new string[] {"Go to the hotel room", "Go to the dining hall"};
string title2 = "text";
int choice2 = menu(menuChoice2, title2);
switch (choice2)
{
case 0:
hotelRoom();
break;
case 1:
diningHall();
break;
}
Many lines of code later...
public static void save()
{
Console.Clear();
string[] menuChoice = {"Chapter 2", "Back to start"};
string title = "text";
int choice = menu(menuChoice, title);
switch (val)
{
case 0:
if (hotelRoom = true) //this if-statement does not work
{
withoutCross();
}
else if (diningHall = true)
{
withCross();
}
break;
case 1:
Main(new string[] { });
break;
}
}
When I understand your title of the question correctly, then this is a solution:
Make the return type of the method bool and fill it to a variable.
bool isMethodExecuted = MyMethod();
Later you can check with a if-Statement, if the method is executed:
if(isMethodExecuted)
MyOtherMethod();

How can I escape from this loop?

I am a beginner at coding - I just started a week ago. I am trying to develop a program where the user can either see all of the names in a list or add a name to a list. When the user prompts to see the names in the list, and then decides to take another action, they are stuck in an endless loop where they can only see the names again. I want them to be able to go back to the start and choose to either see the names or add a name.
I've tried reorganizing and looking things up, but I'm still at a loss.
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<string> names = new List<string>();
names.Add("Vasti");
names.Add("Cameron");
names.Add("Ezra");
names.Add("Tilly");
bool program = false;
bool program2 = false;
Console.WriteLine("Welcome to the names lost! If you wish to add
a name to the list, type 1. If you want to see current names in
the list,
type 2.");
string userChoice = Console.ReadLine();
do
{
switch (userChoice)
{
case "1":
Console.WriteLine("Add a name to the squad.");
string userAddName = Console.ReadLine();
names.Add(userAddName);
break;
case "2":
Console.WriteLine("Here's the list:");
foreach (string name in names)
{
Console.WriteLine(name);
}
Console.WriteLine("Wanna do that again? Type yes or
no.");
do
{
string userContinue = Console.ReadLine();
switch (userContinue)
{
case "yes":
program = true;
program2 = false;
break;
case "Yes":
program = true;
program2 = false;
break;
case "no":
program = false;
program2 = false;
break;
case "No":
program = false;
program2 = false;
break;
default:
Console.WriteLine("Please enter either
yes or no");
userContinue = Console.ReadLine();
program2 = true;
break;
}
}
while (program2 == true);
break;
default:
Console.WriteLine("Please type either 1 or 2 to
select an option.");
break;
}
}
while (program == true);
}
}
I expect the user to return to the beginning prompt, but they are stuck in the same prompt over and over. There are no error messages.
Two major things to cover your difficulties.
First, the input hint should be in the loop so it can show when each loop begin.
Second, do while loop decide if the loop should continue at the end and in your design; it depends on what user input which should be directly utilized as while condition.
Therefore, your code could be simplified as
public static void Main()
{
List<string> names = new List<string>() { "Vasti", "Cameron", "Ezra", "Tilly" };
string userChoice = "";
do
{
Console.WriteLine($#"Welcome to the names lost!{Environment.NewLine} If you wish to add a name to the list, type 1.{Environment.NewLine} If you want to see current names in the list, type 2.");
userChoice = Console.ReadLine();
switch (userChoice)
{
case "1":
Console.WriteLine("Add a name to the squad.");
string userAddName = Console.ReadLine();
names.Add(userAddName);
break;
case "2":
Console.WriteLine("Here's the list:");
foreach (string name in names)
{
Console.WriteLine(name);
}
break;
default:
Console.WriteLine("Please type either 1 or 2 to select an option.");
break;
}
Console.WriteLine(#"Wanna do that again? Type yes or no.");
userChoice = Console.ReadLine();
}
while (userChoice.Equals("yes", StringComparison.OrdinalIgnoreCase));
Console.WriteLine("Program finished");
Console.ReadLine();
}
Change the do{}while() loops to regular while(){} loops.Move string userChoice = Cosole.ReadLine(); inside of the top while loop or you will never ask for other options. . . Also set the ((bool program=true; and bool program=true;)) at the beginning. Do{}while() loops do everything inside the {} first then check to see if the while statement is still true.

Is it correct to make an object for Console.Readlines input?

i just started learning C# and all these Classes, Objects and Methods still confuse me. So i came up with this piece of code, its working fine but i got the feeling i do things that i shouldnt do. Can some C# experts have a look over it?
Is this a good way to take Input and then work with it?
static void Main(string[] args)
{
input crInput = new input();
Console.WriteLine(crInput.evalInput(Console.ReadLine()));
}
private class input
{
public string InputString { get; set; }
public string evalInput(string _input)
{
string result = "";
string input = _input;
if (input == "1")
{
result = "1";
}
else if (input == "2")
{
result = "2";
}
else if (input == "3")
{
result = "3";
}
else
{
result = "NOTHING";
}
return result;
}
}
As I see in your code, several details can be fixed:
First, you can simplify the CRInput assignment by substituting the name of the class for the keyword var: var CRInput = new Input(); in C# var supports almost all types of data.
Another thing to take into account is that the names of the classes should start with Capital, this is the correct way.
Another thing is that you do not have to create another string input if you already have one previously.
The switch keyword simplifies the repetitive use of else if you can see, and it is recommended that each sentence have its own line.
static void Main(string[] args)
{
//var supports almost all types of data, which simplifies the object declaration process
var CRInput = new Input();
Console.WriteLine(CRInput.EvalInput(Console.ReadLine()));
}
//Class names must start with capitals
private class Input
{
public string InputString { get; set; }
public string EvalInput(string input)
{
//Initialize result with the value -> ""
string result = string.Empty;
//The switch structure simplifies and shortens the repetitive use of else if
switch (input)
{
//if
case "1":
result = "1";
break;
//else if
case "2":
result = "2";
break;
//else if
case "3":
result = "3";
break;
//else
default:
result = "NOTHING";
break;
}
return result;
}
}

Console application, How do I make Console.ReadLine() IF/ELSE Statements? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I was making a tool named "Ferna".. And I was trying to make different commands on it, but the problem is that these "commands" have to be in order, like when I try to execute a command, I have to execute them in order.. But I want it like it doesn't have to be in order so if i write "cyan" it executes it, it doesn't have to wait for the 5th ReadLine()
Here's my code:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
if (Console.ReadLine() == "cmds")
{
Console.WriteLine(cmds);
}
else if (Console.ReadLine() == "calculator")
{
cal.ShowDialog();
}
else if (Console.ReadLine() == "cyan")
{
Console.ForegroundColor = ConsoleColor.Cyan;
}
else if (Console.ReadLine() == "black")
{
Console.ForegroundColor = ConsoleColor.Black;
}
else if (Console.ReadLine() == "clear")
{
Console.Clear();
}
}
}
}
Store the results of your Console.ReadLine statement in a variable. This way you don't have to keep calling Console.ReadLine.
var cmd = Console.ReadLine();
if (cmd == "cmds")
{
Console.WriteLine(cmds);
}
else if (cmd == "calculator")
{
...
The issue is essentially every time the if checks each condition its going to wait for more input
Update :
You will need to put it in a loop
string cmd = "";
while(cmd != "exit")
{
cmd = Console.ReadLine();
if (cmd == "cmds")
{
Console.WriteLine(cmds);
}
else if (cmd == "calculator")
{
...
}
Here is the good old switch statement. Gotta break it out every once in while! If/Else works just as well though. This will loop and check for the next line.
class Program
{
static void Main(string[] args)
{
while((string command = Console.ReadLine()) != null)
{
switch (command.ToUpper())
{
case "CMD":
Console.WriteLine("CMD");
break;
case "CALCULATOR":
cal.ShowDialog();
break;
default:
Console.WriteLine("Default");
break;
}
}
}
}
Using Queue<string> to keep a list of commands might not be a bad idea:
class Program
{
static Queue<string> commandQueue = new Queue<string>(new[] {"FirstCommand", "SecondCommand", "ThirdCommand"});
static void Main(string[] args)
{
using (Queue<string>.Enumerator enumerator = commandQueue.GetEnumerator())
{
while (enumerator.MoveNext())
{
Console.WriteLine("Type in next command or type exit to close application");
//this while is used to let user make as many mistakes as he/she wants.
while (true)
{
string command = Console.ReadLine();
if (command == "exit")
{
Environment.Exit(0);
}
//if typed command equals current command in queue ExecuteCommand and move queue to next one
if (command == enumerator.Current)
{
ExecuteCommand(command);
break;
}
else//Show error message
{
if (commandQueue.Contains(command))
{
Console.WriteLine("Wrong command.");
}
else
{
Console.WriteLine("Command not found.");
}
}
}
}
Console.WriteLine("We are done here, press enter to exit.");
Console.ReadLine();
}
}
//Method that executes command according to its name
static void ExecuteCommand(string commandName)
{
switch (commandName)
{
case "FirstCommand":
Console.WriteLine("FirstCommand executed");
break;
case "SecondCommand":
Console.WriteLine("SecondCommand executed");
break;
case "ThirdCommand":
Console.WriteLine("ThirdCommand executed");
break;
default:
Console.WriteLine("Command not found");
break;
}
}
}
Also if the requirement states that actions should performed in exact order it might not be a bad idea to just let the user press Enter to execute the next command instead of having to type its name.

Error in Switch case and not executing the program

The error is "Control cannot fall through from one case label ('case "B":')"
I am trying to do the following:
Upon starting the application a user will be prompted to login. The first action is to authenticate the user by validating an account exists for the entered username. Once the username has been matched to an account let the account object validate the entered pin (see Account class).
o After authentication, display a welcome message to the customer logged in using their name and display a menu offering options to get balance, deposit to account, withdraw from account, modify customer information, display current transactions, and exit.
static void Main(string[] args)
{
Account myCustAcc = new Account();
Transaction myCustTrans = new Transaction();
string input, choice = "";
string adminName, userName= "";
int adminPin, userPin;
//Login
Console.WriteLine("\t\t*****************WELCOME TO BANKING APPLICATION*********************\n");
choice = Console.Readline();
switch (choice)
{
case "A":
choice = "Admin";
Console.Write("\nAdminName :");
adminName = Console.ReadLine();
Console.Write("AdminName :");
Console.Write("AdminPIN :");
adminPin = Convert.ToInt32(Console.ReadLine());
//IT IS DEFINE USERNAME AND PASSWORD
if (adminName.Equals("admn1") && adminPin.Equals("9999"))
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Welcome to Bank");
}
break;
case "B":
{
choice = "User";
Console.Write("\nUserName :");
userName = Console.ReadLine();
Console.Write("UserName :");
Console.Write("PIN :");
userPin = Convert.ToInt32(Console.ReadLine());
//IT IS DEFINE USERNAME AND PASSWORD
if (userName.Equals("SMD") && userPin.Equals("1212"))
{
Console.ForegroundColor = ConsoleColor.White;
//Welcome Message with Name
Console.WriteLine("\t\t\t Welcome to Banking Application",userName);
Console.WriteLine("\n<<<Please Select Following Menus>>>");
do
{
//With Menu option to get balance, deposit/withdraw from account, modify customer information display current balance and exit
Console.WriteLine("\t1> GetBalance");
Console.WriteLine("\t2> Deposit");
Console.WriteLine("\t3> Withdraw");
Console.WriteLine("\t4> Modify");
Console.WriteLine("\t5> Display");
Console.WriteLine("\t6> Exit");
input = Console.ReadLine();
switch (input)
{
case "1":
break;
case "2":
break;
case "3":
break;
case "4":
break;
case "5":
break;
case "6":
default: Console.WriteLine("Exit the Application!!!");
break;
}
} while (input != "6");
}
break;
}
}
//Pause Display
Console.WriteLine("Press Any key to continue...........");
Console.ReadLine();
}
}
}
this is my account class
class Account
{
//Declare Instance Variables
private string customerFirstName;
private string customerLastName;
private string customerAddress;
private string customerState;
private int customerZip;
private double customerBalance;
//Class Variables
private static string customerUserName;
private static int customerPin;
//Retrieve Customer First Name
public string getCustomerFirstName()
{
return customerFirstName;
}
//Set Customer Name
public void setCustomerFirstName(String newFirstName)
{
customerFirstName = newFirstName;
}
//Retrieve Customer Last Name
public string getCustomerLastName()
{
return customerLastName;
}
//Set Customer Last Name
public void setCustomerLastName(String newLastName)
{
customerLastName = newLastName;
}
//Retrieve Customer Address
public string getCustomerAddress()
{
return customerAddress;
}
//Set Customer Address
public void setCustomerAddress(string newAddress)
{
customerAddress = newAddress;
}
//Retrieve Customer State
public string getCustomerState()
{
return customerState;
}
//Set Customer State
public void setCustomerZip(string newState)
{
customerState = newState;
}
//Retrieve Customer Zip
public int getCustomerZip()
{
return customerZip;
}
//Set Customer Zip
public void setCustomerZip(int newZip)
{
customerZip = newZip;
}
}
You need to move the break outside of the if statement. Also I recommend you to use brackes for case statements as they will make the intent clear(where statement block starts and ends).
Your code have too many execution path inside the single method, which makes code hard to read and hard to maintain. Refactor your code to methods which does only one thing, so that it will be easy to maintain.
Your main switch statement is using the choice variable which you are setting to an empty string and then never assigning. It looks like you meant to include a Console.Read after the initial message but forgot it.
The window flickers because 'choice' variable is not initialized to "A" or "B":
string input, choice = ""; //<----- here
Therefore there is no match for the switch block and the program quits.
Unreachable code warning is issued because you put some lines after the break command.
switch (choice)
{
case "A":
...
break;
case "B":
{
...
break; //<--- quits case "B", but the code below belongs to case "B"
}
//Pause Display
Console.WriteLine("Press the Enter key to Exit"); // <--- here is still considered part of case "B"
Console.ReadLine();
// case "B" ends here
} // end of outer switch block.
You may want to consider something like this:
...
Console.WriteLine("\t\t*****************WELCOME TO BANKING APPLICATION*********************\n");
Console.WriteLine("Choose A or B");
choice = Console.ReadLine();
switch (choice)
{
case "A":
...
break;
case "B":
...
break;
} // end of outer switch block.
//Pause Display
Console.WriteLine("Press the Enter key to Exit");
Console.ReadLine();

Categories