Question
In C#, Based on Receipt Version 3, add input validation with regular expression. 1. Item name filed. -- Allow multiple words to be entered --
In C#, Based on Receipt Version 3, add input validation with regular expression.
1. Item name filed. -- Allow multiple words to be entered -- Uppercase/lowercase letters allowded -- 0 to 9 allowed -- White space, (), &, _, -, ., + allowed
2. Quantity field. -- 0 to 9 allowed
3. Price field. -- 0 to 9 allowed -- . allowed
--------------------------------------------------------------------------------------------------
Reciept Version 3 Below
--------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReceiptVersion3
{
class Program
{
static void Main(string[] args)
{
//declare variable to hold count
int count = 0;
//declare a 2d array
string[,] items = new string[100, 4];
//variable that controls the loop
bool repeat = true;
//variable to hold item name
string name;
//variables to hold price,subtotal, tax and total
decimal price, subtotal = 0.0M, tax, total;
//variable to hold quantity
int quantity;
//do-while loop that reads items from user until user enters 0
do
{
//run a loop to read the items
for (int i = 0; i
{
//read the item name
Console.Write(" Item Name (enter 0 to stop): ");
name = Console.ReadLine();
//if user entered 0 exit the loop by setting repeat to false
if (name.Trim() == "0")
{
repeat = false;
break;
}
//else continue read the item details
else
{
//read the price of the item
Console.Write(" Item Price: $");
//convert it to decimal by removing $ from the user input
price = Convert.ToDecimal(Console.ReadLine().Replace('$', ' ').Trim());
//prompt and read the quantity
Console.Write(" Quantity: ");
quantity = Convert.ToInt32(Console.ReadLine());
//compute subtotal
subtotal = price * quantity;
//store the item details int the array
items[i, 0] = name;
items[i, 1] = price.ToString();
items[i, 2] = quantity.ToString();
items[i, 3] = subtotal.ToString();
count++; //increment count
}
}
} while (repeat);
//loop through the items array and compute the sub total
subtotal = 0.0M;
for (int i = 0; i
{
subtotal = subtotal + Convert.ToDecimal(items[i, 3]);
}
//calculate the tax
tax = subtotal * Convert.ToDecimal(0.065);
//calculate the total
total = subtotal + tax;
//add $ symbol for price and subtotal in the array
for (int i = 0; i
{
items[i, 1] = "$" + items[i, 1];
items[i, 3] = "$" + items[i, 3];
}
//print the receipt
Console.WriteLine(" Your Receipt ");
//print the header
Console.WriteLine("Item\t\tPrice\t Quantity\t Subtotal ");
//loop through the array and print the items
for (int i = 0; i
{
for (int j = 0; j
{
//{0,-15} is used to left align and fix a length of 15, 0 represents the index of the output parameter
Console.Write("{0,-15}", items[i, j]);
}
Console.WriteLine();
}
//print the subtotal
Console.WriteLine(" Subtotal : $" + subtotal.ToString("0.00"));
//print the tax
Console.WriteLine(" Tax(0.065%) : $" + tax.ToString("0.00"));
//print the total
Console.WriteLine(" Total : $" + total.ToString("0.00"));
//terminate the program
Console.Write(" Press any key to exit...");
Console.ReadKey();
}
}
}
--------------------------------------------------------------------------------------------------
Output Should Look Like this down below
--------------------------------------------------------------------------------------------------
Scan Items Now Item Name (en r e to stop) sadfsd dsaf nter Quantity: 3 nter Price: 33.33 Item Name (enter to stop) asdfee1 nter Quantity: 2 nter Price: 3.3 Item Name (enter to stop): asf sadf-11 nter Quantity: 1 nter Price: 3 Item Name(enter to stop): Your Receipt Price Quantity Subtotal Item sadfsd dsaf 33.33 99.99
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started