How to find and delete duplicates in an user input in c# - c#

I am trying to find duplicates from user inputs. I am trying to add customers with their first name, last name, and phone number. If all three of the information already exists, I want to throw an appropriate message saying {firstname}, {lastname}, and {phonenumber} already exists. Any idea how to do it?
//this code is on functions class.
public bool addCustomer(params)
{
if (numCustomer < maxCustomer)
{
Random rnd = new Random();
int id = rnd.Next(10000, 99999);
clist[numCustomer] = new Customer(params);
numCustomer++;
return true;
}
return false;
}
//Menu class to prompts user to choose an option
switch (input)
{
case 1: // Add Customer
Console.Clear();
string fName;
string lName;
long phone;
Console.WriteLine("Adding new customer...\nPlease enter the following:");
Console.Write("First Name: ");
fName = Console.ReadLine();
Console.Write("Last Name: ");
lName = Console.ReadLine();
Console.Write("Phone Number (no dashes or spaces): ");
phone = Convert.ToInt64(Console.ReadLine());
m.addCustomer(fName, lName, phone);
Console.WriteLine("\nPress any key to go back to the Customer Menu...");
Console.ReadKey();
customerMenu();
break;
...
}

Related

My Dictionary Isn't Saving The Value In My Program

I want to create a program where I can save cars based of their models, types and license plates. I try to save all of this information inside a dictionary but they aren't saved as to when I search for them
through the 2nd option ("2. Search Vehicle") they aren't found. Here is my code:
What should I do in order for my code to work? I want to be able to search for the cars through their license plates (Swedish license plates, ABC123) but I can't figure out why the dictionary values aren't saved which I enter through my ReadLines()
Dictionary<string, string> newVehicle = new Dictionary<string, string>();
bool sunfleetAdminTools = true;
Dictionary<string, string> newVehicle = new Dictionary<string, string>();
do
{
WriteLine("1. Add Vehicle");
WriteLine("2. Search Vehicle");
WriteLine("3. Log Out");
ConsoleKeyInfo keyInput = ReadKey(true);
Clear();
switch (keyInput.Key)
{
case ConsoleKey.D1:
Clear();
bool invalidCarCredentials = true;
do
{
Write("Registration Number: ");
string regNumber = ReadLine();
newVehicle["Registration Number"] = regNumber;
Write("Brand: ");
string brand = ReadLine();
newVehicle["Brand"] = brand;
Write("Model: ");
string vehicleModel = ReadLine();
newVehicle["Model: "] = vehicleModel;
Write("Type (Sedan, Compact, Subcompact): ");
string vehicleType = ReadLine();
newVehicle["Type: "] = vehicleType;
Write("Autopilot (Yes, No): ");
string autoPilot = ReadLine();
newVehicle["Autopilot: "] = autoPilot;
Clear();
foreach (KeyValuePair<string, string> kvp in newVehicle)
{
WriteLine("Car {0}: {1}", kvp.Key, kvp.Value);
}
WriteLine("Is this correct? (Y)es (N)o");
ConsoleKeyInfo newInput = ReadKey(true);
if (newInput.Key == ConsoleKey.Y)
{
Clear();
WriteLine("Vehicle registered.");
Thread.Sleep(2000);
Clear();
break;
}
else if (newInput.Key == ConsoleKey.N)
{
Clear();
}
} while (invalidCarCredentials);
break;
case ConsoleKey.D2:
Write("Search for vehicle by license plate: ");
string searchingForVehicle = ReadLine();
if (newVehicle.ContainsValue(searchingForVehicle))
{
WriteLine("Value found.");
foreach (KeyValuePair<string, string> kvp in newVehicle)
{
WriteLine("Car {0}: {1}", kvp.Key, kvp.Value);
}
}
else
{
WriteLine("Car not found.");
Thread.Sleep(2000);
}
Clear();
break;
case ConsoleKey.D3:
Clear();
WriteLine("Logging out...");
Thread.Sleep(1000);
sunfleetAdminTools = false;
Environment.Exit(0);
break;
}
} while (sunfleetAdminTools);
but they aren't saved
You stepped into an interesting trap of dictionary syntactic suggar. It has to do with the way you try to add values to the dictionary:
newVehicle["Registration Number"] = regNumber;
If the dictionary is empty, then the key "Registration Number" will be created and saved with regNumber as key value pair. On the other hand if this key already exists, then the value on this key will be simply overwritten!
This is why your problem arises and you get the impression that the values are not saved. You should always be able to find the last values that you have entered.
If you would have used the oldscool way of adding values:
newVehicle.Add("Registration Number", regNumber);
this would have led to an exception that the key already exists. You would have immideately realised that this cannot work.
What should I do in order for my code to work?
You should addapt an object oriented approach and encapsulate all information that belong together in one class:
public class VehicleRepresentation
{
public string RegistrationNumber { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public string Type { get; set; }
public bool AutoPilot { get; set; }
}
Now you can use a List<VehicleRepresentation> allVehicles to collect all your cars.
VehicleRepresentation vehicle = new VehicleRepresentation();
Console.Write("Registration Number: ");
vehicle.RegistrationNumber = Console.ReadLine();
Console.Write("Brand: ");
vehicle.Brand = Console.ReadLine();
Console.Write("Model: ");
vehicle.Model = Console.ReadLine();
Console.Write("Type (Sedan, Compact, Subcompact): ");
vehicle.Type = Console.ReadLine();
Console.Write("Autopilot (Yes, No): ");
vehicle.AutoPilot = Console.ReadLine() == "Yes";
Console.Clear();
allVehicles.Add(vehicle);
Edit:
Addition by WSC:
typically you'd want to create an enum for vehicle.Type rather than a list of user entered strings. Getting that from user input might be a bit cumbersome in a console environment, however.
Search:
The searching can be implemented in different ways. If you want to keep your loop
foreach(var veh in allVehicles)
{
if(veh.RegistrationNumber == searchingForVehicle)
{
WriteLine("Value found.");
// write here all the information you want to display.
}
}
You can of course use also LINQ for filtering. But that you can figure out on your own I guess.

Is there another alternate way to add Console.WriteLine() in this scenario

In this code, I wish to add a Console.Writeline() after each Console.ReadLine() but how I am implementing the code it is giving me an error. Is there another way how I can add Console.WriteLine() in the instance?
public void CreateAccount()
{
Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Console.WriteLine("Create an Account");
Client createAccount = new Client("Create")
{
NameOfUser = Console.ReadLine(),
SurnameOfUser = Console.ReadLine(),
UserID = Console.ReadLine(),
UserEmail = Console.ReadLine(),
UserHomeAdd = Console.ReadLine(),
UserMobileNumber = int.Parse(Console.ReadLine()),
UsernameField = Console.ReadLine(),
PasswordField = Console.ReadLine(),
CoffePoints = int.Parse(Console.ReadLine())
};
List<Client> accountData = new List<Client>()
{
createAccount
};
You could create a method that prints something and returns Console.ReadLine(), for example:
private static string ReadLine(string writeMessage, bool parseAsInt = false)
{
Console.WriteLine(writeMessage);
var line = Console.ReadLine();
if (parseAsInt)
{
int parseInt = 0;
int.TryParse(line, out parseInt);
line = parseInt.ToString();
}
return line;
}
Then just call it when creating the object:
Client createAccount = new Client("Create")
{
NameOfUser = ReadLine("What's your name?"),
SurnameOfUser = ReadLine("Input your surname"),
[...]
CoffePoints = ReadLine("Coffe points?", true)
};
You can't put WriteLine() between your ReadLine(), because you're initializing properties of your new Client. You can, however, do it like this instead:
public void CreateAccount()
{
Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Console.WriteLine("Create an Account");
Client createAccount = new Client("Create");
Console.WriteLine("Enter NameOfUser ");
createAccount.NameOfUser = Console.ReadLine();
Console.WriteLine("Enter SurnameOfUser ");
createAccount.SurnameOfUser = Console.ReadLine();
Console.WriteLine("Enter UserID ");
createAccount.UserID = Console.ReadLine();
Console.WriteLine("Enter UserEmail ");
createAccount.UserEmail = Console.ReadLine();
Console.WriteLine("Enter UserHomeAdd ");
createAccount.UserHomeAdd = Console.ReadLine();
Console.WriteLine("Enter UserMobileNumber ");
createAccount.UserMobileNumber = int.Parse(Console.ReadLine());
Console.WriteLine("Enter UsernameField ");
createAccount.UsernameField = Console.ReadLine();
Console.WriteLine("Enter PasswordField ");
createAccount.PasswordField = Console.ReadLine();
Console.WriteLine("Enter CoffePoints ");
createAccount.CoffePoints = int.Parse(Console.ReadLine());
List<Client> accountData = new List<Client>()
{
createAccount
};
When you appreciate why this works, I'd recommend to do like Isma suggested (if you've been taught about how to make your own methods yet), to make your code cleaner. I wrote this to help you appreciate why what you wrote wasn't working out. Shorthand property initializers like this:
Something s = new Something(){
Property1 = ReadLine(), //no semicolon here, this is all
Property2 = ReadLine() //one line of code in a=1,b=2,c=3 pattern
};
Cannot have multiple lines of code like this:
Something s = new Something(){
Property1 = WriteLine("Blah"); ReadLine(); //can't put a semicolon here
Property2 = WriteLine("Blah"); ReadLine(); //it HAS to be a comma, because it
Property3 = WriteLine("Blah"); ReadLine(); //HAS to be a single line of code
};
Remember that it is not the return key that defines a new line of code in C#, it's the semicolon. It's simply a language rule that the pattern for setting properties like this is single line, and only one statement can appear on the right hand side of the =
You must either not use the shorthand way (as above) or you must put all the multiple lines of code you want to use into a single method, and then call that method (as Isma suggested)
I'd also like to point out that you said you wanted to "writeline a message after every readline" - note that your program will wait for the user to input anything before it prints your message. Isma's way (and this above) print a message BEFORE asking for a readline, because this is more typically what you'd want to do.
If you really do want it after, then move them to be after (but I guess really you can only be thanking them for their input, and overly thankful things are annoying...) so something like this (Isma's way):
private static string ReadLine(string writeMessage)
{
string s = Console.ReadLine();
Console.WriteLine(writeMessage);
return s;
}
or my way:
public void CreateAccount()
{
Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Console.WriteLine("Create an Account");
Client createAccount = new Client("Create");
createAccount.NameOfUser = Console.ReadLine();
Console.WriteLine("Thanks for entering NameOfUser..");

Empty String Input Validation

I've been having trouble on understanding as of why my custom empty string validation method does not work compared when I check for an empty string directly
Validation.EmptyValidation(title,
"Please, do not leave the course title field empty!" +
"\r\nEnter the course title: ");
It does not output the course title in the end, but when I do it this way it does:
while (string.IsNullOrEmpty(title))
{
Console.WriteLine("No empty string: ");
title = Console.ReadLine();
}
Class:
Console.WriteLine("* Create Course *\r\n");
Console.WriteLine("Enter the course title: ");
string title = Console.ReadLine();
while (string.IsNullOrEmpty(title))
{
Console.WriteLine("No empty string: ");
title = Console.ReadLine();
}
Validation.EmptyValidation(title,
"Please, do not leave the course title field empty!" +
"\r\nEnter the course title: ");
Console.WriteLine("\r\nEnter the course description: ");
string description = Console.ReadLine();
Validation.EmptyValidation(description,
"Please, do not leave the course description field empty!" +
"\r\nEnter the course description: ");
Console.WriteLine("\r\nEnter the number of students in the course: ");
=string studentsInput = Console.ReadLine();
int.TryParse(studentsInput, out int students);
CreateCourse(currentCourse, title, description, students);
public static Course CreateCourse (Course _currentCourse, string title string description, int students)
{
Course course = new Course(title, description, students);
_currentCourse = course;
_currentCourse.Title = course.Title;
Console.WriteLine($"\r\nThank you for registering the {_currentCourse.Title} course.\r\n" +
$"\r\nCourse Information" +
$"\r\nTitle: {_currentCourse.Title}" +
$"\r\nDescription: {_currentCourse.Description}" +
$"\r\nStudents: {_currentCourse.Capacity}");
return _currentCourse;
}
Empty Validation Method:
public static string EmptyValidation(string input, string prompt)
{
while (string.IsNullOrEmpty(input))
{
Console.WriteLine(prompt);
input = Console.ReadLine();
}
return input;
}
There is a couple of things going wrong here
// you weren't returning the results
title = Validation.EmptyValidation(title,
"Please, do not leave the course title field empty!" +
"\r\nEnter the course title: ");
Also if you don't need the other validation anymore you are best to remove it
//while (string.IsNullOrEmpty(title))
//{
// Console.WriteLine("No empty string: ");
// title = Console.ReadLine();
// }

how get customer first name and last name

I'm wondering how to change this code to get first name and last name. My friend and I develop this code but I need to alter this code. There is Customer class as well where I set their properties. So looking for suggestion to alter this:
//Getting No: Of Customers for user wish to enter data.
do
{
needToGetInputFromUser = false;
Console.WriteLine("Please enter customer name");
customerName = Console.ReadLine();
if (customerName.Length < 5 || customerName.Length > 20)
{
Console.WriteLine("Invalid name length, must be between 5 and 20 characters");
Console.WriteLine("Please try again.");
Console.WriteLine(" ");
needToGetInputFromUser = true;
}
else
{
isUserEnteredValidInputData = true;
}
} while (needToGetInputFromUser);
//Getting Account number
My approach is slightly different from everyone else's, Where I ask for first name first, then last name. Here is the Customer class:
class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return FirstName + " " + LastName; } }
}
Basically I create a Customer object then set the FirstName and LastName based on user input individually like so:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var customer = new Customer();
customer.FirstName = GetStringValueFromConsole("Customer First Name");
customer.LastName = GetStringValueFromConsole("Customer Last Name");
Console.WriteLine("New Customers name: " + customer.FullName);
Console.WriteLine("Finished");
Console.ReadLine();
}
private static string GetStringValueFromConsole(string valueToAskFor)
{
var needToGetInputFromUser = false;
var stringValue = string.Empty;
do
{
Console.WriteLine("Please enter " + valueToAskFor);
stringValue = Console.ReadLine();
if (stringValue.Length < 5 || stringValue.Length > 20)
{
Console.WriteLine("Invalid \"" + valueToAskFor + "\", must be between 5 and 20 characters");
Console.WriteLine("Please try again.");
Console.WriteLine(" ");
needToGetInputFromUser = true;
}
else
{
needToGetInputFromUser = false;
}
} while (needToGetInputFromUser);
return stringValue;
}
}
}
List<Customer> ListOfCustomer = new List<Customer> ();
do
{
needToGetInputFromUser = false;
Console.WriteLine("Please enter customer name");
customerName = Console.ReadLine();
if (customerName.Length < 5 || customerName.Length > 20)
{
Console.WriteLine("Invalid name length, must be between 5 and 20 characters");
Console.WriteLine("Please try again.");
Console.WriteLine(" ");
needToGetInputFromUser = true;
}
else
{
Customer c = new Customer();
c.Name = customerName;
ListOfCustomer.Add(c);
isUserEnteredValidInputData = true;
}
} while (needToGetInputFromUser);
int CustomerCount = ListOfCustomer.Count;
I assume you get a string like:
Brian Mains
And you want to split by the " " between Brian and Mains, storing it in first/last name in the Customer object right? I think you are trying to do something like:
Customer c = new Customer();
if (customerName.Contains(" ")) {
var terms = customerName.Split(' ');
if (terms.Length == 2) {
c.FirstName = terms[0];
c.LastName = terms[1];
}
else {
//TBD
}
}

Strings-creating "passwords" and "users" (simple with strings)

I need to create a program that will take the users name and "password". If they match the program will say you are in if not, that you're out. I wrote it for 1 user, but I don't know how to make multiple users in one program. My code is below. Thanks for your help :)
Console.WriteLine("Enter your Name");
Console.WriteLine("Enter your Pswrd");
string name = Console.ReadLine();
string pswrd = Console.ReadLine();
string myname = "John";
string mypswrd = "123456";
if (name == myname & pswrd == mypswrd)
{
Console.WriteLine("You are logged in");
}
else
{
Console.WriteLine("Incorrect name or pswrd");
}
Console.ReadLine();
//building the user "database" each pair is <user,password>
Dictionary<string, string> users = new Dictionary<string, string>();
users.Add("John", "123456");
//Here you should add more users in the same way...
//But i would advise reading them from out side the code (SQL database for example).
Console.Writeline("Enter your Name");
string name = Console.ReadLine();
Console.WriteLine("Enter your Passward");
string password = Console.ReadLine();
if (users.ContainsKey(name) && users[name] == password)
{
Console.WriteLine("You are logged in");
}
else
{
Console.WriteLine("Incorrect name or password");
}
Console.ReadLine();
This should work (contains no checks to see if the entered values are correct or not, you should add this kind of safety yourself :)):
Dictionary<string, string> namesToCheck = new Dictionary<string, string>
{
{"John", "123456"},
{"Harry", "someotherpassword"}
};
Console.WriteLine("Enter your Name");
string name = Console.ReadLine();
Console.WriteLine("Enter your Pswrd");
string pswrd = Console.ReadLine();
if (namesToCheck.ContainsKey(name) && namesToCheck[name] == pswrd)
{
Console.WriteLine("You are logged in");
}
else
{
Console.WriteLine("Incorrect name or pswrd");
}
Console.ReadLine();
Why not use arrays?
Console.WriteLine("Enter your Name");
Console.WriteLine("Enter your Pswrd");
string name = Console.ReadLine();
string pswrd = Console.ReadLine();
string[] names = "James,John,Jude".Split(Convert.ToChar(","));
string[] passes = "Pass1, Word2, Password3".Split(Convert.ToChar(","));
for (int i = 0; i<names.Length, i++)
{
if (names[i] == name && passes[i] == pswrd)
{
Console.WriteLine("You are logged in");
}
else
{
Console.WriteLine("Incorrect name or pswrd");
}
}
This will work with the following name/pswrd combinations:
James/Pass1, John/Word2, Jude/Password3
For a bigger list I suggest you use external text file and read lines in each.

Categories

Resources