在过去的几周里,我一直在学习一些C#在线课程,以便学习编程。由于我没有任何编程的朋友,我发现在编写代码时很难获得反馈/评论。当然代码正在运行,但代码质量怎么样?
为了避免最糟糕的初学者错误,如果有人可以快速查看下面的代码或者将我引导到其他代码审查是主题的论坛,我会真的有所帮助。 (如果这是一个不正确的方法,请删除此帖子。)
关于项目
我基于四个类创建了一个简单的Adress Book(控制台应用程序):
该应用程序具有以下功能:
为简单起见,我只在应用程序中添加了名称和地址,还有非常糟糕的数据输入验证(专注于学习OOP)。关于更新功能,我知道它将更新firstName上匹配的所有内容。
问题
代码
主要
class Program
{
static void Main(string[] args)
{
bool ProgramIsRunning = true;
AdressBook ab = StartProgram();
Console.WriteLine("--------- AdressBook 1.0 ---------");
while (ProgramIsRunning)
{
// Print user options
PrintUserOptions();
var userInput = Console.ReadLine();
switch (userInput)
{
case "1":
ab.CreateUser();
break;
case "2":
ab.UpdateUserInformation();
break;
case "3":
ab.RemovePersonFromList();
break;
case "4":
ab.ShowAllPersonsInList();
break;
case "x":
ProgramIsRunning = false;
break;
}
}
}
private static void PrintUserOptions()
{
Console.WriteLine("Choose one of the following options: ");
Console.WriteLine("#1 Create new user");
Console.WriteLine("#2 Edit user information");
Console.WriteLine("#3 Delete existing user");
Console.WriteLine("#4 Show all users in adressBook");
}
private static AdressBook StartProgram()
{
AdressBook ab = new AdressBook();
//Start program by loading saved users from txt-file
WriteAndReadToFile writer = new WriteAndReadToFile();
writer.ReadFromFile(ab);
return ab;
}
}
人
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Adress { get; set; }
public Person(string firstName, string lastName, string adress)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Adress = adress;
}
}
}
AdressBook
class AdressBook
{
private WriteAndReadToFile wtf;
private List<Person> adressBookList = new List<Person>();
public List<Person> AdressBookList
{
get { return adressBookList; }
set { this.adressBookList = value; }
}
public AdressBook()
{
AdressBookList = new List<Person>();
wtf = new WriteAndReadToFile();
}
// Create instance of Person-class and call AddPersonToList-method
public void CreateUser()
{
Console.WriteLine("Enter firstName:");
var firstName = Console.ReadLine();
Console.WriteLine("Enter lastName:");
var lastName = Console.ReadLine();
Console.WriteLine("Enter adress:");
var adress = Console.ReadLine();
Person person = new Person(firstName, lastName, adress);
AddPersonToList(person);
wtf.WriteUserToFile(person);
}
// Add new person to AdressBookList
private void AddPersonToList(Person person) => AdressBookList.Add(person);
//Remove user from list where first and last name match
public void RemovePersonFromList()
{
Console.WriteLine("Enter firstName of the user you want to remove");
var firstName = Console.ReadLine();
Console.WriteLine("Enter lastname of the user you want to remove");
var lastName = Console.ReadLine();
AdressBookList.RemoveAll(item => item.FirstName == firstName && item.LastName == lastName);
wtf.UpdateUserOnFile(adressBookList);
}
//Show all Persons in AdressBookList
public void ShowAllPersonsInList()
{
foreach (var person in AdressBookList)
{
Console.WriteLine("FirstName: {0}, LastName: {1}, Adress: {2}", person.FirstName, person.LastName, person.Adress);
}
}
public void UpdateUserInformation()
{
Console.WriteLine("Which information do you want to update?");
Console.WriteLine("#1: Firstname, #2: Lastname, 3# Adress");
var userOption = Console.ReadLine();
Console.WriteLine("Enter firstname on existing user to be updated");
var oldFirstName = Console.ReadLine();
UpdateUserFunction(userOption, oldFirstName);
}
private void UpdateUserFunction(string userOption, string oldFirstName)
{
var personsWithMatchingFirstName = AdressBookList.Where(person => person.FirstName == oldFirstName);
string newValue;
if(userOption == "1")
{
Console.WriteLine("Enter a new first Name");
newValue = Console.ReadLine();
foreach (var person in personsWithMatchingFirstName)
{
person.FirstName = newValue;
wtf.UpdateUserOnFile(adressBookList);
}
}
else if (userOption == "2")
{
Console.WriteLine("Enter a new last name");
newValue = Console.ReadLine();
foreach (var person in personsWithMatchingFirstName)
{
person.LastName = newValue;
wtf.UpdateUserOnFile(adressBookList);
}
}
else if(userOption == "3")
{
Console.WriteLine("Enter a new adress");
newValue = Console.ReadLine();
foreach (var person in personsWithMatchingFirstName)
{
person.Adress = newValue;
wtf.UpdateUserOnFile(adressBookList);
}
}
}
}
将writeToFile
class WriteAndReadToFile
{
private readonly string UserTextFile = ConfigurationManager.AppSettings["textFileName"];
public void WriteUserToFile(Person person)
{
using (StreamWriter sw = new StreamWriter(UserTextFile, true))
{
sw.WriteLine(person.FirstName + "," + person.LastName + "," + person.Adress + ",");
}
}
public void ReadFromFile(AdressBook ab)
{
string textLine;
try
{
using (StreamReader sr = new StreamReader(UserTextFile))
{
while ((textLine = sr.ReadLine()) != null)
{
string[] userInformation = textLine.Split(',');
Person p = new Person(userInformation[0], userInformation[1], userInformation[2]);
ab.AdressBookList.Add(p);
}
}
}
catch (FileNotFoundException fnf)
{
Console.WriteLine("File does not exist " + fnf);
}
catch (Exception e)
{
Console.WriteLine("Something went wrong" + e);
}
}
public void UpdateUserOnFile(List<Person> adressBookList)
{
// Remove old row
using (StreamWriter sw = new StreamWriter(UserTextFile))
{
sw.Flush();
foreach (var person in adressBookList)
{
sw.WriteLine(person.FirstName + "," + person.LastName + "," + person.Adress + ",");
}
}
}
}