Programming Language/Java

객체로 도서명과 출판사, 저자명, 가격, 할인율 출력하기

Ma_Sand 2022. 3. 3. 23:38
반응형
// 도서명과 출판사, 저자명, 가격, 할인율 출력하기
// this와 this(), setter/getter 메소드를 사용
// 도서명과 출판사, 저자명은 기본적으로 입력한다.

public class Book {
	
	private String title;  // 도서명
	private String publisher;  // 출판사
	private String author;  // 저자명
	private int price;  // 가격
	private double discountRate;  // 할인율
	
	// 기본 생성자
	public Book() {}
	
    // 매개변수 생성자
	public Book(String title, String publisher, String author) {
		this(title, publisher, author, 0, 0.0);  // this()로 코드 줄이기
	}
	public Book(String title, String publisher, String author, int price, double discountRate) {
		this.title = title;
		this.publisher = publisher;
		this.author = author;
		this.price = price;
		this.discountRate = discountRate;
		
	}
	
  // setter/getter 메소드
  // setter와 getter 메소드 순서는 상관없다.
	public String getTitle() {  // .get매개변수명() : 값을 가져온다
		return title;
	}
	public void setTitle(String title) {  // .set매개변수명() : 값을 초기화 선언
		this.title = title;
	}
	public String getPublisher() {
		return publisher;
	}
	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	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;
	}
	
    // 모든 정보를 한번에 출력할 information 메소드
	public String information() {
		return title+", "+publisher+", "+author+", "+price+", "+discountRate;
	}

}

// 최종값을 호출, 출력하는 메소드
public class BookRun {

	public static void main(String[] args) {
        // 도서명과 출판사, 저자명, 가격, 할인율 값 입력
		Book b1 = new Book("Java의 정석", "도우출판", "남궁성", 30000, 5);
		Book b2 = new Book("VOCA 3000", "해커스", "해커스연구소", 17000, 8);
		
        // b1과 b2로 information()을 호출
		System.out.println(b1.information());
		System.out.println(b2.information());
	}

}

▼실행결과

Java의 정석, 도우출판, 남궁성, 30000, 5.0
VOCA 3000, 해커스, 해커스연구소, 17000, 8.0

 

위의 클래스 Book에 대해 실행할 클래스를 배열로 만들어서 사용자에게 입력 받아 값을 출력하기

public class BookArrayRun {
    Book[] arr = new Book[4];  // 배열의 크기를 4로 지정
    
    Scanner sc = new Scanner(System.in);  // 입력받기 위해 Scanner 사용
    
    for(int i = 0; i < arr.length; i++) {  // 배열의 크기만큼 반복(4만큼 반복)
        System.out.println("도서명: ");
        String title = sc.nextLine();  // 도서명은 title로 받기
        
        System.out.println("출판사: ");
        String publisher = sc.nextLine();  // 출판사는 publisher로 받기
        
        System.out.println("저자명: ");
        String author = sc.nextLine();  // 저자명은 author로 받기
        
        System.out.println("가격: ");
        int price = sc.nextInt();  // 가격은 price로 받기
        
        System.out.println("할인율");
        double discountRate = sc.nextDouble();  // 할인율은 discountRate로 받기
        sc.nextLine();  // 다양한 .next들을 사용하였으므로 엔터를 날린다.
        
        // 배열 arr에 Book 배열 담기
        arr[i] = new Book(title, publisher, author, price, discountRate);
    }
    // 저자명 3개 출력하기
    for(int i = 0; i < 3; i++) {
        System.out.println(arr[i].getAuthor());  // .getAuthor()로 저자명만 뽑아내기
    }
}

배열을 만들어 반복문 for문을 이용하면 도서명과 출판사, 저자명, 가격, 할인율을 4번 입력받는다. 왜냐하면 배열의 크기가 4로 지정되었기 때문에. 

그리고 두 번째 for문에서 해당 for문의 저자명 값을 가져오는 것을 세 번 반복하라고 하였다. 그러면 처음 입력받은 저자명의 값부터 세 번째 입력받은 저자명의 값까지 순서대로 출력된다.

반응형