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 |