Jump to content

How to make it better

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
Hi, I'm learning C# and have come up with a little shop scenario to calculate addition and percentage of the stock the user enters, my question is how can I improve the code, so where to go from here and what to learn? To move away from it being basic like it is below, for example could I use arrays? Or place the calculations in different methods?

Thank-You :)

AJ

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace Test_Program

{

    class ShopScenario

    {

        static void Main(string[] args)

        {

            

                Console.WriteLine("Please Enter the Amount of Oranges");  

                int Oranges = Convert.ToInt32(Console.ReadLine());  


                Console.WriteLine("\nPlease Enter the Amount of Apples");  

                int Apples = Convert.ToInt32(Console.ReadLine()); 


                Console.WriteLine("\nPlease Enter the Amount of Grapes"); 

                int Grapes = Convert.ToInt32(Console.ReadLine()); 




                float Stock_Total = Oranges + Apples + Grapes;

                Console.WriteLine("Total Amount: {0}",Stock_Total);


                float Oranges_Percentage = ((Oranges / Stock_Total) * 100);

                float Apples_Percentage = ((Apples / Stock_Total) * 100);

                float Grapes_Percentage = ((Grapes / Stock_Total) * 100);

                


                Console.WriteLine("Orange Percentage: {0:f0}%", Oranges_Percentage);

                Console.WriteLine("Apples Percentage: {0:f0}%", Apples_Percentage);

                Console.WriteLine("Grapes Percentage: {0:f0}%", Grapes_Percentage);

                

                


              

        }



    }

}



#2
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Hi!

Here is one possibility how to make it better, but I'm sure there are many better solutions than mine :-)


using System;

using System.Collections.Generic;


namespace Fruits

{

    class Program

    {

        static IList<Fruit> fruits = new List<Fruit>()

        {

            new Fruit("Apples"),

            new Fruit("Oranges"),

            new Fruit("Grapes")

        };


        static void Main(string[] args)

        {

            double stock = 0;


            try

            {

                foreach (Fruit fruit in fruits)

                {

                    Console.Write("Please Enter the Amount of {0}: ", fruit.Name);

                    fruit.Amount = int.Parse(Console.ReadLine());

                    stock += fruit.Amount;

                }


                Console.WriteLine("\r\nTotal Amount: {0}", stock);

                foreach (Fruit fruit in fruits)

                {

                    Console.WriteLine("{0} Percentage: {1}%", fruit.Name, Math.Round((fruit.Amount / stock) * 100, 1));

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }


            Console.ReadLine();

        }

    }


    class Fruit

    {

        public string Name { get; set; }

        public int Amount { get; set; }


        public Fruit() { }


        public Fruit(string name) : this()

        {

            Name = name;

            Amount = 0;

        }

    }

}