Question Does anyone have a different proposal for solving the task?

Sajo

Member
Joined
Jul 22, 2020
Messages
17
Programming Experience
Beginner
Hello everyone. I solved the task correctly. What worries me is whether my solution takes up too much memory. In case anyone knows a better solution please write. I am currently studying complex conditions and conditional statements.
Here is the text of task.
The store sells three types of chocolate. For each type of chocolate we know how many grams there are and how much it costs. Write a C # program that for each chocolate loads from the user the name of the chocolate mass and price and determines which is the most cost-effective. The most cost-effective chocolate is the one that gets the most chocolate for the least money.
Here is example (Sorry for writing in Excel but it is easiest way for me to show you)
picture1.png



Here is my code
C#:
namespace aplikacija
{
    class Program
    {
        static void Main(string[] args)
        {
           
            string chocolate1, chocolate2, chocolate3;
            int weight1, weight2, weight3, price1, price2, price3;

            Console.WriteLine(" \a  Hello everybody, today we will choose which chocolate you will buy :D \n  Enter information about chocolates\n Enter the names of the chocolates ");
            chocolate1 = Console.ReadLine();
            chocolate2 = Console.ReadLine();
            chocolate3 = Console.ReadLine();
            Console.WriteLine(" Enter the weight of the chocolates ");
            weight1 = int.Parse(Console.ReadLine());
            weight2 = int.Parse(Console.ReadLine());
            weight3 = int.Parse(Console.ReadLine());
            Console.WriteLine(" Enter the prices of the chocolates");
            price1 = int.Parse(Console.ReadLine());
            price2 = int.Parse(Console.ReadLine());
            price3 = int.Parse(Console.ReadLine());
            Console.WriteLine(String.Format("Here is the entered data\n Name of chocolate:\t{0}\t{1}\t{2} \n Weight of chocolate:\t{3} g\t{4} g\t{5} g\n Price of chocolate:{6} $\t{7} $\t{8} $", chocolate1, chocolate2, chocolate3, weight1, weight2, weight3, price1, price2, price3));
            if (((weight1 > weight2) && (weight1 > weight3)))  
            {
             
                Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate1,weight1,price1));
           
            }

            else if (((weight2 > weight1) && (weight2 > weight3)))
            {
                Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate2, weight2, price2));
            }
            else if (((weight3 > weight1) && (weight3 > weight2)))
            {
                Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate3, weight3, price3));
            }
            else if ((price1<price2) && (price1<price3))
            {
                Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate1, weight1, price1));
            }

            else if ((price2 < price1) && (price2 < price3))
            {
                Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate2, weight2, price2));
            }
            else if ((price3 < price1) && (price3 < price2))
            {
                Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate3, weight3, price3));
            }

        }
    }
}
My code is working but if anyone knows better way, please can you write me?
 
Your solution doesn't necessarily take too much memory, but it is a lot of duplicate lines.

When you see yourself copying and pasting code that is usually an indication that you need to be using methods to remove the duplication. Also when you are using names with number suffixes, that you should be using some kind of data structure -- it could be as simple as an array, or something much more sophisticated. In this case, you don't really need it.

Anyway, we won't write your code for you, but consider this alternative pseudo code:
C#:
class Chocolate
{
    string Name;
    decimal Price;
    decimal Mass;
    decimal PricePerGram => Price / Mass;
}

class Program
{
    Chocolate InputChocolate()
    {
        chocolate = new Chocolate();
        input chocolate.Name from user
        input chocolate.Price from user
        input chocolate.Mass from user
        return chocolate
    }

    void Main()
    {
        bestChocolate = InputChocolate();
        do the following 2 times
        {
            chocolate = InputChocolate();

            if chocolate.PricePerGram is less than bestChocolate.PricePerGram
                bestChocolate = chocolate;
        }

        Output $"The best chocolate is {bestChocolate.Name} at {bestChocolate.PricePerGram} dollars per gram.";
    }
}
 
Last edited:
And if you are into using LINQ, Main() could potentially look something like this:
C#:
var best = Enumerable.Repeat(3, 0)
                     .Select(_ => InputChocolate())
                     .Min(c => c.PricePerGram);
Console.WriteLine($"The best chocolate is {best.Name} at {best.PricePerGram} dollars per gram.");
 
Back
Top Bottom