For this assignment you will be creating an array of objects or a list of objects of car parts based on user input. 1. Create a Parts class with the following items a. Properties for PartNum, Part Name, Part Description, and Cost b. Must have a constructor 2. In the main you will need accomplish the following: a. Ask the user how many objects they wish to enter b. Create an array of parts objects c. Loop appropriately to collect the data needed to populate the objects in the array d. Next you will do the following: i. Give the user a menu of all parts ii. Ask the user which part they would like to view data on iii. Create a method to print out the object selected by user values.

Respuesta :

Answer:

C# code for the problem is given below

Explanation:

Program:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace PartsDemo

{

class Parts

{

int partNum;

string partName, partDescription;

double cost;

public Parts(int no,string name,string desc,double cst)

{

partNum = no;

partName = name;

partDescription = desc;

cost = cst;

}

public void print()

{

Console.WriteLine("Part Number: "+partNum );

Console.WriteLine("Part Name: "+partName );

Console.WriteLine("Part Description: "+partDescription );

Console.WriteLine("Cost: "+cost);

}

}

class Program

{

static void Main(string[] args)

{

int numObjects;

Console.Write("How many objects you wish to enter: ");

numObjects = Convert.ToInt32(Console.ReadLine());

//array of objects

Parts[] parts = new Parts[numObjects];

int no;

string name, desc;

double cst;

//entering details of parts from user

for (int i = 0; i < numObjects; i++)

{

Console.Write("Enter part no: ");

no = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter part name: ");

name = Console.ReadLine();

Console.Write("Enter description: ");

desc = Console.ReadLine();

Console.Write("Enter cost: ");

cst = Convert.ToDouble(Console.ReadLine());

parts[i] = new Parts(no,name,desc,cst);

}

Console.WriteLine("\nMenu options: ");

Console.WriteLine("------------------");

for (int i = 0; i < numObjects; i++)

{

Console.WriteLine(i+". Part"+(i+1));

}

//view the selected part by entering its index number

int index;

Console.Write("\nPlease enter the index of a part to view data: ");

index = Convert.ToInt32(Console.ReadLine());

parts[index].print();

Console.ReadKey();

}

}

}