Lektion 13: lösningsförslag
- class Product
- {
- public string Name { get; set; }
- public double Price { get; set; }
- }
- static void Main(string[] args)
- {
- List<Product> cart = new List<Product>();
- string[] lines = File.ReadAllLines("Products.txt");
- foreach (string line in lines)
- {
- try
- {
- string[] parts = line.Split(',');
- string name = parts[0];
- double price = double.Parse(parts[1]);
- Product product = new Product
- {
- Name = name,
- Price = price
- };
- cart.Add(product);
- }
- catch
- {
- Console.WriteLine("Invalid line in product file");
- }
- }
- double total = 0;
- foreach (Product p in cart)
- {
- total += p.Price;
- }
- if (total < 300)
- {
- total += 29;
- }
- Console.WriteLine("Total: " + total);
- }
- class Product
- {
- public string Name { get; set; }
- public double Price { get; set; }
- }
- static void Main(string[] args)
- {
- List<Product> cart = new List<Product>();
- string[] lines = File.ReadAllLines("Products.txt");
- foreach (string line in lines)
- {
- try
- {
- string[] parts = line.Split(',');
- string name = parts[0];
- double price = double.Parse(parts[1]);
- Product product = new Product
- {
- Name = name,
- Price = price
- };
- cart.Add(product);
- }
- catch
- {
- Console.WriteLine("Invalid line in product file");
- }
- }
- double total = 0;
- foreach (Product p in cart)
- {
- total += p.Price;
- }
- if (total < 300)
- {
- total += 29;
- }
- string[] validCodes = File.ReadAllLines("Codes.txt");
- Console.Write("Please enter code: ");
- string userCode = Console.ReadLine();
- bool codeFound = false;
- while (userCode != "" && !codeFound)
- {
- foreach (string code in validCodes)
- {
- if (code == userCode)
- {
- codeFound = true;
- }
- }
- if (!codeFound)
- {
- Console.WriteLine("Invalid code entered.");
- Console.Write("Please enter code: ");
- userCode = Console.ReadLine();
- }
- }
- if (codeFound)
- {
- total = total * 0.8;
- }
- Console.WriteLine("Total: " + total);
- }
- class Product
- {
- public string Name { get; set; }
- public double Price { get; set; }
- }
- static void Main(string[] args)
- {
- List<Product> products = new List<Product>();
- string[] lines = File.ReadAllLines("Products.txt");
- foreach (string line in lines)
- {
- try
- {
- string[] parts = line.Split(',');
- string name = parts[0];
- double price = double.Parse(parts[1]);
- Product product = new Product
- {
- Name = name,
- Price = price
- };
- products.Add(product);
- }
- catch
- {
- Console.WriteLine("Invalid line in product file");
- }
- }
- List<Product> cart = new List<Product>();
- Console.Write("Please enter product name: ");
- string productName = Console.ReadLine();
- while (productName != "")
- {
- bool productFound = false;
- foreach (Product p in products)
- {
- if (p.Name == productName)
- {
- productFound = true;
- cart.Add(p);
- }
- }
- if (!productFound)
- {
- Console.WriteLine("Product " + productName + " does not exist.");
- }
- Console.Write("Please enter product name: ");
- productName = Console.ReadLine();
- }
- double total = 0;
- foreach (Product p in cart)
- {
- total += p.Price;
- }
- if (total < 300)
- {
- total += 29;
- }
- string[] validCodes = File.ReadAllLines("Codes.txt");
- Console.Write("Please enter code: ");
- string userCode = Console.ReadLine();
- bool codeFound = false;
- while (userCode != "" && !codeFound)
- {
- foreach (string code in validCodes)
- {
- if (code == userCode)
- {
- codeFound = true;
- }
- }
- if (!codeFound)
- {
- Console.WriteLine("Invalid code entered.");
- Console.Write("Please enter code: ");
- userCode = Console.ReadLine();
- }
- }
- if (codeFound)
- {
- total = total * 0.8;
- }
- Console.WriteLine("Total: " + total);
- }