Vehicle Gallery System With C# [Course 2]

in #utopian-io7 years ago (edited)

What Will I Learn?

Hello,today i am going to show you how to make a vehicle gallery system without using a form application.

  • You will learn usage of switch case
  • You will learn c# coding
  • You will learn how to make a gallery system without using form application.

Requirements

  • Sharpdevelop
  • Basic knowledge of c#
  • Basic knowledge of sharpdevelop and coding.

Difficulty

  • Intermediate

Tutorial Contents

After defining libraries i create two new arrays.One for strings which means my motorcyle and car types and the other for double which for my vehicles price.


public static string[] Menu = new string[] { "Motorcycle", "Motorcycle1", "Motorcycle2",
"Motorcycle3", "Motorcycle4", "Motorcycle5", "Motorcycle6", "Car1", "Car2", "Car3", "Car4", "Car5",
"Car6", "Car7" };


public static double[] Price = new double[] {10000, 20000, 30000, 40000, 50000, 60000, 70000, 10000, 20000, 30000, 40000, 50000, 60000, 70000 };

Now i am going to define 3 double.I am going equal total1,2 to zero to make calculation start from zero.


public static double piece;
public static double total1=0;
public static double total2=0;

Now i'm creating a new variable called "Total".


public static void Total(int j)

Now i will ask user how many vehicle he wants.To understand what user selected as you can see above i just defined integer called "j".What we do below is basically ask user how many vehicle it wants then convert "piece" variable to double because user entered a number we convert it to a stringer then equal "total1" variable to users vehicle price * piece.Then equal total2 to total1.


Console.Write("How many "+ Menu[j]+" do you want:");
piece = Convert.ToDouble(Console.ReadLine());
total1 = Price[j] * piece;
total2 += total1;

1.png

Then i am starting the program and asking user to press any key to start the program.


Console.WriteLine("Press a button to start");
Console.ReadKey();

Defining three integers which will show users selection below.


int choose, i, choose2;

Now in below i made a keyboard-menu.Which shows user prices of vehicles.

Console.WriteLine("**************************************************************");
            Console.WriteLine("*                             **                             *");
            Console.WriteLine("*      Motorcycles            **         Cars                *");
            Console.WriteLine("*                             **                             *");
            Console.WriteLine("* 1-   Motor : 10.000$        ** 8-   Car 1: 10.000$         *");
            Console.WriteLine("*                             **                             *");
            Console.WriteLine("* 2-   Motor 1: 20.000$       ** 9-   Car 2: 20.000$         *");
            Console.WriteLine("*                             **                             *");
            Console.WriteLine("* 3-   Motor 2: 30.000$       ** 10-  Car 3: 30.000$         *");
            Console.WriteLine("*                             **                             *");
            Console.WriteLine("* 4-   Motor 3: 40.000$       ** 11-  Car 4: 40.000$         *");
            Console.WriteLine("*                             **                             *");
            Console.WriteLine("* 5-   Motor 4: 50.000$       ** 12-  Car 5: 50.000$         *");
            Console.WriteLine("*                             **                             *");
            Console.WriteLine("* 6-   Motor 5: 60.000$       ** 13-  Car 6: 60.000$         *");
            Console.WriteLine("*                             **                             *");
            Console.WriteLine("* 7-   Motor 6: 70.000$       ** 14-  Car 7:  70.000$        *");
            Console.WriteLine("**************************************************************");
            Console.WriteLine("");

2.png

Allowing user to choose maximum 100 vehicles.


choose = 100;

Now i am going to ask user to enter selected products number.Since we got 14 vehicles i need to use for loop.


for (i = 1; i <= choose; i++)
Console.WriteLine(" ");
Console.Write("Products number:");
choose2 = Convert.ToInt16(Console.ReadLine());

3.png

In this part i will use switch case loop to calculate users selection.What we basically gonna do is we will evaluate all possibilities.If user makes a wrong choose we will warn user by using "default".

switch (choose2)
                    case 1: Total(1); break;
                    case 2: Total(2); break;
                    case 3: Total(3); break;
                    case 4: Total(4); break;
                    case 5: Total(5); break;
                    case 6: Total(6); break;
                    case 7: Total(7); break;
                    case 8: Total(8); break;
                    case 9: Total(9); break;
                    case 10: Total(10); break;
                    case 11: Total(11); break;
                    case 12: Total(12); break;
                    case 13: Total(13); break;
                    case 14: Total(14); break;
                    default: Console.WriteLine("You have made a wrong choose "); break;

4.png

In this part we will ask user if it wants to buy anything else if the answer is yes the program will continue if not we will use if loop and create "answer" as string.Then we will evaluate if user writes "n" or "no" and equal "answer" to all answer types.

Console.WriteLine("");
                Console.Write("Do you want to buy anything else? (y/n) ");
                string answer = Console.ReadLine();
                if ((answer == "n") || (answer == "N") || (answer == "no") || (answer == "NO"))
                    break;

Now in the final step we will show user the total price.Important thing here is using {0:C} to show user total price.{0:C} takes total price and shows it to the user.


Console.WriteLine("");
Console.WriteLine("Total:{0:C}", total2);
Console.ReadKey();

Whole code

5.png

6.png

Outputs of the code

7.png

8.png

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort: