tauk

Abstract Item (abstract class) with interface and inheritance

Feb 10th, 2022
1,480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. //interface
  4. interface IPrice {
  5.     public double calcPriceWithVAT();
  6. }
  7.  
  8. //abstract class
  9. abstract class AbsItem implements IPrice {
  10.     protected String itemId;
  11.     protected String name;
  12.     protected double unitPrice;
  13.     protected int itemCount;
  14.    
  15.     public AbsItem(String itemId, String name, double unitPrice, int itemCount) {
  16.         this.itemId = itemId;
  17.         this.name = name;
  18.         this.unitPrice = unitPrice;
  19.         this.itemCount = itemCount;
  20.     }
  21.    
  22.     public String toString() {
  23.         return itemId + " " + name
  24.             + " Unit Price:" + String.format(
  25.             "%.2f",unitPrice)
  26.             + " Item Count:" + itemCount;
  27.     }
  28.    
  29. }
  30.  
  31. //FoodItem child class
  32. class FoodItem extends AbsItem {
  33.     private Date expiryDate;
  34.     private String countryOfOrigin;
  35.    
  36.     public FoodItem(String itemId, String name, double  unitPrice, int itemCount, Date expiryDate,
  37.     String countryOfOrigin) {
  38.         super(itemId, name, unitPrice, itemCount);
  39.         this.expiryDate = expiryDate;
  40.         this.countryOfOrigin = countryOfOrigin;
  41.     }  
  42.    
  43.     public String toString() {
  44.         return super.toString() + " Expiry date:" + expiryDate
  45.             + " Country of origin: " + countryOfOrigin;
  46.     }
  47.    
  48.     public double calcPriceWithVAT() {
  49.         return (this.unitPrice * this.itemCount) +
  50.             (this.unitPrice * this.itemCount * 0.05);
  51.     }
  52. }
  53.  
  54. class NonFoodItem extends AbsItem {
  55.     private String itemCategory;
  56.    
  57.     public NonFoodItem(String itemId, String name, double   unitPrice, int itemCount, String category) {   
  58.         super(itemId, name, unitPrice, itemCount);
  59.         this.itemCategory = category;
  60.     }
  61.    
  62.     public String toString() {
  63.         return super.toString() + " category:" +itemCategory;
  64.     }
  65.    
  66.     public double calcPriceWithVAT() {
  67.         return (this.unitPrice * this.itemCount) +
  68.             (this.unitPrice * this.itemCount * 0.025);
  69.     }
  70. }
  71.  
  72. public class AbsItemTester {
  73.     public static void main(String[] args) {
  74.         //Create a list of items
  75.         List<AbsItem> items = new ArrayList<>();
  76.        
  77.         AbsItem foodItem1 = new FoodItem("F101", "Alpin Water", 10, 10, new Date("22/12/2023"), "Turkiye");
  78.        
  79.         AbsItem nonFoodItem1 = new NonFoodItem("KB11", "Keyboard", 20, 10, "Computer Equipment");
  80.        
  81.         //add items to the list
  82.         items.add(foodItem1);
  83.         items.add(nonFoodItem1);
  84.        
  85.         //iterate over the list using for-each loop
  86.         //and print item, its subtotal
  87.         double total = 0;
  88.         for (AbsItem item : items) {
  89.             System.out.println(item
  90.             + "\n\tSubtotal:"
  91.                 + String.format("%.2f",item.calcPriceWithVAT()));
  92.             total += item.calcPriceWithVAT();
  93.         }
  94.        
  95.         //print total of all items in the list
  96.         System.out.println("\nTotal price:" +total);
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment