【发布时间】:2009-11-10 17:34:13
【问题描述】:
这是一个家庭作业问题 - 如果您能告诉我我做错了什么以及如何解决它,我将不胜感激。如何优化我的编程技术:)。谢谢!
我构建它是为了将 employee 保存在数组中 - 它会正确保存第一个,但是当它保存其他时它显示为空白 - 就在构造函数之外。为什么?
using System;
using System.IO;
class Driver
{
const int NUMBER_OF_EMPLOYEES = 10;
// Main Method
// Purpose: directs the program in what to do and loops the program while np.again != n
// Parameters: none
// Returns: nothing
// Pre-conditions: none
// Post-conditions: none
public static void Main()
{
Driver nd = new Driver();
char response = 'y';
string PathName = "empdata" + ".txt";
int counter = 0;
do
{
employee[] emps = new employee[NUMBER_OF_EMPLOYEES];
TextReader tr = new StreamReader(PathName);
do
{
emps[counter] = nd.getSaveEmpdataPrint(PathName, counter, tr );
counter++;
} while (counter != 6);
response = nd.again();
Console.Clear();
} while (response != 'n');
}
public employee getSaveEmpdataPrint(string PathName, int counter, TextReader tr)
{
employee tempEmployee = new employee();
Driver nd = new Driver();
double i = 1;
string temp = "";
do
{
temp = tr.ReadLine();
if ((i % 1) == 0.5)
i += 0.5;
try
{
if (temp != null)
{
try
{
if ((int.Parse(temp)) == 1)
{
tempEmployee.empNumber = (int)(i - 0.5);
i += 0.5;
}
}
catch (FormatException){ }
if (i == 2)
{
string fn = "";
string ln = "";
nd.SplitString(temp, ref fn , ref ln);
tempEmployee.firstName = fn;
tempEmployee.lastName = ln;
i += 0.5;
}
if (i == 3)
{
tempEmployee.adress = temp;
i += 0.5;
}
if (i == 4)
{
double temphrwage = 0;
double temphrsworked = 0;
nd.SplitDouble(temp, ref temphrwage, ref temphrsworked);
tempEmployee.hrsWorked = temphrsworked;
tempEmployee.hrlyWage = temphrwage;
i += 0.5;
}
}
}
catch (NullReferenceException)
{
Console.WriteLine("The data in the text file was imcomplete or was formated wrong.");
Console.ReadLine();
}
} while (temp != null && i != 4.5);
return tempEmployee;
}
private void PrintEmployee(employee tempEmployee)
{
Console.WriteLine("-------------------------------------");
Console.WriteLine("Employee Number: --- {0}", tempEmployee.empNumber);
Console.WriteLine(" Name: --- {0}, {1}", tempEmployee.lastName, tempEmployee.firstName);
Console.WriteLine(" Adress: --- {0}", tempEmployee.adress);
Console.WriteLine(" Hourly wage: --- {0:f2} (USD per hour)", tempEmployee.hrlyWage);
if (tempEmployee.hrsWorked == 1)
Console.WriteLine(" Hours Worked: --- 1hr ");
if (tempEmployee.hrsWorked != 1)
Console.WriteLine(" Hours Worked: --- {0:f2}hrs", tempEmployee.hrsWorked);
Console.WriteLine("-------------------------------------");
}
private void SplitString(string temp, ref string FirstName, ref string LastName)
{
char[] delimit = new char[] { ' ' };
int counter = 0;
foreach (string substr in temp.Split(delimit))
{
if (counter == 0)
FirstName = substr;
if (counter == 1)
LastName = substr;
counter++;
}
}
private void SplitDouble(string temp, ref Double a, ref Double b)
{
char[] delimit = new char[] { ' ' };
int counter = 0;
string ta = "";
string tb = "";
foreach (string substr in temp.Split(delimit))
{
if (counter == 0)
ta = substr;
if (counter == 1)
tb = substr;
counter++;
}
a = double.Parse(ta);
b = double.Parse(tb);
}
// again Method
// Purpose: asks the user if they want to run the program again
// Parameters: none
// Returns: a char ( y or n )
// Pre-conditions: none
// Post-conditions: none
public char again()
{
char response = 'y';
Console.Write("\nWould you like run again? (y or n)");
response = char.Parse(Console.ReadLine());
response = char.ToLower(response);
Console.Clear();
return response;
}
}
class employee
{
private int EmpNumber;
private string FirstName;
private string LastName;
private string Adress;
private double HrlyWage;
private double HrsWorked;
// Default Constructor
public employee()
{
EmpNumber = 0;
FirstName = "";
LastName = "";
Adress = "";
HrlyWage = 0;
HrsWorked = 0;
}
// Method empNumber
// Porpose: get and set the EmpNumber
// Pramereters: int
// Returns: a int (employee Number)
public int empNumber
{
get
{
return EmpNumber;
}
set
{
EmpNumber = value;
}
}
// Method lastName
// Porpose: get and set the name
// Pramereters: string
// Returns: a string (employee name)
public string lastName
{
get
{
return LastName;
}
set
{
LastName = value;
}
}
// Method firstName
// Porpose: get and set the first name
// Pramereters: string
// Returns: a string (employee's first name)
public string firstName
{
get
{
return FirstName;
}
set
{
FirstName = value;
}
}
// Method adress
// Porpose: get and set the adress of the employee
// Pramereters: string
// Returns: a string (employee adress)
public string adress
{
get
{
return Adress;
}
set
{
Adress = value;
}
}
// Method hrlyWage
// Porpose: get and set the Hourly Wage
// Pramereters: Double
// Returns: a double (employee's Hourly Wage)
public double hrlyWage
{
get
{
return HrlyWage;
}
set
{
HrlyWage = value;
}
}
// Method hrsWorked
// Porpose: get and set the Hours Worked
// Pramereters: Double
// Returns: a double (the number of hours the employee worked)
public double hrsWorked
{
get
{
return HrsWorked;
}
set
{
HrsWorked = value;
}
}
// Method reset
// Porpose: reset everything to zero/default
// Pramereters: none
// Returns: nothing
public void reset()
{
EmpNumber = 0;
FirstName = "";
LastName = "";
Adress = "";
HrlyWage = 0;
HrsWorked = 0;
}
// Method empNumber
// Porpose: get and set the EmpNumber
// Pramereters: int
// Returns: a int (employee Number)
public int empNumber
{
get
{
return EmpNumber;
}
set
{
EmpNumber = value;
}
}
// Method lastName
// Porpose: get and set the name
// Pramereters: string
// Returns: a string (employee name)
public string lastName
{
get
{
return LastName;
}
set
{
LastName = value;
}
}
// Method firstName
// Porpose: get and set the first name
// Pramereters: string
// Returns: a string (employee's first name)
public string firstName
{
get
{
return FirstName;
}
set
{
FirstName = value;
}
}
// Method adress
// Porpose: get and set the adress of the employee
// Pramereters: string
// Returns: a string (employee adress)
public string adress
{
get
{
return Adress;
}
set
{
Adress = value;
}
}
// Method hrlyWage
// Porpose: get and set the Hourly Wage
// Pramereters: Double
// Returns: a double (employee's Hourly Wage)
public double hrlyWage
{
get
{
return HrlyWage;
}
set
{
HrlyWage = value;
}
}
// Method hrsWorked
// Porpose: get and set the Hours Worked
// Pramereters: Double
// Returns: a double (the number of hours the employee worked)
public double hrsWorked
{
get
{
return HrsWorked;
}
set
{
HrsWorked = value;
}
}
// Method reset
// Porpose: reset everything to zero/default
// Pramereters: none
// Returns: nothing
public void reset()
{
EmpNumber = 0;
FirstName = "";
LastName = "";
Adress = "";
HrlyWage = 0;
HrsWorked = 0;
}
}
}
示例 txt 文件格式:
1
约翰·梅里天气
西大街 123 号
5.00 30
2
安德鲁·巴顿
17东江景大道
12.00 40
3
玛莎华盛顿
弗农山巷 1 号
7.25 20
...
txt 中的示例
谢谢,伙计们,很抱歉没有所有代码 :P 和 txt 文件。 :))
【问题讨论】:
-
你确定有足够的代码吗?
-
哇。从哪里开始? Wallter:您能提供一些 empdata.txt 文件中的示例吗?也许添加一些cmets? getSaveEmpdataPrint() 方法是一个野兽。
-
哇...对不起,我有点间隔为 txt 文件添加示例 :) 抱歉,谢谢您的快速反馈 :)
-
这段代码有很多问题,这只是冰山一角。它令人困惑,令人费解,而且在很多地方都没有任何意义。例如,为什么要使用令人困惑的模数来四舍五入?为什么要使用舍入?您似乎只是将其用作某种标志。为什么要在驱动程序中分配新的驱动程序?您可以从当前实例中访问这些函数。