객체 (23.05.06)

·2023년 5월 9일
0

Coding Test

목록 보기
11/39
post-thumbnail

  • Book 클래스
package edu.kh.oop.practice.model.vo;

public class Book {
	
	// 필드
	private String title;
	private int price;
	private double discountRate;
	private String author;
	
	// 생성자
	public Book() {}
	
	public Book(String title, int price, double discountRate, String author) {
		super();
		this.title = title;
		this.price = price;
		this.discountRate = discountRate;
		this.author = author;
	}
	
	// 메소드
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public double getDiscountRate() {
		return discountRate;
	}

	public void setDiscountRate(double discountRate) {
		this.discountRate = discountRate;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	@Override
	public String toString() {
		return title + " / " + price + " / " + discountRate + " / " + author;
	}
}
  • BookService 클래스

(1) 내 풀이

package edu.kh.oop.practice.model.service;

import edu.kh.oop.practice.model.vo.Book;

public class BookService {

	public void practice() {
		
		Book b1 = new Book();
		Book b2 = new Book("자바의정석", 30000, 0.2, "남궁성");
		System.out.println(b1.toString());
		System.out.println(b2.toString());
		
		System.out.println("=============================");
		
		b1.setTitle("C언어");
		b1.setPrice(30000);
		b1.setDiscountRate(0.1);
		b1.setAuthor("홍길동");
		
		System.out.println(b1.toString());
		
		System.out.println("=============================");
		
		System.out.println("도서명 = " + b2.getTitle());
		System.out.printf("할인된 가격 = %d원\n", b2.getPrice()-(int)(b2.getPrice()*b2.getDiscountRate()));
		
		System.out.println("도서명 = " + b1.getTitle());
		System.out.printf("할인된 가격 = %d원", b1.getPrice()-(int)(b1.getPrice()*b1.getDiscountRate()));
	}
	
}

(2) 다른 풀이 - 할인된 가격을 출력하는 코드가 조금 더 간결하게 작성됨

package edu.kh.oop.practice.model.service;

import edu.kh.oop.practice.model.vo.Book;

public class BookService {

	public void practice() {
		
		 // 1) 기본 생성자를 이용하여 첫 번째 Book 객체 생성
		Book b1 = new Book();
		
		// 2) 매개변수 생성자를 이용하여 두 번째 Book 객체 생성
		Book b2 = new Book("자바의정석", 30000, 0.2, "남궁성");
		
		// 3) 객체가 가진 필드 값을 toString()을 이용하여 출력
		System.out.println(b1.toString());
		System.out.println(b2.toString());
		
		System.out.println("=============================");
		
		// 4) 첫 번째 객체가 가진 값을 setter를 이용하여 수정
		b1.setTitle("C언어");
		b1.setPrice(30000);
		b1.setDiscountRate(0.1);
		b1.setAuthor("홍길동");
		
	    // 5) 수정된 객체의 필드 값을 toString() 메소드를 이용하여 출력
	    System.out.println("수정된 결과 확인");
	    System.out.println(b1);
		
		System.out.println("=============================");
		
	    // 6) 각 객체의 할인율을 적용한 책 가격을 계산해서 출력
	    // 7) 할인된 가격 = 가격 - (가격 * 할인율)
		System.out.printf("도서명 = %s \n할인된 가격 = %d\n", b1.getTitle(), b1.getPrice() - (int)(b1.getPrice() * b1.getDiscountRate()));
		
		System.out.printf("도서명 = %s \n할인된 가격 = %d\n", b2.getTitle(), b2.getPrice() - (int)(b2.getPrice() * b2.getDiscountRate()));
	}
	
}
  • PracticeRun 클래스
package edu.kh.oop.practice.run;

import edu.kh.oop.practice.model.service.BookService;

public class PracticeRun {

	public static void main(String[] args) {
		
		BookService service = new BookService();
		service.practice();
		
	}

}
profile
풀스택 개발자 기록집 📁

0개의 댓글