settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Fruit Market - C# Task

+8 votes

The local fruit market offers fruits and vegetables with the following standard price list:

  • banana -> 1.80
  • cucumber -> 2.75
  • tomato -> 3.20
  • orange -> 1.60
  • apple -> 0.86

The market owner decided to introduce the following discounts:

  • Friday -> 10% off for all products
  • Sunday -> 5% off for all products
  • Tuesday -> 20% off for fruits
  • Wednesday -> 10% off for vegetables
  • Thursday -> 30% off for bananas

Write a program that helps the fruit market owner to calculate the total price for orders that consist of day, 3 products with quantities.

Input:

The input data should be read from the console. The input data consists of exactly 7 lines:

  • At the first line you will be given the day of week.
  • At the next 6 lines you will be given: quantity1, product1, quantity2, product2, quantity3, product3.

The input data will always be valid and in the format described. There is no need to check it explicitly.

Output:

You have to print at the console the total price for the specified 3 products at the specified day of week.

Constraints:

  • The day of week is one of the values: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday.
  • The product quantities (quantity1, quantity2, quantity3) will be a number in the range [1…100], with up to 2 digits after the decimal point. The will be used "." as decimal separator.
  • The products names (product1, product2, product3) is one of the values: banana, cucumber, tomato, orange, and apple.
  • The total price should be rounded to exactly 2 digits after the decimal point (use "." as decimal separator).
  • Allowed work time for your program: 0.1 seconds.
  • Allowed memory: 16 MB.

Examples:

fruit market c # task example

fruit market c # task example 2

asked in C# category by user john7
edited by user golearnweb

2 Answers

+3 votes
 
Best answer

Here's the solution with dictionary:

using System;
using System.Collections.Generic;

class FruitMarket14April2014Morning
{
    static void Main()
    {
        string dayOfTheWeek = Console.ReadLine();
        decimal quantity1 = decimal.Parse(Console.ReadLine());
        string product1 = Console.ReadLine();
        decimal quantity2 = decimal.Parse(Console.ReadLine());
        string product2 = Console.ReadLine();
        decimal quantity3 = decimal.Parse(Console.ReadLine());
        string product3 = Console.ReadLine();

        Dictionary<string, decimal> products = new Dictionary<string, decimal>();
        products.Add("banana", 1.8m);
        products.Add("orange", 1.6m);
        products.Add("apple", 0.86m);
        products.Add("cucumber", 2.75m);
        products.Add("tomato", 3.2m);

        switch (dayOfTheWeek)
        {
            case "Monday":
                products["banana"] = 1.8m;
                products["orange"] = 1.6m;
                products["apple"] = 0.86m;
                products["cucumber"] = 2.75m;
                products["tomato"] = 3.2m;
                break;
            case "Tuesday":
                products["banana"] = 1.8m * 0.8m;
                products["orange"] = 1.6m * 0.8m;
                products["apple"] = 0.86m * 0.8m;
                products["cucumber"] = 2.75m;
                products["tomato"] = 3.2m;
                break;
            case "Wednesday":
                products["banana"] = 1.8m;
                products["orange"] = 1.6m;
                products["apple"] = 0.86m;
                products["cucumber"] = 2.75m * 0.9m;
                products["tomato"] = 3.2m * 0.9m;
                break;
            case "Thursday":
                products["banana"] = 1.8m * 0.7m;
                products["orange"] = 1.6m;
                products["apple"] = 0.86m;
                products["cucumber"] = 2.75m;
                products["tomato"] = 3.2m;
                break;
            case "Friday":
                products["banana"] = 1.8m * 0.9m;
                products["orange"] = 1.6m * 0.9m;
                products["apple"] = 0.86m * 0.9m;
                products["cucumber"] = 2.75m * 0.9m;
                products["tomato"] = 3.2m * 0.9m;
                break;
            case "Saturday":
                products["banana"] = 1.8m;
                products["orange"] = 1.6m;
                products["apple"] = 0.86m;
                products["cucumber"] = 2.75m;
                products["tomato"] = 3.2m;
                break;
            case "Sunday":
                products["banana"] = 1.8m * 0.95m;
                products["orange"] = 1.6m * 0.95m;
                products["apple"] = 0.86m * 0.95m;
                products["cucumber"] = 2.75m * 0.95m;
                products["tomato"] = 3.2m * 0.95m;
                break;
        }

        decimal sum = quantity1 * products[product1] + quantity2 * products[product2] + quantity3 * products[product3];
        Console.WriteLine("{0:F2}",sum);
    }
}

 

answered by user sam
selected by user golearnweb
+3 votes

Interesting solution here as well: http://www.vitoshacademy.com/c-basic-exam-fruit-market/

answered by user sam
edited by user golearnweb
...