Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.util.Locale;
- import java.util.Scanner;
- class Product {
- private String name;
- private BigDecimal price;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public BigDecimal getPrice() {
- price = BigDecimal.ZERO;
- switch (this.name) {
- case "banana":
- price = new BigDecimal("1.80");
- break;
- case "orange":
- price = new BigDecimal("1.60");
- break;
- case "apple":
- price = new BigDecimal("0.86");
- break;
- case "tomato":
- price = new BigDecimal("3.20");
- break;
- case "cucumber":
- price = new BigDecimal("2.75");
- break;
- default:
- break;
- }
- return price;
- }
- public void setPrice(BigDecimal price) {
- this.price = price;
- }
- public Product(String name) {
- setName(name);
- setPrice(price);
- }
- public boolean isFruit() {
- return (!this.name.equals("tomato") && !this.name.equals("cucumber"));
- }
- }
- public class _1_FruitMarket {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Locale.setDefault(Locale.ROOT);
- Scanner scan = new Scanner(System.in);
- String dayOfWeek = scan.next();
- BigDecimal totalPrice = BigDecimal.ZERO;
- for (int i = 0; i < 3; i++) {
- String quantity = scan.next();
- String productName = scan.next();
- Product product = new Product(productName);
- BigDecimal currentPrice = applyTheDayOfWeekMarketDiscountsAndGetProductPrice(
- product, dayOfWeek);
- currentPrice = currentPrice.multiply(new BigDecimal(quantity));
- totalPrice = totalPrice.add(currentPrice);
- }
- System.out.printf("%.2f\n", totalPrice);
- }
- private static BigDecimal applyTheDayOfWeekMarketDiscountsAndGetProductPrice(
- Product product, String day) {
- // TODO Auto-generated method stub
- BigDecimal price = product.getPrice();
- switch (day) {
- case "Tuesday":
- return (!product.isFruit()) ? price : price
- .multiply(new BigDecimal("0.80"));
- case "Wednesday":
- return (product.isFruit()) ? price : price
- .multiply(new BigDecimal("0.90"));
- case "Thursday":
- return (!product.getName().equals("banana")) ? price : price
- .multiply(new BigDecimal("0.70"));
- case "Friday":
- return price.multiply(new BigDecimal("0.90"));
- case "Sunday":
- return price.multiply(new BigDecimal("0.95"));
- default:
- return price;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement