Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Cathleen L.

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

bool EndTrans = true; //set EndTrans to true


do //do while loop
{
Console.WriteLine("\n\t-----Here are the options-----");
Console.WriteLine(" \t---Press 1 - Add Product, 2 - Search Product, 3 - Display All Product, 4 - Sales
Transaction---");

try //try and catch exception


{
Console.Write("\tChoose: ");
int option = Convert.ToInt32(Console.ReadLine()); //users option

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
}

switch (option) //switch case statement


{
case 1:
Console.WriteLine("\n\t-----Add Product-----");
productlist.AddProduct(); //call the method in the class ProductManager

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

var Product_found = productlist.FindProductById(id); //call the FindProductById method by using the


object productlist of the class ProductManager
//and assign it to Product_found
if (Product_found != null) // if product is found
{
Console.Write($"\n\tProduct Found: {Product_found.ProductName}\n"); //then, Output the
ProductName
}

else //otherwise product is not found


{
Console.WriteLine("\n\tProduct not Found!");
}

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

do //do while loop to allow users to products to add in his sales


{
Console.Write("\n\tEnter Product ID to add to sale: "); //ask the user to enter a product to add to sale
int findProduct = Convert.ToInt32(Console.ReadLine()); //users input, and store it in the variable
findProduct

Product addProduct = productlist.FindProductById(findProduct); //use the FindProductById method


by the object of ProductManager,
//find the product that the user entered
//then store it on a variable called addProduct, temporarily a
product
//to add to sales

if (addProduct != null) //if is not null


{
sales.AddProductToSale(addProduct); //then add product to sale
Console.WriteLine($"\tProduct '{addProduct.ProductName}' added to sale."); //to display that the
product is added to sale
Console.Write("\n\tEnter True to end Sales Transaction or Enter any keys to Continue: ");
string choice = "True"; // initialize a variable called choice

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
}

else //otherwise product is not found


{
Console.WriteLine("\n\tProduct not found.");
}

} while (!SalesEnd); //if SalesEnd is true then terminate the do while loop moving on to the next line of
codes

sales.DisplayProductsInSale(); //which is displaying all the Product in Sale by calling the


DisplayProductsInSale using the sales object
sales.CompleteSale(); //also display to the user that the sale is complete and output the totalcost of the
productsInSale
break; //terminates the switch case no. 4

}
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!");
}

} while (EndTrans == false);


Console.ReadLine();

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()
{

try //try catch exception to


{

Console.Write("\tProduct ID: ");


int id = Convert.ToInt32(Console.ReadLine()); //input id

if(id < 0) //throw excewption if id id a negative number it is not allowed


{
throw new ArgumentOutOfRangeException();
}

Console.Write("\tProduct Name: ");


string name = Console.ReadLine(); //input name
Console.Write("\tProduct Price: ");
double price = Convert.ToDouble(Console.ReadLine()); //input price

if (price < 0) //throw excewption if id price a negative number it is not allowed


{
throw new ArgumentOutOfRangeException();
}

Product newProduct = new Product() //create an object Product clas


{
ProductId = id,
ProductName = name,
ProductPrice = price
};
products.Add(newProduct); //add newProduct to products
}
catch (FormatException)
{
Console.WriteLine("\n\tError! Enter a number!");
AddProduct(); //try again
}
catch (ArgumentOutOfRangeException)
{
Console.WriteLine("\n\tError! Enter a Positive Number! ");
AddProduct(); //try again
}
}

public void DisplayProducts()


{
foreach (Product product in products) //loop each of the detils on products and then output each details
{
Console.WriteLine("\n\t-----Product Details-----");
Console.WriteLine($"\tID: {product.ProductId}");
Console.WriteLine($"\tName: {product.ProductName}");
Console.WriteLine($"\tPrice: {product.ProductPrice}");
}
}
public Product FindProductById(int id)
{

foreach (Product product in products) //loop through each item in products


{
if (product.ProductId == id)//it they are equal
{
return product; // return the product if found
}
}
return null; // return null if no product found with the given id
}
}
}
Sales.cs

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
}

return totalCost; // return the totalCost


}
public void DisplayProductsInSale()
{
Console.WriteLine("\n\t-----Transaction-----");

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
}
}
}

You might also like