What am i doing wrong? I am trying to have my program read the data from a csv file but i keep getting this unhandled exception, is it because of how i wrote the txt file? the exception?
First I tried to have the data be displayed as a list in the console, but that did not happened either. I cant seem to be able to do that here or even get it right to read from my txt file.
//My first program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PracticeProgram
{
public class Program
{
public static bool W2 { get; private set; }
public static void Main(string[] args)
{
List<Person> myContacts = new List<Person>();
// Loop to collect developers data
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Welcome developer, enter your name to continue");
string name = Console.ReadLine();
Console.WriteLine("Hello, " + name);
Console.WriteLine("enter your address");
string address = Console.ReadLine();
Console.WriteLine(address);
Console.WriteLine("enter your monthly income");
double GrossMonthlyPay = 5000;
Console.WriteLine(GrossMonthlyPay);
Console.ReadLine();
Console.WriteLine("your tax deduction is set at 7%");
double taxes = (0.07);
Console.WriteLine(taxes);
Console.ReadLine();
Console.WriteLine("your monthly tax deduction is ");
Console.WriteLine(taxes = GrossMonthlyPay * taxes);
Console.ReadLine();
Console.WriteLine("Based on your monthly income your Annual gross pay is ");
double AnnualGrossPay = GrossMonthlyPay * 12;
Console.WriteLine(AnnualGrossPay);
Console.ReadLine();
Console.WriteLine("Your annual taxes are ");
double AnnualTaxes = AnnualGrossPay * 0.07;
Console.WriteLine(AnnualTaxes);
Console.ReadLine();
Console.WriteLine("Select 1 for W2, select 2 for 1099");
bool A = W2;
Console.WriteLine();
Console.ReadLine();
if (A)
{
Console.WriteLine("You employment type is W2");
}
else
{
Console.WriteLine("Your employment type is 1099");
}
Console.Write("\nPress any key to continue...");
Console.ReadLine();
myContacts.Add(new Person(name, address, GrossMonthlyPay, taxes, AnnualGrossPay, AnnualTaxes));
}
// Or you could read from CSV or any other source
foreach (string line in System.IO.File.ReadLines(#"C:\Users\Owner\Documents\Visual Studio 2015\Projects\new\data.txt"))
{
myContacts.Add(Person.ReadFromCSV(line));
}
// Serialize your informations at the end
foreach (Person contact in myContacts)
{
contact.SerializeToCSV(#"C:\Users\Owner\Documents\Visual Studio 2015\Projects\new\data.txt");
}
}
}
internal class Person
{
private string address;
private double grossMonthlyPay;
private string name;
private double taxes;
private double annualGrossPay;
private double annualTaxes;
public Person(string name, string address, double grossMonthlyPay, double taxes)
{
this.name = name;
this.address = address;
this.grossMonthlyPay = grossMonthlyPay;
this.taxes = taxes;
}
public Person(string name, string address, double grossMonthlyPay, double taxes, double annualGrossPay, double annualTaxes) : this(name, address, grossMonthlyPay, taxes)
{
this.annualGrossPay = annualGrossPay;
this.annualTaxes = annualTaxes;
}
public override string ToString()
{
return "Name=" + this.name + ",Address =" + this.address + " Monthly Pay=" + this.grossMonthlyPay + " Taxes=" + this.taxes.ToString();
}
internal static Person ReadFromCSV(string line)
{
throw new NotImplementedException();
}
internal void SerializeToCSV(string v)
{
throw new NotImplementedException();
}
}
}
this is the error
Your function "ReadFromCSV" implement a
throw new NotImplementedException();
try to delete it or implement with the right code
Related
Is there a way to get rid of practise.AcademicProgram in the output when the display() is called for an object initialized by the parameterized constructor. In my code below this occurs when s2.Display(); is executed.
Below is my working code for the same:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace practise
{
public class AcademicProgram
{
//Memnber variables
private int programCode;
private string programName;
private int programCredits;
//Properties for reading and writting the values of private fields
public int ProgramCode { get => programCode; set => programCode = value; }
public string ProgramName { get => programName; set => programName = value; }
public int ProgramCredits { get => programCredits; set => programCredits = value; }
//Default Constructor
public AcademicProgram()
{
}
//Parameterized Constructor
public AcademicProgram (int programCode, string programName, int programCredits)
{
this.programCode = programCode;
this.programName = programName;
this.programCredits = programCredits;
}
public void DisplayResults()
{
Console.WriteLine("Program Code: {0}\nProgram Name: {1}\nProgram Credits: {2}\n", programCode, programName, programCredits);
Console.WriteLine();
}
}
public class Instructor
{
//Member variables
public string forename;
public string surname;
public int identificationNumber;
public AcademicProgram academicProgram;
//Default Constructor
public Instructor()
{
this.forename = string.Empty;
this.surname = string.Empty;
this.identificationNumber = 0;
this.academicProgram = null;
}
//Parameterized Constructor
public Instructor(string forename, string surname, int identificationNumber, AcademicProgram academicProgram)
{
this.forename = forename;
this.surname = surname;
this.identificationNumber = identificationNumber;
this.academicProgram = academicProgram;
}
// Member Function to display values of member variables on Console.
public void Display()
{
Console.WriteLine(this.forename + ", " + this.surname + ", " + this.identificationNumber + ", " + this.academicProgram);
Console.WriteLine();
}
//Driver function
static void Main(string[] args)
{
//Instantiating object to call non-static member method
Instructor p = new Instructor();
p.Go();
Console.ReadKey();
}
//Non-static Member function
public void Go()
{
//Instantiating object without passing any values on runtime.
Instructor s = new Instructor();
//Instantiating object of AcademicProgram class without passing any values on runtime.
AcademicProgram progName = new AcademicProgram ();
//Set the values of fields using properties
progName.ProgramCode = 8230;
progName.ProgramName = "Systems Development: Cocnepts and Analysis";
progName.ProgramCredits = 4;
// Instantiating object while providing values on runtime.
Instructor s2 = new Instructor("Eddie ", "Jessup", 2394589, progName);
//Call to display method
s.Display();
s2.Display();
progName.DisplayResults();
}
}
}
Which gives an output shown here
Aside from override ToString(), I was able to fix it by a simple fix in display() of Instructor class as below:
public void Display()
{
Console.WriteLine(this.forename + ", " + this.surname + ", " + this.identificationNumber + ", " + this.academicProgram.ProgramCode + ", " + this.academicProgram.ProgramName + ", " + this.academicProgram.ProgramCredits);
Console.WriteLine();
}
I have a problem with my class where it displays a comma before it displays the string and I cant find a way to take the comma out in the front and keep it in all the other places
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Carnival
{
class Player
{
public string Name { get; set; }
public double SpendingMoney { get; set; }
public string PrizesWon { get; set; }
//constructors
public Player( string n, double sp)
{
Name = n;
SpendingMoney = sp;
PrizesWon = "";
}
//methods
public string Play(GameBooth aGames)
{
string newPrize;
if (SpendingMoney >= aGames.Cost)
{
newPrize = aGames.Start();
//here is what I need to fix
PrizesWon = "," + newPrize + PrizesWon ;
return newPrize;
}
else
{
return "error no money fool";
}
}
}
}
here is the main code if you need it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Carnival
{
class Program
{
static void Main(string[] args)
{
//local variables
string name;
double money;
int choice = 0;
string newPrize;
//Game objects.
GameBooth balloonDartToss = new GameBooth(2, "tiger plush", "sticker");
GameBooth ringToss = new GameBooth(2, "bear keychain", "pencil");
GameBooth breakAPlate = new GameBooth(1.5, "pig plush", "plastic dinosaur");
Console.ForegroundColor = ConsoleColor.Cyan;
//asking player name
Console.Write("Welcome to the Carnival. What is your name? ");
name = Console.ReadLine();
//asking how much spending money the player has
Console.Write("How much money do you have? ");
money = Convert.ToDouble(Console.ReadLine());
Console.ResetColor();
//Creating player object.
Player Gambler = new Player(name, money);
//main program loop
while (choice != 4)
{
//print menu. Check out the local method below
printMenu();
//retrieve user choice. See the local method below
choice = getChoice();
//main switch. User Choices:
// 1 - we play Baloon Darts Toss and show prize
// 2 - we play ring Toss and show prize
// 3 - we play Break a Plate and show prize
// 4 - Show users all his prizes
switch (choice)
{
case 1:
newPrize = Gambler.Play(balloonDartToss);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(newPrize);
break;
case 2:
newPrize = Gambler.Play(ringToss);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(newPrize);
break;
case 3:
newPrize = Gambler.Play(breakAPlate);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(newPrize);
break;
case 4:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("{0}, here is your prize list: {1}", Gambler.Name, Gambler.PrizesWon);
Console.ResetColor();
break;
}//end switch
}//end while
//next line simply pauses the program until you hit Enter.
Console.ReadLine();
}//end main
//this method prints the main menu
public static void printMenu()
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine();
Console.WriteLine("Select the game you would like to play:");
Console.WriteLine("1. Balloon Dart Toss");
Console.WriteLine("2. Ring Toss");
Console.WriteLine("3. Break a Plate");
Console.WriteLine("4. Get Prizes and Leave Carnival");
Console.Write("Enter Your Choice: ");
Console.ResetColor();
}//end printMenu
//this methods accepts user input 1-4
public static int getChoice()
{
Console.ForegroundColor = ConsoleColor.Yellow;
int input = 0;
do
{
Console.ForegroundColor = ConsoleColor.Yellow;
input = Convert.ToInt32(Console.ReadLine());
if (input < 1 || input > 4)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Out of range. Input 1-4 only");
Console.Write("Enter your choice: ");
Console.ResetColor();
}
} while (input < 1 || input > 4);
Console.ResetColor();
return input;
}
}
}
here is my other class if you need that too but you probably don't
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Carnival
{
class GameBooth
{
//properties
public double Cost { get; set; }
public string FirstPrize { get; set; }
public string ConsolationPrize { get; set; }
public int w { get; set; }
public int l { get; set; }
//constructors
public GameBooth(double c, string fp, string cp)
{
Cost = c;
FirstPrize = fp;
ConsolationPrize = cp;
}
//methods
public string Start()
{
Random r = new Random();
w = 1;
l = 1;
for (int i = 1; i < 3; i++)
{
int randomChoice = r.Next(0, 400);
if ( randomChoice == 1)
{
w++;
}
}
if (w == 3)
{
return FirstPrize;
}
else
{
return ConsolationPrize;
}
}
}
}
Update the line to:
PrizesWon = string.IsNullOrEmpty(PrizesWon) ? newPrize : newPrize + "," + PrizesWon;
This uses the conditional operator to return a different result depending on whether or not PrizesWon already has something in it.
In your code you had put (,) before newPrize. For solving this, you have to just put it after PrizesWon variable and it will look like this :
**PrizesWon = newPrize + PrizesWon + ",";**
I hope your problem will be solved by this.
Cant't figure out what I am doing wrong,want to display user name in console through a method in the same class,missing a minor syntax please have a look and guide me.
public class Program
{
string name;
public void GetName()
{
Console.WriteLine("Enter ur name");
name = Console.ReadLine();
Console.WriteLine("Name is",name);
Console.ReadLine();
}
static void Main()
{
Program p = new Program();
p.GetName();
Console.ReadLine();
}
}
You need to specify the format string try:
public class Program
{
string name;
public void GetName()
{
Console.WriteLine("Enter ur name");
name = Console.ReadLine();
Console.WriteLine("Name is {0}",name);
Console.ReadLine();
}
static void Main()
{
Program p = new Program();
p.GetName();
Console.ReadLine();
}
}
You can also do it like this
Console.Writeline("name is:" + name);
Console.WriteLine("Name is : " + name);
So I've been looking into this for what feel like forever and can not figure out what I'm doing wrong. here are excerpts from my code where I want the string CashierLOgInNName's value to be the new Cashier objects name:
public class Cashiers
{
public int CashierID;
public int Password;
public string FirstName;
public string LastName;
public void SetCashiers(int CashierID, int Password,string FirstName, string LastName )
{
this.CashierID = CashierID;
this.Password = Password;
this.FirstName=FirstName;
this.LastName = LastName;
}
Console.WriteLine("enter New Log in name");
string CashierLOgInNName= Console.ReadLine();
Console.WriteLine("enter First Name");
string CashierFirstName = Console.ReadLine();
Console.WriteLine("enter Last name");
string CashierLastName = Console.ReadLine();
Console.WriteLine("enter Cashier ID");
string f = Console.ReadLine();
int NewCashierID = Int32.Parse(f);
Console.WriteLine("enter Cashier Password");
string g = Console.ReadLine();
int NewCashierPWD = Int32.Parse(g);
CashierLOgInNName = (Cashiers)Activator.CreateInstance(null, CashierLOgInNName).Unwrap();
First a little correction. It will make sure the Cashier's Object cashierObj is created.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Activators
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter New Log in name");
string CashierLOgInNName = Console.ReadLine();
Console.WriteLine("enter First Name");
string CashierFirstName = Console.ReadLine();
Console.WriteLine("enter Last name");
string CashierLastName = Console.ReadLine();
Console.WriteLine("enter Cashier ID");
string f = Console.ReadLine();
int NewCashierID = Int32.Parse(f);
Console.WriteLine("enter Cashier Password");
string g = Console.ReadLine();
Cashiers cashierObj = (Cashiers)Activator.CreateInstance(typeof(Cashiers), f, g, CashierFirstName, CashierLastName);
}
}
public class Cashiers
{
public string CashierID;
public string Password;
public string FirstName;
public string LastName;
public Cashiers(string CashierID, string Password, string FirstName, string LastName)
{
this.CashierID = CashierID;
this.Password = Password;
this.FirstName = FirstName;
this.LastName = LastName;
}
}
}
Coming to the next part CashierLOgInNName's value to be the new Cashier objects name. This seems to be tricky and non useful. I suggest there are better alternatives :
You can define property loginName in Cashiers.
You can create a Dictionary<string, Cashier> LoginName as key and Cashier Object as value.
But still if you persist there is a way using CSharpCodeProvider Class which allows dynamically creating an assembly from source code.
In source code you can write the object name as loginName and then refer that assembly dynamically.
Yet it serves you no purpose as you will not be able to use that object as its name is known to you at run time only. I hope it helps your curiosity.
βIn a time of destruction, create something.β β Maxine Hong Kingston
The MSDN documentation of activator.createinstance and objecthandle.unwrap might be of help.
Activator.CreateInstance Method
ObjectHandle.Unwrap Method
I am working on an assignment and I am getting this error which I don't understand. I am writing a WCF client for a working service. Can anyone help me out here?
This line is throwing the error:
MyComplex sumcplx = proxy.complex_sum(one,two);
Error I get
Error: The best overloaded method match for 'NETProxyWCFClient.ProxyTypes.CalculatorClient.complex_sum(NETProxyWCFClient.ProxyTpes.MyComplex,NETProxyWCFClient.ProxyTpes.MyComplex)' has some invalid arguments
Code to my program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.ServiceModel.Description;
namespace NETProxyWCFClient
{
[DataContract]
public class MyComplex
{
int real;
[DataMember]
public int Real
{
get { return real; }
set { real = value; }
}
[DataMember]
int im;
public int Im
{
get { return im; }
set { im = value; }
}
}
[ServiceContract]
interface ICalculator
{
[OperationContract]
int mult(int a, int b);
[OperationContract]
List<int> fib(int n);
[OperationContract]
MyComplex complex_sum(MyComplex a, MyComplex b);
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter in 2 numbers");
string read = Console.ReadLine();
string[] numbers = read.Split(' ');
int m = int.Parse(numbers[0]);
int n = int.Parse(numbers[1]);
ProxyTypes.CalculatorClient proxy = new ProxyTypes.CalculatorClient();
//Multiplcation
int sum = proxy.mult(m, n);
Console.WriteLine(sum.ToString());
//Mycomplex
MyComplex one = new MyComplex();
one.Im = m;
one.Real = n;
MyComplex two = new MyComplex();
two.Im = n;
two.Real = m;
MyComplex sumcplx = proxy.complex_sum(one,two);
Console.WriteLine(sumcplx.Im + " , " + sumcplx.Real);
//fib one
int[] listM = proxy.fib(m);
foreach (int listItem in listM)
{
Console.Write(listItem.ToString() + " ");
}
Console.WriteLine("");
//fib 2
int[] listN = proxy.fib(n);
foreach (int listItem in listN)
{
Console.Write(listItem.ToString() + " ");
}
Console.ReadLine();
}
}
}
If you have Added a service reference to your test program, there is a proxt class that is generated for you, so if you have ICalculator defined explicity (code you wrote) remove it. it is already in your project, under the NETProxyWCFClient.ProxyTpes.MyComplex,NETProxyWCFClient.ProxyTpes namespace.