OOP 실습 문제

JAKE·2023년 7월 26일

실습문제풀이

목록 보기
3/5

문제 1.

public class BookService { 
	public void practice() {
// 1) 기본 생성자를 이용하여 첫 번째 Book 객체 생성
// 2) 매개변수 생성자를 이용하여 두 번째 Book 객체 생성 (사용 데이터 참고)
// 3) 객체가 가진 필드 값을 toString()을 이용하여 

// 4) 첫 번째 객체가 가진 값을 setter를 이용하여 수정
// 5) 수정된 객체의 필드 값을 toString() 메소드를 이용하여 출력

// 6) 각 객체의 할인율을 적용한 책 가격을 계산해서 출력
// 7) 할인된 가격 = 가격 - (가격 * 할인율)
	}
}

1. 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) { 
		this.title = title;
		this.price = price;
		this.discountRate = discountRate;
		this.author = author;
	}
	

	public String toString() {
		System.out.println(title + " / " + price + " / " + discountRate + " / " + author);	
		return "";
	}
	
	
	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;
	}
	
}

2. BookService 클래스

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.print(b1.toString());
		System.out.print(b2.toString());
		System.out.println("=============================");
		System.out.println("수정된 결과 확인");
				
		b1.setTitle("C언어");
		b1.setPrice(30000);
		b1.setDiscountRate(0.1);
		b1.setAuthor("홍길동");

		System.out.print(b1.toString());
		System.out.println("=============================");
		
		double discountedPrice = b1.getPrice()*(1-b1.getDiscountRate());
		double discountedPrice2 = b2.getPrice()*(1-b2.getDiscountRate());
		
		System.out.println("도서명 = " + b1.getTitle());
		System.out.printf("할인된 가격 = %.0f원\n", discountedPrice);
		System.out.println("도서명 = " + b2.getTitle());
		System.out.printf("할인된 가격 = %.0f원\n", discountedPrice2);
	}
	
	
}

3. Run클래스

package edu.kh.oop.practice.run;

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

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

출력값


문제 2.

1. Snack 클래스

package edu.kh.practice.snack.model.vo;

public class Snack {

	private String kind; // 종류
	private String name; // 이름
	private String flavor; //맛
	private int numOf; //개수
	private int price; //가격
	
	public Snack() {}
	
	public Snack(String kind, String name, String flavor, int numOf, int price) {
		this.kind = kind;
		this.name = name;
		this.flavor = flavor;
		this.numOf = numOf;
		this.price = price;
	}
		
	
	public String information() { // 담긴 데이터 반환

		return kind + "(" + name + " - " + flavor + ") " + numOf + "개 " + price + "원";
	}
	
	
	public String getKind() {
		return kind;
	}
	public void setKind(String kind) {
		this.kind = kind;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getFlavor() {
		return flavor;
	}
	public void setFlavor(String flavor) {
		this.flavor = flavor;
	}
	public int getNumOf() {
		return numOf;
	}
	public void setNumOf(int numOf) {
		this.numOf = numOf;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	
	
}

2. SnackController 클래스

package edu.kh.practice.snack.controller;

import edu.kh.practice.snack.model.vo.Snack;
import edu.kh.practice.snack.view.SnackMenu;

public class SnackController {

	private Snack s = new Snack();
	
	public SnackController() {}
	
	public String saveData(String kind, String name, String flavor, int numOf, int price) { //정보 저장
	
		s.setKind(kind);
		s.setName(name);
		s.setFlavor(flavor);
		s.setNumOf(numOf);
		s.setPrice(price);
		
		return "";
	}
	
	public String confirmData() { // 저장된 데이터 반환 메소드
		
		return s.information();
		
	}
	
}

3. SnackMenu클래스

package edu.kh.practice.snack.view;

import java.util.Scanner;
import edu.kh.practice.snack.controller.SnackController;

public class SnackMenu {

	Scanner sc = new Scanner(System.in);
	SnackController scr = new SnackController();
	
	public void menu() {
	
		System.out.println("스낵류를 입력하세요.");
		System.out.print("종류 : ");
		String kind = sc.next();

		System.out.print("이름 : ");
		String name = sc.next();
		
		System.out.print("맛 : ");
		String flavor = sc.next();

		System.out.print("개수 : ");
		int numOf = sc.nextInt();

		System.out.print("가격 : ");
		int price = sc.nextInt();
	
		scr.saveData(kind, name, flavor, numOf, price);
		System.out.println("\n저장 완료되었습니다.");
	
		System.out.print("저장된 정보를 확인하시겠습니다?(y/n) :");
		char input = sc.next().charAt(0);
		
		if(input == 'y') {
			System.out.println(scr.confirmData());
		}
		
	
	}

}

4. Run 클래스

package edu.kh.practice.snack.run;

import edu.kh.practice.snack.view.SnackMenu;

public class Run {

	public static void main(String[] args) {
		
		SnackMenu sm = new SnackMenu();
		
		sm.menu();
	}
	
}

출력값


0개의 댓글