OOP 실습문제

송원철·2023년 7월 26일


빵먹고싶다

Ex1 BOOK

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

BookService

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

import java.util.Scanner;

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

public class BookService {

		private Scanner sc = new Scanner(System.in);
		
		public void practice() {
			
			Book book1 = new Book();
			Book book2 = new Book("자바의 정석", 30000, 0.2, "남궁성");
			
			System.out.println(book1.toString());
	        System.out.println(book2.toString());
			
	        book1.setTitle("C언어");
	        book1.setPrice(30000);
	        book1.setDiscountRate(0.1);
	        book1.setAuthor("홍길동");
			
	        System.out.println("=============================");
	        System.out.println("수정된 결과 확인");
	        System.out.println(book1.toString());
	        
	        System.out.println("=============================");
	        System.out.println("도서명 = " + book1.getTitle());
	        System.out.println("할인된 가격 = " + (book1.getPrice() - (book1.getPrice() * book1.getDiscountRate())) + "원");

	        System.out.println("도서명 = " + book2.getTitle());
	        System.out.println("할인된 가격 = " + (book2.getPrice() - (book2.getPrice() * book2.getDiscountRate())) + "원");
			
	}
		
		
		
}

PracticeRun

package edu.kh.oop.practice.run;

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

public class PracticeRun {

	public static void main(String[] args) {
		
		//HeroService hs = new HeroService();
		//hs.practice();
		
		BookService bs = new BookService();
		bs.practice();
	}
	
	
}

Ex2 Snack

Snack

package com.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 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;
		}

	public String information() {
			return this.kind + "(" + this.name + " - " + this.flavor + ") " + this.numOf + "개 " + this.price + "원"; 
	}
}

SnackController

package com.kh.practice.snack.controller;

import com.kh.practice.snack.model.vo.Snack;

public class SnackController {

	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();
	}
	
}

SnackMenu

package com.kh.practice.snack.view;

import java.util.Scanner;

import com.kh.practice.snack.controller.SnackController;

public class SnackMenu {

	private Scanner sc = new Scanner(System.in);
	
	private SnackController scr = new SnackController();
	

	public void menu() {
		String kind; String name; String flavor; int numOf; int price;
		
		System.out.println("스낵류를 입력하세요.");
		System.out.print("종류 : ");
		kind = sc.nextLine();
		System.out.print("이름 : ");
		name = sc.nextLine();
		System.out.print("맛 : ");
		flavor = sc.nextLine();
		System.out.print("개수 : ");
		numOf = sc.nextInt(); sc.nextLine();
		System.out.print("가격 : ");
		price = sc.nextInt(); sc.nextLine();

		System.out.println(scr.saveData(kind, name, flavor, numOf, price));
		
		System.out.println("저장한 정보를 확인하시겠습니까?(y/n) : ");
		String yn = sc.nextLine();
		
		if(yn.equals("y")) {
			System.out.println(scr.confirmData());	
		} else if(yn.equals("n")) {
			return;
		}
		
	}
	
}

Run

package com.kh.practice.snack.run;

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

public class Run {

	public static void main(String[] args) {
		
		SnackMenu SM = new SnackMenu();
		SM.menu();
		//menu() 호출
		
	}

}
profile
초보자

0개의 댓글