Cant work out why my program is behaving differently

WB1975

Well-known member
Joined
Apr 3, 2020
Messages
87
Programming Experience
Beginner
Hi guys,

I just cant work out why my program is behaving differently to the tutorial im following, i have checked the code again and again (Though i bet you its something stupid i missed, usually is)

the problem im having, where his program is different is that after he creates a new car object, he is presented with the menu again:

Choose an action: \n(0) to quit \n(1) to add to inventory \n(2) to add a car to cart \n(3) to checkout"

where my program just wants to add a car again without presenting the earlier objects, its like the method just isnt being called again to take a user choice, and i dont know why.

this line seems to be the first i see after i create a new car.

also, what is the difference between

static public

and

public static?

C#:



C#:
using CarClassLibrary;
using System;

namespace CarShopConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Store s = new Store();

            Console.WriteLine("Welcome to the car store. First you must create some car inventory. Then you may add some cars to the shopping cart. Finally you can checkout which will give you the value of the shopping cart.");

            int action = ChooseAction();
          
            while (action != 0)
            {
                Console.WriteLine("You chose " + action);
                switch (action)
                {
                    case 1:
                        Console.WriteLine("You chose to add a new car to the inventory.");

                        string carMake = "";
                        string carModel = "";
                        Decimal carPrice = 0;

                        Console.WriteLine("What is the car make? eg: Ford, Nissan, Toyota etc");
                        carMake = Console.ReadLine();

                        Console.WriteLine("What is the car model? eg: Cortina, Sentra, Corrola etc");
                        carModel = Console.ReadLine();

                        Console.WriteLine("What is the car price?");
                        carPrice = int.Parse(Console.ReadLine());

                        Car newCar = new Car(carMake, carModel, carPrice);
                        s.CarList.Add(newCar);

                        PrintInventory(s);
                        break;
                        
                }
            }

            

          
        }

        private static void PrintInventory(Store s)
        {
            foreach(Car c in s.CarList)
            {
                Console.WriteLine("Car: " + c.Make + " " + c.Model + " " + c.Price);
            }
        }

        static public int ChooseAction ()
        {
            int choice = 0;
            Console.WriteLine("Choose an action: \n(0) to quit \n(1) to add to inventory \n(2) to add a car to cart \n(3) to checkout");
            choice = int.Parse(Console.ReadLine());
            return choice;
        }
    }
}
 
also, what is the difference between

static public

and

public static?
It means the same, they are both modifiers. One is an access modifier, the other that it is static rather than instance. There is a preferred modifier order, and this rule is silent by default, so it is not important.
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:silent
from .NET coding convention settings For EditorConfig - Visual Studio
The access modifiers are put first, and that seem logical to me and eases readability.
 
Back
Top Bottom