Created
August 29, 2017 16:22
-
-
Save punkyoon/2d6be998a97a8f408f66086dad153f7c to your computer and use it in GitHub Desktop.
modified example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ChildrensPrice extends Price | |
| { | |
| int getPriceCode() | |
| { | |
| return Movie.CHILDRENS; | |
| } | |
| double getCharge(int daysRented) | |
| { | |
| double result = 1.5; | |
| if(daysRented > 3) | |
| result += (daysRented - 3) * 1.5; | |
| return result; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Enumeration; | |
| import java.util.Vector; | |
| public class Customer | |
| { | |
| private String _name; | |
| private Vector _rentals = new Vector(); | |
| public Customer(String name) | |
| { | |
| _name = name; | |
| } | |
| public void addRental(Rental arg) | |
| { | |
| _rentals.addElement(arg); | |
| } | |
| public String getName() | |
| { | |
| return _name; | |
| } | |
| public String statement() | |
| { | |
| Enumeration rentals = _rentals.elements(); | |
| String result = getName() + "고객님의 대여 기록\n"; | |
| while(rentals.hasMoreElements()) | |
| { | |
| Rental each = (Rental) rentals.nextElement(); | |
| result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.getCharge()) + "\n"; | |
| } | |
| result += "누적 대여료: " + String.valueOf(getTotalCharge()) + "\n"; | |
| result += "적립 포인트: " + String.valueOf(getTotalFrequentRenterPoints()); | |
| return result; | |
| } | |
| public String htmlStatement() | |
| { | |
| Enumeration rentals = _rentals.elements(); | |
| String result = "<h1><em>" + getName() + " 고객님의 대여 기록</em></h1><p>\n"; | |
| while(rentals.hasMoreElements()) | |
| { | |
| Rental each = (Rental)rentals.nextElement(); | |
| result += each.getMovie().getTitle() + ": " + String.valueOf(each.getCharge()) + "<br>\n"; | |
| } | |
| result += "<p>누적 대여료 <em>" + String.valueOf(getTotalCharge()) + "</em><p>\n"; | |
| result += "적립 포인트: <em>" + String.valueOf(getTotalFrequentRenterPoints()) + "</em><p>"; | |
| return result; | |
| } | |
| private double getTotalCharge() | |
| { | |
| double result = 0; | |
| Enumeration rentals = _rentals.elements(); | |
| while(rentals.hasMoreElements()) | |
| { | |
| Rental each = (Rental)rentals.nextElement(); | |
| result += each.getCharge(); | |
| } | |
| return result; | |
| } | |
| private int getTotalFrequentRenterPoints() | |
| { | |
| int result = 0; | |
| Enumeration rentals = _rentals.elements(); | |
| while(rentals.hasMoreElements()) | |
| { | |
| Rental each = (Rental)rentals.nextElement(); | |
| result += each.getFrequentRenterPoints(); | |
| } | |
| return result; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Movie | |
| { | |
| public static final int CHILDRENS = 2; | |
| public static final int REGULAR = 0; | |
| public static final int NEW_RELEASE = 1; | |
| private String _title; | |
| private Price _priceCode; | |
| public Movie(String title, int priceCode) | |
| { | |
| _title = title; | |
| setPriceCode(priceCode); | |
| } | |
| public int getPriceCode() | |
| { | |
| return _priceCode.getPriceCode(); | |
| } | |
| public void setPriceCode(int arg) | |
| { | |
| switch (arg) | |
| { | |
| case REGULAR: | |
| _priceCode = new RegularPrice(); | |
| break; | |
| case CHILDRENS: | |
| _priceCode = new ChildrensPrice(); | |
| break; | |
| case NEW_RELEASE: | |
| _priceCode = new NewReleasePrice(); | |
| break; | |
| default: | |
| throw new IllegalArgumentException("가격 코드가 잘못됐습니다."); | |
| } | |
| } | |
| public String getTitle() | |
| { | |
| return _title; | |
| } | |
| int getFrequentRenterPoints(int daysRented) | |
| { | |
| return _priceCode.getFrequentRenterPoints(daysRented); | |
| } | |
| double getCharge(int daysRented) | |
| { | |
| return _priceCode.getCharge(daysRented); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class NewReleasePrice extends Price | |
| { | |
| int getPriceCode() | |
| { | |
| return Movie.NEW_RELEASE; | |
| } | |
| double getCharge(int daysRented) | |
| { | |
| return daysRented * 3; | |
| } | |
| int getFrequentRenterPoints(int daysRented) | |
| { | |
| return (daysRented > 1) ? 2 : 1; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abstract class Price | |
| { | |
| abstract int getPriceCode(); | |
| abstract double getCharge(int daysRented); | |
| int getFrequentRenterPoints(int daysRented) | |
| { | |
| return 1; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RegularPrice extends Price | |
| { | |
| int getPriceCode() | |
| { | |
| return Movie.REGULAR; | |
| } | |
| double getCharge(int daysRented) | |
| { | |
| double result = 2; | |
| if(daysRented > 2) | |
| result += (daysRented - 2) * 1.5; | |
| return result; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Rental | |
| { | |
| private Movie _movie; | |
| private int _daysRented; | |
| public Rental(Movie movie, int daysRented) | |
| { | |
| _movie = movie; | |
| _daysRented = daysRented; | |
| } | |
| public int getDaysRented() | |
| { | |
| return _daysRented; | |
| } | |
| public Movie getMovie() | |
| { | |
| return _movie; | |
| } | |
| int getFrequentRenterPoints() | |
| { | |
| return _movie.getFrequentRenterPoints(_daysRented); | |
| } | |
| double getCharge() | |
| { | |
| return _movie.getCharge(_daysRented); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment