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.
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.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
read_database that:products.txt file into a List<string[]>.get_product that:output_product that:NAME £PRICE.transaction that:get_product to input the button number.output_product to output the relevant data.read_database is called.transaction is called.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
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}");
}