using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO; // 파일스트림
namespace StudentApp
{
public partial class Form1 : Form
{
// 필드
private List<Student> students = new List<Student>(); // 학생 리스트 (이것도 어디엔가 분리돼야함)
private IniFile iniFile = new IniFile(); // 공용으로 한 번만 생성
private Create create = new Create();
private ListManage listManage = new ListManage();
public Form1()
{
InitializeComponent();
}
// 학생 생성
private void btnCreate_Click(object sender, EventArgs e)
{
string name = txtName.Text;
int age; // TryParse 라는 함수도 있음
try
{
age = int.Parse(txtAge.Text);
}
catch (FormatException)
{
MessageBox.Show("나이는 숫자로 입력해주세요.");
return;
}
string major = txtMajor.Text;
// 객체 생성
Student student1 = create.CreateStudent(name, age, major);
// 결과 출력
lbResult.Text = student1.Introduce();
// 학생 리스트에 추가
listManage.AddStudent(students, student1);
for (int i = 0; i < students.Count; i++)
{
listStudents.Items.Add(students[i].Name);
}
}
// 파일 저장
private void btnSave_Click(object sender, EventArgs e)
{
try
{
iniFile.WriteFile(students);
MessageBox.Show("학생 정보가 저장되었습니다.");
}
catch (Exception ex)
{
MessageBox.Show($"파일 저장 실패: {ex.Message}");
}
}
// 파일 읽기
private void btnOpen_Click(object sender, EventArgs e)
{
try
{
students = iniFile.ReadFile();
listStudents.Items.Clear(); // 리스트박스 초기화
foreach (var s in students) // 학생 이름들 추가
{
listStudents.Items.Add(s.Name);
}
MessageBox.Show("불러오기 완료");
}
catch (Exception ex)
{
MessageBox.Show($"파일 읽기 실패: {ex.Message}");
}
}
// 상세 정보
private void listStudents_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = listStudents.SelectedIndex; // 선택한 인덱스번호가 저장돼요
Modify modify = new Modify(); // 인스턴스 생성
Student selectedStudent = modify.ModifyIndex(index, students); // 메서드값 넘겨받아요
if (selectedStudent != null)
{
MessageBox.Show(selectedStudent.Introduce()); // 클래스의 메서드 호출
}
else
{
MessageBox.Show("오류");
}
}
// 초기화
private void btnClear_Click(object sender, EventArgs e)
{
ClearUI clearUI = new ClearUI();
clearUI.ClearInputs(txtName, txtAge, txtMajor, listStudents);
// 피드백: 이렇게 ui를 넘기는 경우는 잘 없음
}
}
}