how do I run this type of program on Using C# and Virtual Studio 2015 . when I o
ID: 3748025 • Letter: H
Question
how do I run this type of program on Using C# and Virtual Studio 2015 . when I open up new project do i select class libray? console application? Which one should I select to make an code like one below run?
Create a “class” called Insurance that instantiates an employee from the Employee class an Employee class (that you also create) and 2 instance variables.
The Employee class should contain the name, address, and ID number.
The Insurance class will have instance variables the “agent”, are the number of “customers”, and “state” where local insurance headquarters are located. Use the correct datatype, and in the comments of your program state why you choose the data type you did.
Example:
// I choose “int” for customers since you cannot have half ( .5 ) of a customer
int iCustomers = 8;
Create a constructor that initializes the class members. The Insurance class must contain a C# property for each instance variable for get and set. These are not java access and mutators. The property for number should verify that the number of Insurance customers is greater than zero. If a negative value is passed in, the set accessor should set the Insurance number to 0.
Create a another class InsuranceTest (this will be the driver class in Java parlance) that prompts the user for information to create two Insurance objects using the constructor that you developed for the Insurance class. Display agent information, number of customers, local state.
Explanation / Answer
Start a new project in Visual Studio and chose console application. Name your project and save this project at your desired project, once you create the project you will that program.cs file is opened by default. Copy and paste the below code in that Program.cs file and run the program by clicking start button...
Copy the colplete code below...
using System;
namespace Insurance
{
class InsuranceTest
{
static void Main(string[] args)
{
Insurance insurance1 = new Insurance();// Creating instance for first insurance
Insurance insurance2 = new Insurance();// Creating instance for second insurance
// Reading data for first Insurance
Console.WriteLine("Please Enter the name of first Insurance Agent: ");
insurance1.Agent.Name = Console.ReadLine();
Console.WriteLine("Please Enter the address of first Insurance Agent: ");
insurance1.Agent.Address = Console.ReadLine();
Console.WriteLine("Please Enter the Id of first Insurance Agent: ");
insurance1.Agent.Id = Console.ReadLine();
Console.WriteLine("Please Enter the number of customers for first Insurance: ");
insurance1.NumberOfCustomers = Convert.ToInt32(Console.ReadLine());//Since we are reading number as string we need to conver it to int using convert.ToInt32 function
Console.WriteLine("Please Enter the state of first Insurance: ");
insurance1.State = Console.ReadLine();
// Reading data for second Insurance
Console.WriteLine("Please Enter the name of second Insurance Agent: ");
insurance2.Agent.Name = Console.ReadLine();
Console.WriteLine("Please Enter the address of second Insurance Agent: ");
insurance2.Agent.Address = Console.ReadLine();
Console.WriteLine("Please Enter the Id of second Insurance Agent: ");
insurance2.Agent.Id = Console.ReadLine();
Console.WriteLine("Please Enter the number of customers for second Insurance: ");
insurance2.NumberOfCustomers = Convert.ToInt32(Console.ReadLine());//Since we are reading number as string we need to conver it to int using convert.ToInt32 function
Console.WriteLine("Please Enter the state of second Insurance : ");
insurance2.State = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("*************************************************");
Console.WriteLine();
//Printing data for first insurance
Console.WriteLine("Printing data for first Insurance");
Console.WriteLine( "Agent Name : "+ insurance1.Agent.Name);
Console.WriteLine("Agent Address : " + insurance1.Agent.Address);
Console.WriteLine("Agent Id : " + insurance1.Agent.Id);
Console.WriteLine("Total Number of Customers : " + insurance1.NumberOfCustomers);
Console.WriteLine("Local State : " + insurance1.State);
Console.WriteLine();
Console.WriteLine("Printing data for second Insurance");
Console.WriteLine("Agent Name : " + insurance2.Agent.Name);
Console.WriteLine("Agent Address : " + insurance2.Agent.Address);
Console.WriteLine("Agent Id : " + insurance2.Agent.Id);
Console.WriteLine("Total Number of Customers : " + insurance2.NumberOfCustomers);
Console.WriteLine("Local State : " + insurance2.State);
Console.ReadLine();//To stop the console from disappearing
}
}
public class Insurance
{
public Insurance()
{
Agent = new Employee();
NumberOfCustomers = 0;
State = "";
}
//Agent is of emloyee type... beacaue each Insurance is under some agent employee
public Employee Agent { get; set; }
//number of customers cannot be fractional so I have taken it as int
private int numberOfCustomers;
public int NumberOfCustomers
{
get
{
return numberOfCustomers;
}
set
{
if (numberOfCustomers != value)
{
numberOfCustomers = value;
if (numberOfCustomers < 0) //negative value passed will be set to 0
numberOfCustomers = 0;
}
}
}
private string state;
public string State
{
get
{
return state;
}
set
{
if (state != value)
state = value;
}
}
}
public class Employee
{
public Employee()
{
}
// This is a c# property with get and set accessor implemented. We can also write as -- > public string Name { get; set; } this is equilavalent to the below written Name property the compiler creates the private filed name and implement the set and get accessor internally...Please refer Insuarance class where I have created Agent property with shorcut...
private string name;
public string Name
{
get
{
return name;
}
set
{
if (name != value)
name = value;
}
}
private string address;
public string Address
{
get
{
return address;
}
set
{
if (address != value)
address = value;
}
}
//id can also be taken as int, I have chosen it as string so that Id like 'ABC123' can also be used...
private string id;
public string Id
{
get
{
return id;
}
set
{
if (id != value)
id = value;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.