goblin
리니팅
goblin

공지사항

전체 방문자
오늘
어제
  • 분류 전체보기 (75)
    • 개발 (31)
      • Spring (12)
      • JPA (4)
      • JAVA (4)
      • Python (6)
      • Docker (1)
      • Error (3)
      • Spring Cloud로 개발하는 MSA (1)
    • 알고리즘 (32)
    • 자료구조 (3)
    • 컴퓨터 개론 (3)
    • 개인 프로젝트 (4)
      • 쇼핑몰 만들기 (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

인기 글

태그

  • JPA
  • Spring
  • 클래스
  • springboot
  • 파이썬
  • 객체
  • 다이나믹프로그래밍
  • 백준
  • dp
  • 스프링부트
  • 조합
  • Intellij
  • gradle
  • 다이나믹 프로그래밍
  • 스프링
  • 파워자바
  • 자료구조
  • python
  • 코딩테스트
  • sorting
  • 정렬
  • 코딩테스트연습
  • 구현
  • inflearn
  • 알고리즘
  • 프로그래머스
  • tdd
  • BOJ
  • 문자열
  • 동적계획법

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
goblin

리니팅

POWER JAVA(파워 자바 개정3판) - 5장 클래스와 객체 II Mini Project
개발/JAVA

POWER JAVA(파워 자바 개정3판) - 5장 클래스와 객체 II Mini Project

2022. 4. 10. 02:06
728x90

5장 클래스와 객체 II의 미니 프로젝트 1,2입니다.

 

Mini Project1 : 전기차 클래스

전기 자동차를 클래스로 작성해보자. 자동차는 완전(100%) 배터리로 시작한다. 자동차를 운전할 때마다 1km를 주행하고 배터리의 10%를 소모한다. 전기 자동차에는 2가지 정보를 보여주는 디스플레이가 있다. 주행한 총 거리는 “주행거리: ...km”. 남은 배터리 충전량은 “배터리: ...%”와 같이 표시된다.

 

저는 주행시 기존 배터리의 10%를 사용한다고 생각하고 풀어봤습니다.

 

소스 코드

class ECar{
	
	private int battery=100;
	private int distance=0;
	private static ECar instance = new ECar();
	private ECar() {}
	
	
	
	public static ECar getInstance() {
		return instance;
	}
	
	String dispDistance() {
		return "주행거리:"+ distance+"km";
	}
	
	String dispBattery() {
		return "배터리"+battery+"%";
	}
	
	void drive() {
		battery=(int) (battery*0.9);
		distance+=1;
	}
}
public class mini2 {
	public static void main(String[] args) {
		ECar car = ECar.getInstance();
		car.drive();
		
		System.out.println(car.dispBattery());
		System.out.println(car.dispDistance());
		car.drive();
		
		System.out.println(car.dispBattery());
		System.out.println(car.dispDistance());
	}
	
	}

 

실행 결과

 

Mini Project2 : 책 정보 저장

사용자가 읽은 책과 평점을 저장하는 객체 배열을 생성해보자. 다음과 같은 메뉴가 제공된다.

 

 

이 프로젝트는 클래스 파일을 3개 만들어서 풀었습니다.

 

소스 코드

1. Book.java

public class Book{
	private String title;
	private int value;
	Book(String title, int value) {
		this.title = title;
		this.value = value;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	
}

 

2. BookManage.java

import java.util.ArrayList;

public class BookManager {

	
	private static BookManager instance = new BookManager();
	private ArrayList<Book> bookList;
	
	private BookManager() {
		
		bookList = new ArrayList<Book>();
	}
	public static BookManager getInstance() {return instance;}
	
	public void showMenu() {
		System.out.println("================");
		System.out.println("1. 책 등록");
		System.out.println("2. 책 검색");
		System.out.println("3. 모든 책 출력");
		System.out.println("4. 종료");
		
	}
	public int readInput() {
		Scanner sc = new Scanner(System.in);
		int in = sc.nextInt();
		return in;
	}
	public void enroll() {
		Scanner en = new Scanner(System.in);
		System.out.print("제목: ");
		String title = en.nextLine();
		System.out.print("평점: ");
		int val = en.nextInt();
		Book book = new Book(title,val);
		bookList.add(book);
		
	}
	public void search() {
		System.out.print("책 제목을 입력하세요:");
		Scanner c = new Scanner(System.in);
		String t = c.nextLine();
		for (int i=0;i<bookList.size();i++) {
			if(t.equals(bookList.get(i).getTitle())) {
				System.out.println("찾았습니다!");
				System.out.println(bookList.get(i).getTitle());
				System.out.println(bookList.get(i).getValue());
			}
		}
	}
	public void print() {
		for (int i=0;i<bookList.size();i++) {
			System.out.println(bookList.get(i).getTitle());
			System.out.println(bookList.get(i).getValue());
		}
	}
}

 

3. BookTest.java

public class BookTest {

	static final int ENROLL = 1;
	static final int SEARCH = 2;
	static final int PRINT = 3;
	static final int EXIT = 4;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BookManager manager = BookManager.getInstance();
		int input;
		do
		{
			manager.showMenu();
			System.out.print("번호를 입력하시오:");
			input = manager.readInput();
			
			switch(input)
			{
			case ENROLL:
				manager.enroll();break;
			case SEARCH:
				manager.search();break;
			case PRINT:
				manager.print();break;
			case EXIT:
				return;				
			}
		}while(true);
		
	}

}

 

 

실행 결과

728x90
반응형

'개발 > JAVA' 카테고리의 다른 글

[SpringBoot] lombok  (0) 2022.05.22
POWER JAVA(파워 자바 개정 3판) - 4장 클래스와 객체 I Mini Project  (0) 2022.04.10
명품 자바 에센셜 2장 실습 문제  (0) 2022.04.03
    '개발/JAVA' 카테고리의 다른 글
    • [SpringBoot] lombok
    • POWER JAVA(파워 자바 개정 3판) - 4장 클래스와 객체 I Mini Project
    • 명품 자바 에센셜 2장 실습 문제
    goblin
    goblin

    티스토리툴바