Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am new here.
I am looking for help / hints.
I have code to write that counts articles. After entering the code starting with A, do not count the tax, for B it is 8%, for the other codes 23% of the tax (I already have it), but I have a problem summing up these amounts, and the application should ask for many codes (I also have it) to time until we enter "sum" and only then adds up the codes dividing into net and gross. I have already started writing List <>, but is this the right direction?
I will be grateful for the hint, and not solving the task for me.

What I have tried:

C#
<pre>using System;
using System.Collections.Generic;
using System.Linq;

namespace KasaTest
{
    class Kasa
    {

        public static void Main(string[] args)
        {
            
            

            while (true)
            {
                Console.WriteLine("podaj kod produktu");
                try
                {
                    
                    //string podlicz = Console.ReadLine(); 
                    string productcode = Console.ReadLine();
            string code = productcode.Substring(0, 1);
                   
            double number = Convert.ToInt64(productcode.Substring(1));


                    if (code == "exit")
                    {
                        return;
                    }
                    List<double> sumowanie = new List<double> { number };


                    //Console.WriteLine(code);
                    //Console.WriteLine(number);
                    //Console.WriteLine(productcode);
                    //Console.WriteLine(list);



                    if (code == "a" || code == "A")
                    {
                        Console.WriteLine("do zapłaty " + number / 100 + ",00 zł");
                      
                        continue;
                        
                    }

                    if (code == "b" || code == "B")
                    {
                        
                        Console.WriteLine("do zapłaty " + (number * 0.0108) + " zł");
                    
                        continue;
                    }
                    else
                    {
                        
                        Console.WriteLine("do zapłaty " + (number * 0.0123) + " zł");
                        
                        continue;
                    }
                    if (code == "suma")
                    {
                        Console.WriteLine(sumowanie.Sum());
                        break;
                    }

                }
                catch (FormatException)
                {
                    Console.WriteLine("UWAGA! Podaj właściwy kod");
                    continue; 
                }
          
               
            }
            

        }

    }
    
    

}
Posted
Updated 21-Feb-22 11:26am
v2

A number of basic problems:
1. code can never equal "exit" because it is only a single character.
2. You recreate the List<double> each time around the loop. You should create it once before the loop starts and just add each number as you progress.
3. You should use Double.TryParse Method (System) | Microsoft Docs[^] to capture the input values.
4. Instead of using a List for the sum(s) you could create variables at the b eginning and just add the values as you capture them.
5. As point 1 "suma" is more than one character.
 
Share this answer
 
Comments
Maciej Los 21-Feb-22 10:46am    
5ed!
Richard MacCutchan 21-Feb-22 11:45am    
Thanks again.
You need a collection of some form - and a List is a good idea - if you are planning of storing the entries.
But if all you are interested in is the sum or the values, then just create a simple variable to hold it, initialise it to zero, and add each value as you go round. You can then print the sum so far at any time.

But ... rather than using Convert and throwing a try ... catch in there, use int.TryParse, Doubel.TryParse or similar - it returns a boolean "success / fail" value which you can use in a if directly. Makes you code much easier to read.

To be honest, it doesn't look like you thought much before you leapt into code there: the indentation is all over the place, the spacing is weird, and I have no idea why you convert a string to an integer just to store it in a double?
Stop and think about what you are doing instead of jumping right into coding - it will save you a huge amount of time in the long run! This may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
Maciej Los 21-Feb-22 10:46am    
5ed!
The main idea is to work on specific data.
So, if you have a product, its price and tax rate for each product code, i'd suggest to create 2 classes:

C#
//holds product code and tax rate for that product
public class PTax
{
	public string PCode { get; set; }
	public double TaxRate { get; set; }
}

//holds product price depending on product code 
public class PPrice
{
	public PTax PTax { get; set; }
	public double Netto { get; set; }
	public double Brutto
	{
		get  => Netto * PTax.TaxRate;
	}
}


Usage:
C#
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("pl-PL");
List<PTax> prodTax = new List<PTax>()
    {
        new PTax{PCode = "A", TaxRate = 1.00},
        new PTax{PCode = "B", TaxRate = 1.08},
        new PTax{PCode = "C", TaxRate = 1.23}
    };
List<PPrice> prodPrice = new List<PPrice>()
{
    new PPrice(){PTax = prodTax[0], Netto = 110.00},
    new PPrice(){PTax = prodTax[0], Netto = 200.00},
    new PPrice(){PTax = prodTax[0], Netto = 5.50},
    new PPrice(){PTax = prodTax[1], Netto = 500.00},
    new PPrice(){PTax = prodTax[1], Netto = 1180.15},
    new PPrice(){PTax = prodTax[2], Netto = 333.33},
    new PPrice(){PTax = prodTax[2], Netto = 2000.00}
};

Console.WriteLine("List of products and their prices:");
foreach(PPrice pp in prodPrice)
    Console.WriteLine($"Product: '{pp.PTax.PCode}'\tNet. price: {pp.Netto.ToString("C2", ci)}\tBrutto: {pp.Brutto.ToString("C2", ci)}");

var sumy = prodPrice
    .GroupBy(x => x.PTax.PCode)
    .Select(grp => new
    {
        PCode = grp.Key,
        Count = grp.Count(),
        Netto = grp.Sum(x=> x.Netto),
        Tax = grp.Select(x=> x.PTax.PCode).FirstOrDefault(),
        Brutto = grp.Sum(x=> x.Brutto)
    })
    .ToList();

Console.WriteLine("\nSum of products based on product code:");
foreach(var p in sumy)
    Console.WriteLine($"Product: '{p.PCode}'\tCount:{p.Count}\tNet. price:{p.Netto.ToString("C2", ci)}\tBrutto:{p.Brutto.ToString("C2", ci)}");


Result:
List of products and their prices:
Product: 'A'  Net. price: 110,00 zł  Brutto: 110,00 zł
Product: 'A'  Net. price: 200,00 zł  Brutto: 200,00 zł
Product: 'A'  Net. price: 5,50 zł  Brutto: 5,50 zł
Product: 'B'  Net. price: 500,00 zł  Brutto: 540,00 zł
Product: 'B'  Net. price: 1 180,15 zł  Brutto: 1 274,56 zł
Product: 'C'  Net. price: 333,33 zł  Brutto: 410,00 zł
Product: 'C'  Net. price: 2 000,00 zł  Brutto: 2 460,00 zł

Sum of products based on product code:
Product: 'A'  Count:3  Net. price:315,50 zł  Brutto:315,50 zł
Product: 'B'  Count:2  Net. price:1 680,15 zł  Brutto:1 814,56 zł
Product: 'C'  Count:2  Net. price:2 333,33 zł  Brutto:2 870,00


Good luck!
 
Share this answer
 
What y think about it?
Now I have one problem....netto.Sum() and brutto.Sum(). Doesn't work. Can someone explain why?

C#
<pre>using System;
using System.Collections.Generic;
using System.Linq;

namespace KasaTest
{
    class Kasa
    {
        public static void Main(string[] args)
        {
            while (true)
            {  
                Console.WriteLine("podaj kod produktu");
                try
                {
                    List<double> netto = new List<double>();
                    List<double> brutto = new List<double>();
                    string productcode = Console.ReadLine();
                    string code = productcode.Substring(0, 1);
                    double number = int.Parse(productcode.Substring(1));
                    
                    if (code == "a" || code == "A")
                    {
                        Console.WriteLine("kwota netto " + number / 100  + " kwota brutto: " + number / 100 + ",00 zł");
                        brutto.Add(number / 100);
                        netto.Add(number / 100);
                        continue;
                    }

                    if (code == "b" || code == "B")
                    {
                        Console.WriteLine("kwota netto: " + (number /100) + " kwota brutto: " + (number * 0.0108) + " zł");
                        brutto.Add(number * 0.0108);
                        netto.Add(number / 100);
                        continue;
                    }

                    else
                    { 
                        Console.WriteLine("kwota netto: " + (number / 100) + " kwota brutto: " + (number * 0.0123) + " zł");
                        brutto.Add(number * 0.0123);
                        netto.Add(number / 100);
                        continue;
                    }
                    
                       // Console.WriteLine(netto.Sum() + brutto.Sum());
                }
                catch (FormatException)
                {
                    Console.WriteLine("UWAGA! Podaj właściwy kod");
                    continue;
                }

            }
        }
    }
}
 
Share this answer
 
Comments
Maciej Los 21-Feb-22 10:38am    
This is not an answer. Please, improve your question instead of posting a "solution".
BTW: you don't need to add "zł" or "PLN" at the end of line. Please, read this:
Standard numeric format strings | Microsoft Docs[^] -> Currency format specifier.
Custom numeric format strings | Microsoft Docs[^]

See:
var value = 1235.58;
Console.WriteLine(value.ToString("C3", System.Globalization.CultureInfo.CreateSpecificCulture("pl-PL")));
//prints: 1 235,580 zł

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900