Created
May 11, 2020 07:47
-
-
Save ssabii/3e66196c365e0f1eac7a6ff67f978a04 to your computer and use it in GitHub Desktop.
인터페이스 예제 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IFile | |
{ | |
void Save(List<int> item); | |
} | |
// 점수를 텍스트 파일로 저장하는 클래스 | |
class TextFile : IFile | |
{ | |
public void Save(List<int> item) | |
{ | |
string scoreText = ""; | |
for(int i=0; i<item.Count; i++) | |
{ | |
scoreText += item[i].ToString(); | |
if (i < item.Count - 1) | |
scoreText += " "; | |
} | |
File.WriteAllText("score.txt", scoreText); | |
Console.WriteLine("txt 파일로 저장"); | |
} | |
} | |
// 점수를 CSV 파일로 저장하는 클래스 | |
class CSVFile : IFile | |
{ | |
public void Save(List<int> item) | |
{ | |
string scoreText = ""; | |
for (int i = 0; i < item.Count; i++) | |
{ | |
scoreText += item[i].ToString(); | |
if (i < item.Count - 1) | |
scoreText += ","; | |
} | |
File.WriteAllText("score.csv", scoreText); | |
Console.WriteLine("csv 파일로 저장"); | |
} | |
} | |
class Score | |
{ | |
private List<int> scores; | |
private IFile file; | |
public Score() | |
{ | |
scores = new List<int>(); | |
} | |
public void SetFileType(IFile file) | |
{ | |
this.file = file; | |
} | |
public void AddScore(int score) | |
{ | |
scores.Add(score); | |
} | |
public void SaveScore() | |
{ | |
if(file != null) | |
file.Save(scores); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Score score = new Score(); | |
score.AddScore(100); | |
score.AddScore(90); | |
score.AddScore(80); | |
score.AddScore(70); | |
TextFile textFile = new TextFile(); | |
CSVFile csvFile = new CSVFile(); | |
score.SetFileType(textFile); | |
score.SaveScore(); | |
score.SetFileType(csvFile); | |
score.SaveScore(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment