C# Cognizant Handson Solutions

Product Details C# Cognizant Handson Solution

Scenario

Avenue Mart has purchased products for their company. The supplier has provided the details of the purchased products in the form of a csv file . The SoftTech wants to generate a report on the purchased product details. Given the product details in a csv file, write a program to display the product details from the file.

Implementation

Create Product class with the following member variables

Data TypeVariable Name
string_productName
string_serialNumber
DateTime_purchaseDate
double_purchaseCost

Create 4 argument constructor with the following argument _productName, _serialNumber, _purchaseDate, _purchaseCost and override ToString() method. It must return product details in the specified format.

Create Program class.

Read the input file using StreamReader.

Read the lines from the input file and create product object by parsing the input. Add all the product object to the ArrayList.

Display all the objects in the array list in the specified format given below.

Input and Output Format:

The input file name must be “input.csv“.

The input file consists of a set of product details in comma separated format in the order Product name, Serial Number, Purchase Date, Purchase Cost.

The Purchase date must be in (dd-mm-yyyy) format.

Use String.Format(“{0,-15}{1,-15}{2,-15}{3,-15}”, “Product Name”, “Serial Number”, “Purchase Date”, “Purchase Cost”) to format the output.

Sample Output:

Product Name      Serial Number            Purchase Date        Purchase Cost

HairTrimmer            HT123                     10-02-2017                    800

Steel Box                 SB231                     11-04-2018                   250

Rope                        RP240                     13-05-2019                   100

Program.cs

using System;
using System.Collections;
using System.Globalization;
using System.IO;


public class Program  //DO NOT CHANGE the name of class 'Program'
{
    public static void Main(string[] args) //DO NOT CHANGE 'Main' Signature
    {

        //Fill the code here
        StreamReader reader = new StreamReader("input.csv");
        string data = "";
        Product p = null;
        ArrayList arlobj = new ArrayList();
        while ((data = reader.ReadLine()) != null)
        {
            string[] info = data.Split(',');
            string productName = info[0].Trim();
            string serialNumber = info[1].Trim();
            DateTime purchaseDate = DateTime.ParseExact(info[2].Trim(), "dd-MM-yyyy", null);
            double cost = Double.Parse(info[3].Trim());
            p = new Product(productName, serialNumber, purchaseDate, cost);
            arlobj.Add(p);

        }
        Console.WriteLine("{0}", String.Format("{0,-15}{1,-15}{2,-15}{3,-15}", "Product Name", "Serial Number", "Purchase Date", "Cost"));
        foreach (var obj in arlobj)
        {
            Product prd = (Product)obj;
            Console.WriteLine("{0}", prd.ToString());
        }
    }

}

Product.cs

using System;
using System.Collections.Generic;
using System.Text;

public class Product
{
    public string _productName;
    public string _serialNumber;
    public DateTime _purchaseDate;
    public double _cost;

    // Implement 4-Argument Constructor
    public Product(string _productName, string _serialNumber, DateTime _purchaseDate, double _cost)
    {
        this._productName = _productName;
        this._serialNumber = _serialNumber;
        this._purchaseDate = _purchaseDate;
        this._cost = _cost;
    }

    // Implement Properties
    public string ProductName
    {
        set { this._productName = value; }
        get { return this._productName; }
    }
    public string SerialNumber
    {
        set { this._serialNumber = value; }
        get { return this._serialNumber; }
    }
    public DateTime PurchaseDate
    {
        set { this._purchaseDate = value; }
        get { return this._purchaseDate; }
    }
    public double Cost
    {
        set { this._cost = value; }
        get { return this._cost; }
    }
    public override string ToString()
    {
        return String.Format("{0,-15}{1,-15}{2,-15}{3,-15}", ProductName, SerialNumber, String.Format("{0:d}", PurchaseDate), Cost);
    }
}

Similar Posts