Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.5 kB
4
Indexable
Never
using System;

namespace Car
{
    public class Task2
    {
        public static double p1;    //purchase price car1
        public static double p2;    //purchase price car2
        public static double c1;    //fuel consumption car1
        public static double c2;    //fuel consumption car2
        public static double t1;    //total cost car1
        public static double t2;    //total cost car2
        public static void Main()
        {
            Reset();
            Car1Declaration();
            Car2Declaration();
            Calculate();
        }
        public static void Car1Declaration()
        {
            Console.WriteLine("Please enter the purchase price for car 1: ");
            string tmp = Console.ReadLine();
            if (!double.TryParse(tmp, out p1) || p1 <= 0)
            {
                Console.WriteLine(tmp + " is invalid. Please input a positive number for the price for car 1");
                Main();
            }

            Console.WriteLine("Please enter the fuel consumption rate for car 1: ");
            string tmp2 = Console.ReadLine();
            if (!double.TryParse(tmp2, out c1) || c1 <= 0)
            {
                Console.WriteLine(tmp2 + " is invalid. Please input a positive number for the fuel consumption rate for car 1");
                Main();
            }
        }

        public static void Car2Declaration()
        {
            Console.WriteLine("Please enter the purchase price for car 2: ");
            string tmp = Console.ReadLine();
            if (!double.TryParse(tmp, out p2) || p2 <= 0)
            {
                Console.WriteLine(tmp + " is invalid. Please input a positive number for the price for car 2");
                Main();
            }

            if(p1 >= p2)
            {
                Console.WriteLine($"invalid, price of car 1 must be smaller than price of car 2");
                Main();
            }

            Console.WriteLine("Please enter the fuel consumption rate for car 2: ");
            string tmp2 = Console.ReadLine();
            if (!double.TryParse(tmp2, out c2) || c2 <= 0)
            {
                Console.WriteLine(tmp2 + " is invalid. Please input a positive number for the fuel consumption rate for car 2");
                Main();
            }

            if(c1 <= c2)
            {
                Console.WriteLine($"invalide, fuel consumption rate of car 1 must be larger than fuel consumption rate of car 2");
                Main();
            }
        }

        public static void Calculate()
        {
            t1 = c1 * 15000 * 4 * 10 + p1;
            t2 = c2 * 15000 * 4 * 10 + p2;

            if(t1 < t2)
            {
                Console.WriteLine("Cost of car 1 is: " + t1);
                Console.WriteLine("Cost of car 2 is: " + t2);
                Console.WriteLine("Car 1 is more cost efficient than Car 2\n");
                Main();
            }
            else
            {
                Console.WriteLine("Cost of car 1 is: " + t1);
                Console.WriteLine("Cost of car 2 is: " + t2);
                Console.WriteLine("Car 2 is more cost efficient than Car 1\n");
                Main();
            }
        }

        public static void Reset()
        {
            p1 = 0;
            p2 = 0;
            c1 = 0;
            c2 = 0;
            t1 = 0;
            t2 = 0;
        }
    }
}
Leave a Comment