과제
Day16 - static 심화
qltjfeo55555
2024. 12. 12. 16:24
https://record5555.tistory.com/23
Day16 - static 1~6과제
과제 1) static 직접 만들어 보기일반 클래스를 하나 만든 후, 맴버 변수로 private static 정수를 하나 가지게 하자. 일반 클래스 public 메서드로 해당 static 정수를 하나 1 올리는 메서드와, 해당 static변
record5555.tistory.com
심화 과제 3) 업적 시스템
새로운 cs 파일을 만들고, Achievement 클래스를 생성. 아래 구현 사항을 보다가 필요해보이는 내용이 발견되면 자유롭게 추가 구현 ㄱㄱ
일반 필드 및 프로퍼티
- string Name: 업적 이름
- string Description: 업적 설명
- int Goal: 목표 수치
- int Progress: 현재 진행 수치 (기본값 0)
- bool IsCompleted: 업적 달성 여부 (기본값 false)
static 필드
- int TotalAchievements: 생성된 업적의 총 개수를 저장하는 static 필드
- int CompletedAchievements: 달성된 업적의 총 개수를 저장하는 static 필드
생성자
- 이름, 설명, 목표 수치를 받아 초기화
- 생성자가 호출될 때마다 TotalAchievements를 증가.
메서드
- AddProgress
- 반환 없음. 인자값으로 int value를 받음
- Progress에 value를 더하고 목표 수치에 도달했는지 확인
- 목표를 달성하면:
- IsCompleted를 true로 설정.
- CompletedAchievements를 1 증가.
- "업적 [업적 이름] 달성!" 출력.
- DisplayInfo
- 업적 이름, 설명, 목표 및 진행 상황, 달성 여부를 출력
- 본인 취향껏
- static 메서드: DisplaySummary
- 반환 없음
- 현재 생성된 업적의 총 개수와 달성된 업적의 총 개수를 출력
- 테스트를 위해 메인으로 이동하여 다음을 작성
- Achievement 객체를 3개 생성
- 첫 번째 업적: "초급 도전자", "점수 100점 달성", 목표 100
- 두 번째 업적: "중급 도전자", "점수 500점 달성", 목표 500
- 세 번째 업적: "고급 도전자", "점수 1000점 달성", 목표 1000
- 각 업적의 AddProgress 메서드를 호출하여 진행 상황을 업데이트:
- 첫 번째 업적에 AddProgress 인자값으로 100
- 두 번째 업적에 AddProgress 인자값으로 600
- 세 번째 업적에 AddProgress 인자값으로 800
- 각 업적의 DisplayInfo를 호출하여 현재 상태를 출력.
- DisplaySummary를 호출하여 총 업적 및 달성된 업적 개수를 출력.
internal class Achievement
{
string AchievementName;
string Description;
int Goal;
int progress = 0;
bool IsCompleted = false;
//static
static int TotalAchievements;
static int CompletedAchievements;
public Achievement(string name, string description,int goal )
{
this.AchievementName = name;
this.Description = description;
this.Goal = goal;
TotalAchievements++;
}
//목표 수치에 도달했는지
public void AddProgress(int value)
{
progress += value;
if(progress >= Goal)
{
IsCompleted = true;
CompletedAchievements++;
Console.WriteLine($"업적 [{AchievementName}] 달성!");
}
}
public void DisplayInfo()
{
Console.WriteLine("업적 이름 : " + AchievementName);
Console.WriteLine("업적 설명 : " + Description);
Console.WriteLine("목표 : " + Goal);
Console.WriteLine("진행 상황 : " + progress);
Console.WriteLine("달성 : " + IsCompleted);
}
static public void DisplaySummary()
{
Console.WriteLine("현재 생성된 업적 총 개수 : "+ TotalAchievements);
Console.WriteLine("달성된 업적의 총 개수 : "+ CompletedAchievements);
}
}
//메인문
internal class Program
{
static void Main(string[] args)
{
Achievement achievement1 = new Achievement("초급 도전자", "점수 100점 달성", 100); ;
Achievement achievement2 = new Achievement("중급 도전자", "점수 500점 달성", 500); ;
Achievement achievement3 = new Achievement("고급 도전자", "점수 1000점 달성", 1000); ;
achievement1.AddProgress(100);
achievement2.AddProgress(600);
achievement3.AddProgress(800);
achievement1.DisplayInfo();
Achievement.DisplaySummary();
Console.WriteLine();
achievement2.DisplayInfo();
Achievement.DisplaySummary();
Console.WriteLine();
achievement3.DisplayInfo();
Achievement.DisplaySummary();
Console.WriteLine();
}
}