0

I have this code in which I take in data from the user to create a vehicle object. The only problem is, since they are different data types I am asking for I have to switch between Read and ReadLine. When the code gets down to the ReadLine part, it prints out all of my WriteLine statements instead of just waiting for input from the first.

Code:

Console.Write("What would you like to do? ");
s = Console.ReadLine();
if (s == "1")
{
    Console.Write("Please Enter the vehicle ID: \n");
    ID = Console.Read();
    Console.Write("Please enter the vehicle make: \n");
    make = Console.ReadLine();
    Console.Write("Please enter the vehicle model: \n");
    model = Console.ReadLine();

    Console.Write("Please enter the vehicle year: \n");
    year = Console.Read();

    Vehicle v;
    v = new Vehicle
    {
        Id = ID,
        Make = make,
        Model = model,
        Year = year
    };

    Create(v);
}

Output:

 Please enter the vehicle ID:
 Input: 123

 Please enter the Vehicle make:

 Please enter the Vehicle model:

Here is my class definition, as requested in the comments:

public class Vehicle
{
    public int Id { get; set; }
    public int Year { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public static string Class { get; set; }

    public interface VehicleService
    {
        List<Vehicle> Get();
        Vehicle Get(int id);
        void Create(Vehicle vehicle);
        void Update(Vehicle vehicle);
        void Delete(Vehicle vehicle);
    }
}

Any help would be greatly appreciated.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
kdean693
  • 27
  • 7

2 Answers2

0

It appears you have just started learning coding. Below is what you need to do as a novice. Try to invest time to understand the below code and try to improve on. You have not shown the datatype of object members, so I have done with an assumption.

static void Test1()
{
    string s = "", make = "", model = "";
    int ID, year;

    Console.Write("What would you like to do ? Enter option between 1 or 2 : ");
    //Console.ReadLine() will take the all the characters typed by the user before enter key is hit.
    //These characters will be assigned to a string s.
    //If you want to read next character use Console.Read(). 
    //Note: Its returns an int rather than byte or string.
    //since your if condition matches it with "1", you can use ReadLine()

    s = Console.ReadLine();
    if (s == "1")
    {
        string consoleLine;
        bool validInput = false;

        //you can create a function for the validation and re-enter
        do
        {
            Console.WriteLine("Enter the vehicle ID: ");
            consoleLine = Console.ReadLine();
            //check for validity
            if (int.TryParse(consoleLine, out ID))
            {
                //assuming ID should be between 1 and 10
                if (ID > 0 && ID < 11)
                {
                    validInput = true;
                }
                else
                    Console.Write("Vehicle ID should be between 1 and 10. Re-");
            }
            else
            {
                Console.WriteLine("Vehicle ID should be an integer. Re-");
            }
        } while (!validInput);

        validInput = false;
        do
        {
            Console.WriteLine("Enter the Year: ");
            consoleLine = Console.ReadLine();
            //check for validity
            if (int.TryParse(consoleLine, out year))
            {
                //assuming ID should be between 1 and 10
                if (year > 0 && year < 2021)
                {
                    validInput = true;
                }
                else
                    Console.Write("Year should be between 1 and 2020. Re-");
            }
            else
            {
                Console.WriteLine("Year should be an integer. Re-");
            }
        } while (!validInput);

        validInput = false;
        do
        {
            Console.WriteLine("Enter the vehicle make: ");
            make = Console.ReadLine();

            if (make.Length > 0)
            {
                validInput = true;
            }
            else
                Console.Write("Vehicle Make should be a valid text. Re-");
        } while (!validInput);

        validInput = false;
        do
        {
            Console.WriteLine("Enter the vehicle model: ");
            model = Console.ReadLine();

            if (model.Length > 0)
            {
                validInput = true;
            }
            else
                Console.Write("Vehicle Model should be a valid text. Re-");
        } while (!validInput);


        //here you have received all input
        //now assign to Vehicle
        var vehicle = new Vehicle
        {
            Id = ID,
            Make = make,
            Year = year,
            Model = model
        };

        //Below code shows you that values are stored in the object vehicle
        Console.WriteLine("Vehicle details stored in the object vehicle");
        Console.WriteLine("Vehicle Id = " + vehicle.Id.ToString());
        Console.WriteLine("Vehicle Year = " + vehicle.Year.ToString());
        Console.WriteLine("Vehicle Make = " + vehicle.Make);
        Console.WriteLine("Vehicle Model = " + vehicle.Model);

        //You have not shown where you have declared the List<Vehicle> to
        //store the collection of Vehicles.
        //You might have implemented VehicleService. So you need to search the 
        //Id in the List of Vehicle and if found use Update() else use Create()
        //through the service implementation.
    }

    Console.WriteLine("Done. Press enter to Exit.");
    Console.ReadKey();
}
Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19
  • I appreciate that a lot, but how would I use that input to create my vehicle object which I showed in the code above? Sorry, I am very new to c#. – kdean693 Apr 09 '17 at 04:16
  • Added the class definition as an answer below. – kdean693 Apr 10 '17 at 01:27
  • @kdean693, Updated the code. Generally I will not invest time in writing the implementation, but since you are novice I have written the function for you. Refer the url "http://stackoverflow.com/questions/6825943/difference-between-console-read-and-console-readline" for difefrence between Read and ReadLine. – Mukul Varshney Apr 10 '17 at 04:45
0

You should use Console.ReadLine(); instead of Console.Read();, you can find different of them by difference-between-console-read-and-console-readline

If you still want to use Console.Read(); please use FlushKeyboard() method.

private static void FlushKeyboard()
{
    while (Console.In.Peek() != -1)
        Console.In.Read();
}

And use this method after calling Console.Read();

Console.Write("What would you like to do? ");
s = Console.ReadLine();
if (s == "1")
{
    Console.Write("Please Enter the vehicle ID: \n");
    ID = Console.Read();
    FlushKeyboard();
    Console.Write("Please enter the vehicle make: \n");
    make = Console.ReadLine();
    Console.Write("Please enter the vehicle model: \n");
    model = Console.ReadLine();

    Console.Write("Please enter the vehicle year: \n");
    year = Console.Read();
    FlushKeyboard();

    Vehicle v;
    v = new Vehicle
    {
        Id = ID,
        Make = make,
        Model = model,
        Year = year
    };
    Create(v);
}
Community
  • 1
  • 1
GSP
  • 574
  • 3
  • 7
  • 34