과제로 메모장을 만들어 보았다.
MemoService.java
import java.util.Scanner;
public class MemoService {
public static void main(String[] args) {
MemoListValue memo = new MemoListValue();
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("-----메모장-----");
System.out.println("1. 메모리스트" );
System.out.println("2. 메모 보기" );
System.out.println("3. 메모작성" );
System.out.println("4. 메모수정" );
System.out.println("5. 메모삭제" );
System.out.println("6. 종료" );
System.out.println("번호를 입력하십시오");
int enterNumber = scanner.nextInt();
if (1 == enterNumber){
memo.showMemoList();
}
else if(2 == enterNumber) {
memo.readMemo();
}
else if(3 == enterNumber) {
memo.createMemo();
}
else if(4 == enterNumber) {
memo.updateMemo();
}
else if(5 == enterNumber) {
memo.deleteMemo();
}
else if(6 == enterNumber) {
break;
}
else{
System.out.println("");
System.out.println("잘못된 입력입니다");
System.out.println("번호를 다시 입력해주세요");
System.out.println("");
}
}
}
}
MemoValue.java
import java.time.LocalDateTime;
public class MemoValue {
public String password;
private String title;
private String content;
private LocalDateTime lastUpdateTime;
public MemoValue(String title, String content, String password) {
this.title = title;
this.content = content;
this.lastUpdateTime = LocalDateTime.now();
this.password =password;
}
static MemoValue newMemo(String title, String content, String password) {
return new MemoValue(title,content,password);
}
void update(String content) {
this.content = content;
this.lastUpdateTime = LocalDateTime.now();
}
String getTitle() {
return title;
}
String getContent() {
return content;
}
String getLastUpdatedDatetime() {
return lastUpdateTime.toString();
}
}
MemoListValue.java
import java.util.Objects;
import java.util.Scanner;
public class MemoListValue {
private int memoLength = 1;
private final MemoValue[] memoVlues;
private final int Memo_SIZE = 20;
public MemoListValue() {
this.memoVlues = new MemoValue[Memo_SIZE];
}
public void createMemo() {
System.out.println("");
if (Memo_SIZE == this.memoLength) {
System.out.println("메모가 꽉찼습니다");
System.out.println("");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("제목을 작성해주세요");
String title = scanner.nextLine();
System.out.println("본문을 작성해주세요");
String content = scanner.nextLine();
System.out.println("비밀번호를 입력해 주세요");
String password = scanner.nextLine();
memoVlues[this.memoLength++] = MemoValue.newMemo(title, content, password);
System.out.println("메모가 작성되었습니다");
System.out.println("");
}
public void readMemo () {
System.out.println("");
Scanner scanner = new Scanner(System.in);
System.out.println("확인할 메모의 번호를 입력해주세요");
int selectedNumber = scanner.nextInt();
MemoValue memoValue = memoVlues[selectedNumber];
if (null == memoValue) {
System.out.println("작성된 메모가 없습니다");
System.out.println("");
return;
}
System.out.println("");
String headLine = String.format("번호:%d 제목:%s", selectedNumber, memoValue.getTitle());
System.out.println(headLine);
System.out.println(memoValue.getLastUpdatedDatetime());
System.out.println(memoValue.getContent());
System.out.println("");
}
public void updateMemo() {
System.out.println("");
Scanner scanner = new Scanner(System.in);
System.out.println("수정하실 메모의 번호를 입력해주세요");
int selectedNumber = Integer.parseInt(scanner.nextLine());
System.out.println("수정하실 메모의 비밀번호를 입력해주세요");
String password = scanner.nextLine();
System.out.println("본문을 작성해주세요");
String content = scanner.nextLine();
MemoValue memoValue = memoVlues[selectedNumber];
if (Objects.equals(memoVlues[selectedNumber].password, password)) {
if (null == memoValue) {
System.out.println("존재하지 않는 메모입니다");
return;
}
memoValue.update(content);
System.out.println("메모가 수정되었습니다");
System.out.println("");
} else {
System.out.println("비밀번호가 일치하지 않습니다");
}
}
public void deleteMemo() {
System.out.println("");
Scanner scanner = new Scanner(System.in);
System.out.println("삭제하실 메모의 번호를 입력해주세요");
int selectedNumber = Integer.parseInt(scanner.nextLine());
MemoValue memoValue = memoVlues[selectedNumber];
if (null == memoValue) {
System.out.println("존재하지 않는 메모입니다");
return;
}
System.out.println("삭제하실 메모의 비밀번호를 입력해주세요");
String password = scanner.nextLine();
if (Objects.equals(memoVlues[selectedNumber].password, password)) {
for (int i = 1; i < this.memoLength; i++) {
if(memoVlues[i] == memoVlues[selectedNumber]){
for(int j = i; j < this.memoLength-1; j++){
memoVlues[j]=memoVlues[j+1];
}
this.memoLength--;
}
}
System.out.println("메모가 삭제되었습니다");
System.out.println("");
} else {
System.out.println("비밀번호가 일치하지 않습니다");
}
}
public void showMemoList () {
System.out.println("");
if (1 == this.memoLength) {
System.out.println("작성된 메모가 없습니다");
System.out.println("");
return;
}
for (int i = 1; i < this.memoLength; i++) {
MemoValue memoValue = memoVlues[i];
String headLine = String.format("번호:%d 제목:%s 작성날짜:%s", i, memoValue.getTitle(),
memoValue.getLastUpdatedDatetime());
System.out.println(headLine);
}
System.out.println("");
}
}
스캐너 사용법도 모르고, 링크드리스트를 생성해서 값을 넣으려다 해결하지 못하고 시간이 촉박해 인터넷 강의에서 다른 방법을 찾아 만들었는데, 나중에 내가 구상한대로 만드는 것을 도전해봐야 할 것 같다.
'개발일지(일간)' 카테고리의 다른 글
22년 11월 27일 미니프로젝트2(2) (0) | 2022.11.27 |
---|---|
22년 11월 24일 미니프로젝트2(1) (0) | 2022.11.24 |
22년 11월 22일 객체지향(3) (0) | 2022.11.22 |
22년 11월 21일 객체지향(2) (0) | 2022.11.21 |
22년 11월 18일 객체지향(1) (0) | 2022.11.18 |