Professional Documents
Culture Documents
M6.Act2 Abila
M6.Act2 Abila
Abila BSIT-2
Screenshoot (Code)
Program.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace M6.Act2___OOp
{
internal class Program
{
static void Main(string[] args)
{
ProductManager productlist = new ProductManager(); //create an object of the class ProductManager
if((option < 1 || option > 4)) // throw exception if the user enters a negative number or not in the number
choices
{
throw new ArgumentOutOfRangeException(); //throw statement
}
break;
case 2:
Console.WriteLine("\n\t-----Search Product-----");
Console.Write("\n\tEnter Product ID: ");
int id = Convert.ToInt32(Console.ReadLine()); //enter a product id that need to be search
break;
case 3:
Console.WriteLine("\n\t-----Display All Product-----");
productlist.DisplayProducts(); //call the DisplayProducts method by using the object productlist of the
class ProductManager
break;
case 4:
Console.WriteLine("\n\t-----Sales Transaction-----");
ProductSale sales = new ProductSale(); //object of the class ProductSale where you can store the sales
bool SalesEnd = false; //set SalesEnd to false for now
string input = Console.ReadLine(); // user's input whether he/she will end Sales Transaction or
not
if (input == choice) //if the user enter 'True' set SalesEnd to true, then the do while loop terminates
{
SalesEnd = true;//and fulfilled the while condition below, then it asks the user if he/she will
End All Transaction?
//True or False
}
} while (!SalesEnd); //if SalesEnd is true then terminate the do while loop moving on to the next line of
codes
}
Console.WriteLine("\n\t-----End All Transaction? True or False-----");
Console.Write("\n\tChoose: ");
EndTrans = Convert.ToBoolean(Console.ReadLine()); //user input
}
catch (ArgumentOutOfRangeException)
{
Console.WriteLine("\n\tPick Number from 1 - 4!");
EndTrans = false; // to try again
}
catch (FormatException)
{
Console.WriteLine("\n\tError! Input a number!");
}
Console.ReadKey();
}
}
Product.cs
Code:
using M6.Act2___OOp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace M6.Act2___OOp
{
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public double ProductPrice { get; set; }
}
public class ProductManager
{
private List<Product> products = new List<Product>();
public void AddProduct()
{
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace M6.Act2___OOp
{
public class ProductSale
{
private List<Product> productsInSale = new List<Product>(); // an object
public void AddProductToSale(Product product)
{
productsInSale.Add(product); //add product to productsInSale
}
public double CalculateTotalCost() //calculate the sum of the products
{
double totalCost = 0;
foreach (var product in productsInSale) //loop through each product in the productsInSale
{
totalCost += product.ProductPrice; //add the price
}
foreach (Product product in productsInSale) //loop through each item in productsInSale to display the items and
its details
{
Console.WriteLine($"\tID: {product.ProductId}");
Console.WriteLine($"\tName:{product.ProductName}");
Console.WriteLine($"\tPrice:{ product.ProductPrice}");
}
}
public void CompleteSale() //a method to display to the user that the sales transacttion has been ended and it's
complete
// and it outputs the totalCost of the productsInSale
{
Console.WriteLine("\n\tThe sale has been completed. The total cost is: " + CalculateTotalCost());
productsInSale.Clear(); // Clear the list of products in the sale
}
}
}