Working with struct data type and arrays:
Use C# 6.0 and 7.0 features, wherever applicable
Write a program to define a two dimensional array of numbers to store 5 rows and 6 columns. Write a code to accept the data, assign it in array, and print the data entered by the user.
Define a single dimension array of strings to hold the name of City. Accept some values from the user and store them in the array. Use foreach loop to print all the data of the array.
Create a class named ProductDemo which accepts the details of the product, converts the details into reference types using boxing and displays them by converting them into their relevant types using unboxing and calculate the amountPayable. Refer the class diagram given below.
Output:
Enter the id of product : 101
Enter the name of the product : Segate HDD
Enter Price : 9000
Enter quantity : 2
Product Details :
Product id : 101
Product name : Segate HDD
Price : 9000
Quantity : 2
Amt Payable : 18000.00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C_SHARP
{
class Program
{
class ProductDemo
{
private object id;
private object name;
private object price;
private object quantity;
public ProductDemo() { }
public ProductDemo(int id, string name, double price, int quantity)
{
// boxing
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
/// <summary>
/// Displays Product Details
/// </summary>
public void displayProductDetails() {
//using unboxing and calculate the amountPayable
double amtPayable = (double)price * (int)quantity;
Console.WriteLine("Product Details: ");
Console.WriteLine("Product id: {0}",id);
Console.WriteLine("Product name: {0}",name);
Console.WriteLine("Product Price: {0}",price);
Console.WriteLine("Product Quantity: {0}",quantity);
Console.WriteLine("Product Amt Payable: {0}",amtPayable.ToString("N"));
}
}
static void Main(string[] args)
{
//two dimensional array of numbers to store 5 rows and 6 columns.
string [,]values=new string[5,6];
//accept the data, assign it in array
for(int i=0;i<5;i++){
for (int j = 0; j < 6; j++) {
Console.Write("Enter value[{0},{1}]: ",i,j);
values[i,j]=Console.ReadLine();
}
}
//print the data entered by the user.
Console.WriteLine("The values are: ");
for (int i = 0; i < 5; i++){
for (int j = 0; j < 6; j++){
Console.WriteLine("The value[{0},{1}]: is {2}", i, j,values[i,j]);
}
}
//Define a single dimension array of strings to hold the name of City.
string []cites=new string[5];
//Accept some values from the user and store them in the array.
Console.WriteLine(Environment.NewLine);
for (int i = 0; i < 5; i++) {
Console.Write("Enter city {0}: ",(i+1));
cites[i] = Console.ReadLine();
}
//Use foreach loop to print all the data of the array.
Console.WriteLine(Environment.NewLine+"All cities are:");
foreach (var c in cites) {
Console.WriteLine("{0}", c);
}
Console.Write("Enter the id of product: ");
int id=int.Parse(Console.ReadLine());
Console.Write("Enter the name of product: ");
string name=Console.ReadLine();
Console.Write("Enter the price of product: ");
double price=double.Parse(Console.ReadLine());
Console.Write("Enter the quantity of product: ");
int quantity=int.Parse(Console.ReadLine());
ProductDemo productDemo=new ProductDemo(id,name,price,quantity);
productDemo.displayProductDetails();
Console.ReadLine();
}
}
}
Comments
Leave a comment