Witam. Mam problem z funkcją SendProfit( w klasie Profit podanej niżej). W momencie tworzenia plików danego miesiąca zmienna LoggedUser z klasy Account jest pusta. Nie bardzo rozumiem dlaczego?
MainWindow
public MainWindow()
{
InitializeComponent();
}
private void createAccountButton_Click(object sender, RoutedEventArgs e)
{
Account account = new Account(createUserNameTB.Text, createUserPasswordTB.Password);
account.CreateFiles();
account.CreateUser();
}
private void loginButton_Click(object sender, RoutedEventArgs e)
{
Account account = new Account(loginTB.Text, loginPasswordBox.Password);
account.Authorization();
}
private void sendProfitButton_Click(object sender, RoutedEventArgs e)
{
Profit profit = new Profit(profitTB.Text, profitDescriptionTB.Text, monthProfitCB.SelectedIndex);
profit.checkingTextBoxValue();
profit.SendProfit();
}
Klasa Account
public class Account
{
private string userName;
private string userPassword;
public string LoggedUser;
public string[] months = { "January", "Febrauary", "March", "April", "May", "June",
"July", "August", "Sectember", "October", "November", "December" };
public Account()
{
}
public Account(string username, string userpassword)
{
userName = username;
userPassword = userpassword;
}
public void CreateUser()
{
StreamWriter UserFileWritter = new StreamWriter(@"E:\" + userName + @"\" + userName + @".txt", true);
UserFileWritter.WriteLine(userName);
UserFileWritter.WriteLine(userPassword);
UserFileWritter.Close();
MessageBox.Show("Konto utworzone!");
}
public void CreateFiles()
{
for(int i=0; i<months.Length; i++)
{
Directory.CreateDirectory(@"E:\" + userName + @"\Profit\");
Directory.CreateDirectory(@"E:\" + userName + @"\Expense\");
StreamWriter ProfitFilesWriter = new StreamWriter(@"E:\" + userName + @"\Profit\" + months[i] + @".txt");
StreamWriter ExpenseFilesWriter = new StreamWriter(@"E:\" + userName + @"\Expense\" + months[i] + @".txt");
ProfitFilesWriter.Close();
ExpenseFilesWriter.Close();
}
}
public void Authorization()
{
if(File.Exists(@"E:\" + userName + @"\" + userName + @".txt"))
{
StreamReader AuthorizationUserReader = new StreamReader(@"E:\" + userName + @"\" + userName + @".txt");
string authorizationUserName = AuthorizationUserReader.ReadLine();
string authorizationUserPassword = AuthorizationUserReader.ReadLine();
AuthorizationUserReader.Close();
if(authorizationUserName == userName && authorizationUserPassword == userPassword)
{
OpenFiles();
MessageBox.Show("Zalogowano!");
LoggedUser = authorizationUserName;
}
else
{
MessageBox.Show("Złe hasło!");
}
}
else
{
MessageBox.Show("Użytkownik nie istnieje!");
}
}
public void OpenFiles()
{
for (int i = 0; i < months.Length; i++)
{
StreamReader ProfitFileReader = new StreamReader(@"E:\" + userName + @"\Profit\" + months[i] + @".txt", true);
StreamReader ExpenseFileReader = new StreamReader(@"E:\" + userName + @"\Expense\" + months[i] + @".txt", true);
}
}
} // end of class Account
Klasa Profit
class Profit
{
Account account = new Account();
private string profitTextBoxValue;
private string description;
private decimal profit;
private int monthSelected;
public Profit(string p, string d, int m)
{
profitTextBoxValue = p;
description = d;
monthSelected = m;
}
public void checkingTextBoxValue()
{ // popraw pierwszy warunek[ czy jest stringiem]
if (profitTextBoxValue == "" || profitTextBoxValue == null)
{
MessageBox.Show("To nie jest liczba!");
}
else
{
profit = Convert.ToDecimal(profitTextBoxValue);
}
}
public void SendProfit()
{
StreamWriter sendProfitFile = new StreamWriter(@"E:\" + account.LoggedUser + @"\Profit\" + account.months[monthSelected] + @".txt", true);
sendProfitFile.WriteLine(profit);
sendProfitFile.WriteLine(description);
sendProfitFile.Close();
MessageBox.Show(account.LoggedUser);
}
} // end of Profit class