Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

Lookup product description and price.

★★☆

A touchscreen till displays a number of virtual buttons. Each button corresponds to a product for sale. For example, button 1 is a Snickers Bar. The products are held in a file called products.txt. The format of the file is: button number, product name, price.

0,CADBURY DAIRY MILK WHOLENUT 49G,0.70
1,SNICKERS BAR 48G,0.70
2,CADBURY DAIRY MILK MILK CARAMEL,0.70
3,MARS BAR ORIGINAL 51G,0.80
4,CADBURY FLAKE 32G,0.80
5,GALAXY CARAMEL STANDARD 48G,0.80
6,GALAXY MILK CHOCOLATE 46G,0.80
7,CADBURY STAR BAR 49G,0.60
8,NESTLE MUNCHIES 52G,0.60
9,KINDER BUENO 43g,1.00
10,SNICKERS DUO 83.4G,1.00

For the purposes of testing, the till is simulated with the user entering a button number followed by Enter. Any non-matching input (including an empty line) simulates the end of a transaction. A user can input any number of products in a single transaction. When a transaction is complete the total price is displayed.

Checkout

Make

Write a program that allows the user to enter a button number. Invalid numbers end the transaction. The program outputs the name of the product and the price. The user can keep entering numbers until they enter something that does not match a product, at which point the total of the transaction is shown.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called read_database that:

  1. Reads the products.txt file into a List<string[]>.

Complete the subprogram called get_product that:

  1. Prompts the user to enter a button number.
  2. Validates the input so that only no input or a number between 0 and the maximum button number in the product list is accepted.
  3. Returns the button number, or -1 for no input.

Complete the subprogram called output_product that:

  1. Takes a parameter, the button number.
  2. Outputs the product name and price formatted as NAME £PRICE.

Complete the subprogram called transaction that:

  1. Calls get_product to input the button number.
  2. Calls output_product to output the relevant data.
  3. Continues to input and output products until the transaction is terminated.
  4. Outputs the total price of the transaction with two decimals.

Complete the main program so that:

  1. read_database is called.
  2. transaction is called.

Typical inputs and outputs from the program would be:

Enter button number:
2
CADBURY DAIRY MILK MILK CARAMEL £0.70
Enter button number:

Total: £0.70
Enter button number:
13
Enter button number:

Total: £0.00
Enter button number:
8
NESTLE MUNCHIES 52G £0.60
Enter button number:
2
CADBURY DAIRY MILK MILK CARAMEL £0.70
Enter button number:
4
CADBURY FLAKE 32G £0.80
Enter button number:

Total: £2.10
🆘 If you're really stuck, use this Parsons code sorting exercise
read_database
// Read data from filename and return a 2D list
static void read_database()
{
---
    StreamReader file = new StreamReader("products.txt");
    string record;
---
    // Read each line of data until end of file
    while ((record = file.ReadLine()) != null)
    {
---
        record = record.Trim();
        string[] fields = record.Split(',');
        products.Add(fields);
---
    }
---
    file.Close();
}
get_product
// Input a valid product code or no data to terminate
static int get_product()
{
---
    int num_choice = 0;
    bool valid = false;
    // Keep asking until a valid choice is entered
    while (!valid)
    {
---
        Console.WriteLine("Enter button number:");
        string choice = Console.ReadLine();
        valid = true;
---
        // No data terminates the transaction
        if (choice == null || choice.Trim() == "")
            return -1;
        choice = choice.Trim();
---
        // The input must be a number
        foreach (char c in choice)
        {
            if (!char.IsDigit(c))
            {
                valid = false;
            }
        }
---
        // The number must be a valid button number
        if (valid)
        {
            num_choice = Convert.ToInt32(choice);
            if (num_choice < 0 || num_choice >= products.Count)
            {
                valid = false;
            }
        }
    }
---
    return num_choice;
}
output_product
// Output the product description and price
static void output_product(int button_number)
{
---
    string output = products[button_number][1] + " £" + products[button_number][2];
    Console.WriteLine(output);
---
}
transaction
// Process one transaction
static void transaction()
{
---
    int button_number = 0;
    double total = 0;
---
    // Input products until transaction ends
    while (button_number != -1)
    {
---
        button_number = get_product();
        // Output the product and add price to transaction total
        if (button_number != -1)
        {
---
            output_product(button_number);
            total += Convert.ToDouble(products[button_number][2]);
---
        }
---
    }
---
    // Output the total
    string stotal = total.ToString("0.00");
    Console.WriteLine($"Total: £{stotal}");
}