Advertisement
dimipan80

C#Exams 1. Fruit Market (on Java Code, using class Product)

Aug 24th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.util.Locale;
  3. import java.util.Scanner;
  4.  
  5. class Product {
  6.     private String name;
  7.     private BigDecimal price;
  8.  
  9.     public String getName() {
  10.         return name;
  11.     }
  12.  
  13.     public void setName(String name) {
  14.         this.name = name;
  15.     }
  16.  
  17.     public BigDecimal getPrice() {
  18.         price = BigDecimal.ZERO;
  19.         switch (this.name) {
  20.         case "banana":
  21.             price = new BigDecimal("1.80");
  22.             break;
  23.         case "orange":
  24.             price = new BigDecimal("1.60");
  25.             break;
  26.         case "apple":
  27.             price = new BigDecimal("0.86");
  28.             break;
  29.         case "tomato":
  30.             price = new BigDecimal("3.20");
  31.             break;
  32.         case "cucumber":
  33.             price = new BigDecimal("2.75");
  34.             break;
  35.  
  36.         default:
  37.             break;
  38.         }
  39.  
  40.         return price;
  41.     }
  42.  
  43.     public void setPrice(BigDecimal price) {
  44.         this.price = price;
  45.     }
  46.  
  47.     public Product(String name) {
  48.         setName(name);
  49.         setPrice(price);
  50.     }
  51.  
  52.     public boolean isFruit() {
  53.         return (!this.name.equals("tomato") && !this.name.equals("cucumber"));
  54.     }
  55. }
  56.  
  57. public class _1_FruitMarket {
  58.  
  59.     public static void main(String[] args) {
  60.         // TODO Auto-generated method stub
  61.         Locale.setDefault(Locale.ROOT);
  62.         Scanner scan = new Scanner(System.in);
  63.         String dayOfWeek = scan.next();
  64.  
  65.         BigDecimal totalPrice = BigDecimal.ZERO;
  66.         for (int i = 0; i < 3; i++) {
  67.             String quantity = scan.next();
  68.             String productName = scan.next();
  69.             Product product = new Product(productName);
  70.             BigDecimal currentPrice = applyTheDayOfWeekMarketDiscountsAndGetProductPrice(
  71.                     product, dayOfWeek);
  72.             currentPrice = currentPrice.multiply(new BigDecimal(quantity));
  73.             totalPrice = totalPrice.add(currentPrice);
  74.         }
  75.  
  76.         System.out.printf("%.2f\n", totalPrice);
  77.     }
  78.  
  79.     private static BigDecimal applyTheDayOfWeekMarketDiscountsAndGetProductPrice(
  80.             Product product, String day) {
  81.         // TODO Auto-generated method stub
  82.         BigDecimal price = product.getPrice();
  83.         switch (day) {
  84.         case "Tuesday":
  85.             return (!product.isFruit()) ? price : price
  86.                     .multiply(new BigDecimal("0.80"));
  87.         case "Wednesday":
  88.             return (product.isFruit()) ? price : price
  89.                                         .multiply(new BigDecimal("0.90"));
  90.         case "Thursday":
  91.             return (!product.getName().equals("banana")) ? price : price
  92.                     .multiply(new BigDecimal("0.70"));
  93.         case "Friday":
  94.             return price.multiply(new BigDecimal("0.90"));
  95.         case "Sunday":
  96.             return price.multiply(new BigDecimal("0.95"));
  97.  
  98.         default:
  99.             return price;
  100.         }
  101.     }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement