我是 c# 的新手,所以请原谅我的基本问题,但是您如何解决“方法 'SaveData' 没有重载需要 8 个参数”的问题?
这是我的代码:
using System;
using System.IO;
namespace Assignment2partA
{
class Program
{
const int MAXCUSTOMERS = 1;
const float PEAK_RATE = 0.50F;
const float OFF_PEAK_RATE = 0.18F;
const float STANDARD_RATE = 0.32F;
const float GST_RATE = 0.10F;
const string PEAK_SYM = "P";
const string STANDARD_SYM = "S";
static void Main()
{
string[] CustomerNumber = new string[MAXCUSTOMERS];
string[] CustomerName = new string[MAXCUSTOMERS];
string[] CustomerAddress = new string[MAXCUSTOMERS];
string[] CustomerRateType = new string[MAXCUSTOMERS];
float[] Peak = new float[MAXCUSTOMERS];
float[] OffPeak = new float[MAXCUSTOMERS];
float[] Standard = new float[MAXCUSTOMERS];
int currentcustomer = 0;
string menuSelection = "";
while (menuSelection != "x")
{
menuSelection = Menu();
switch (menuSelection)
{
case "s":
SaveData(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, ref currentcustomer);
break;
case "a":
AddCustomer(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, MAXCUSTOMERS, ref currentcustomer);
break;
case "f":
FindCustomer(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, currentcustomer, CustomerName.ToString());
break;
case "u":
Update(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, currentcustomer, CustomerName.ToString());
break;
case "e":
Electricity(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, currentcustomer);
break;
case "b":
Bills(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, ref currentcustomer);
break;
case "c":
Clear(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, currentcustomer);
break;
case "x":
break;
}
}
}
static string Menu()
{
string menuSelection = "";
Console.WriteLine("What would you like to do?");
Console.WriteLine("Add a Customer? (a)");
Console.WriteLine("Find a Customer? (f)");
Console.WriteLine("Update a Customer Record? (u)");
Console.WriteLine("Enter Electricity Consumption data? (e)");
Console.WriteLine("Calculate Bills? (b)");
Console.WriteLine("Save data to file? (s)");
Console.WriteLine("Clear Consuption data? (c)");
Console.WriteLine("Exit? (x)");
Console.Write("Enter your response >");
menuSelection = Console.ReadLine();
return menuSelection;
}
static void AddCustomer(string[] CustomerNumber, string[] CustomerName, string[] CustomerAddress, string[] CustomerRateType, float[] Peak, float[] OffPeak, float[] Standard, int MaxCustomers, ref int CurrentCustomer)
{
if (CurrentCustomer < MaxCustomers)
{
Console.WriteLine("Enter Customer Number");
Console.Write("Customer Number > ");
CustomerNumber[CurrentCustomer] = Console.ReadLine();
if (CustomerNumber[CurrentCustomer] == "")
{
Console.WriteLine("Error : Customer Number cannot be empty,");
}
// Get the Customer Name and exit is an error is detected
Console.Write("Customer Name > ");
CustomerName[CurrentCustomer] = Console.ReadLine();
if (CustomerName[CurrentCustomer] == "")
{
Console.WriteLine("Error : Customer Name cannot be empty,");
}
// Get the Customer Address and exit is an error is detected
Console.Write("Address > ");
CustomerAddress[CurrentCustomer] = Console.ReadLine();
if (CustomerAddress[CurrentCustomer] == "")
{
Console.WriteLine("Error : Address cannot be empty,");
}
// Get the Rate Type and exit is an error is detected
while (CustomerRateType[CurrentCustomer] != PEAK_SYM && CustomerRateType[CurrentCustomer] != STANDARD_SYM)
{
Console.Write("Standard Rate (S) or Peak/Off Peak (P) >");
CustomerRateType[CurrentCustomer] = Console.ReadLine().ToUpper();
if (CustomerRateType[CurrentCustomer] != PEAK_SYM && CustomerRateType[CurrentCustomer] != STANDARD_SYM)
{
Console.WriteLine("Error : The Rate Type must be either S or P.");
}
}
}
}
static int Search(string[] CustomerName, int Count, string id)
{
int record;
for (record = 0; record < Count; record++)
if (CustomerName[record] == id) break;
if (record != Count) return -1; // not found
else return record; // return record number
}
static void FindCustomer(string[] CustomerNumber, string[] CustomerName, string[] CustomerAddress, string[] CustomerRateType, float[] Peak, float[] OffPeak, float[] Standard, int CurrentCustomer, string CustomerSearch)
{
Console.WriteLine("Enter Customer Name: ");
CustomerSearch = Console.ReadLine();
int record = Search(CustomerName, CurrentCustomer, CustomerSearch);
if (record == -1)
{
Console.WriteLine("Error! There is no customer that contains that name. ");
}
else
{
Console.WriteLine(CustomerNumber[record]);
Console.WriteLine(CustomerName[record]);
Console.WriteLine(CustomerAddress[record]);
Console.WriteLine(CustomerRateType[record]);
if (CustomerRateType[record] == PEAK_SYM)
{
Console.WriteLine(Peak[record]);
Console.WriteLine(OffPeak[record]);
}
else
{
Console.WriteLine(Standard[record]);
}
}
}
static void Bills(string[] CustomerNumber, string[] CustomerName, string[] CustomerAddress, string[] CustomerRateType, float[] Peak, float[] OffPeak, float[] Standard, ref int CurrentCustomer)
{
float TotalBilled = 0.0F;
float TotalUsage = 0.0F;
float AverageBilled = 0.0F;
float AverageUsage = 0.0F;
int record;
CurrentCustomer = CustomerName.Length;
// Calculation variables
float PeakCharge = 0.0F;
float OffPeakCharge = 0.0F;
float TotalCharge = 0.0F;
float TotalPlusGST = 0.0F;
for (record = 0; record < CurrentCustomer; record++)
{
if (CustomerRateType[record] == PEAK_SYM)
{
PeakCharge = Peak[record] * PEAK_RATE;
OffPeakCharge = OffPeak[record] * OFF_PEAK_RATE;
TotalCharge = PeakCharge + OffPeakCharge;
// Keep track of the usage
TotalUsage = TotalUsage + (Peak[record] + OffPeak[record]);
}
else
{
TotalCharge = Standard[record] * STANDARD_RATE;
// Keep track of the usage
TotalUsage = TotalUsage + Standard[record];
}
TotalPlusGST = TotalCharge + TotalCharge * GST_RATE;
// Keep track of the total billed
TotalBilled = TotalBilled + TotalPlusGST;
Console.WriteLine("===================================");
Console.WriteLine("Electricity Bill for Customer {0}", CustomerNumber[record]);
Console.WriteLine("===================================");
Console.WriteLine("Name : {0}", CustomerName[record]);
Console.WriteLine("Address : {0}", CustomerAddress[record]);
if (CustomerRateType[record] == PEAK_SYM)
{
Console.WriteLine("Peak : {0,7:C}", PeakCharge);
Console.WriteLine("Off Peak : {0,7:C}", OffPeakCharge);
}
Console.WriteLine("Total : {0,7:C}", TotalCharge);
Console.WriteLine();
Console.WriteLine("Including GST : {0,7:C}", TotalPlusGST);
Console.WriteLine("===================================");
}
AverageBilled = TotalBilled / CurrentCustomer;
AverageUsage = TotalUsage / CurrentCustomer;
// Display the averages
Console.WriteLine("Average amount billed is {0:C}.", AverageBilled);
Console.WriteLine("Average consumption per customer is {0}.", AverageUsage);
}
static void Update(string[] CustomerNumber, string[] CustomerName, string[] CustomerAddress, string[] CustomerRateType, float[] Peak, float[] OffPeak, float[] Standard, int CurrentCustomer, string CustomerSearch)
{
string Update;
Console.WriteLine("Enter Customer Name: ");
CustomerSearch = Console.ReadLine();
int record = Search(CustomerName, CurrentCustomer, CustomerSearch);
if (record == -1)
{
Console.WriteLine("Error! There is no customer that contains that name. ");
}
else
{
Console.WriteLine(CustomerNumber[record]);
Console.Write("Do you want to update this record? (Y/N): ");
Update = Console.ReadLine();
if (Update == "Y")
{
Console.Write("Please enter new value: ");
CustomerNumber[record] = Console.ReadLine();
}
Console.WriteLine(CustomerName[record]);
Console.Write("Do you want to update this record? (Y/N): ");
Update = Console.ReadLine();
if (Update == "Y")
{
Console.Write("Please enter new value: ");
CustomerName[record] = Console.ReadLine();
}
Console.WriteLine(CustomerAddress[record]);
Console.Write("Do you want to update this record? (Y/N): ");
Update = Console.ReadLine();
if (Update == "Y")
{
Console.Write("Please enter new value: ");
CustomerAddress[record] = Console.ReadLine();
}
Console.WriteLine(CustomerRateType[record]);
if (CustomerRateType[record] == PEAK_SYM)
{
Console.WriteLine(Peak[record]);
Console.WriteLine(OffPeak[record]);
}
else
{
Console.WriteLine(Standard[record]);
}
}
}
static void Electricity(string[] CustomerNumber, string[] CustomerName, string[] CustomerAddress, string[] CustomerRateType, float[] Peak, float[] OffPeak, float[] Standard, int CurrentCustomer)
{
CurrentCustomer = CustomerName.Length;
int record;
for (record = 0; record < CurrentCustomer; record++)
{
Console.WriteLine(CustomerName[record]);
if (CustomerRateType[record] == PEAK_SYM)
{
Console.WriteLine("Enter Peak Usage: ");
Peak[record] = float.Parse(Console.ReadLine());
Console.WriteLine("Enter Off Peak Usage: ");
OffPeak[record] = float.Parse(Console.ReadLine());
}
else
{
Console.WriteLine("Enter Standard Usage: ");
Standard[record] = float.Parse(Console.ReadLine());
}
}
}
static void Clear(string[] CustomerNumber, string[] CustomerName, string[] CustomerAddress, string[] CustomerRateType, float[] Peak, float[] OffPeak, float[] Standard, int CurrentCustomer)
{
CurrentCustomer = CustomerName.Length;
int record;
for (record = 0; record < CurrentCustomer; record++)
{
Peak[record] = 0.0f;
OffPeak[record] = 0.0f;
Standard[record] = 0.0f;
}
}
static void SaveData(string CustomerNumber, string CustomerName, string CustomerAddress, string CustomerRateType, float Peak, float OffPeak, float Standard)
{
FileStream fin;
string str;
string[] data = null;
// Open a file for reading
try
{
fin = new FileStream("billingdata.txt", FileMode.Open);
}
// Report an error if file cannot be opened
catch (IOException exc)
{
Console.WriteLine(exc.Message);
return;
}
StreamReader fstr_in = new StreamReader(fin);
string str1 = "";
StreamWriter fstr_out;
try
{
fstr_out = new StreamWriter("billingdata.txt");
}
catch (IOException exc)
{
Console.WriteLine(exc.Message);
return;
}
try
{
fstr_out.WriteLine(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType);
}
catch (IOException exc)
{
Console.WriteLine(exc.Message);
return;
}
fstr_out.Close();
}
}
}