Advertisement
coffeebeforecode

Rujul_Java_CAT1

Feb 16th, 2022
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. package temp;
  2.  
  3. import java.util.*;
  4.  
  5. public class RujulCAT {
  6.  
  7.     public static void main(String[] args) {
  8.         ArrayList<Invoice> invoices = new ArrayList<Invoice>();
  9.        
  10.         // Adding three invoices
  11.         invoices.add(new Invoice("Part 1", "Desc 1", 12, 5.0));
  12.         invoices.add(new Invoice("Part 2", "Desc 2", -1, 10.0));
  13.         invoices.add(new Invoice("Part 3", "Desc 3", 20, -9.0));
  14.        
  15.         // Printing invoice amount
  16.         for(Invoice i : invoices) {
  17.             System.out.println("\nPart number: " + i.getPartNum() + "\nDescription: " + i.getDesc() + "\nQuantity:" + i.getQuantity() + "\nPrice Per item: " + i.getPricePerItem());
  18.             System.out.println("Invoice Amount:" + i.getInvoiceAmount());
  19.         }
  20.        
  21.     }
  22.  
  23. }
  24.  
  25. class Invoice{
  26.     String partNum;
  27.     String desc;
  28.     int quantity;
  29.     double pricePerItem;
  30.    
  31.     public Invoice(String partNum, String desc, int q, double p) {
  32.         setPartNum(partNum);
  33.         setDesc(desc);
  34.         setQuantity(q);
  35.         setPricePerItem(p);
  36.     }
  37.    
  38.     public void setPartNum(String partNum) {
  39.         this.partNum = partNum;
  40.     }
  41.    
  42.     public void setDesc(String desc) {
  43.         this.desc = desc;
  44.     }
  45.    
  46.     public void setQuantity(int quantity) {
  47.         if (quantity < 0) {
  48.             this.quantity = 0;
  49.         }
  50.         else {
  51.             this.quantity = quantity;
  52.         }
  53.        
  54.     }
  55.    
  56.     public void setPricePerItem(double pricePerItem) {
  57.         if (pricePerItem < 0) {
  58.             this.pricePerItem = 0.0;
  59.         }
  60.         else {
  61.             this.pricePerItem = pricePerItem;
  62.         }
  63.     }
  64.    
  65.     public String getPartNum() {
  66.         return partNum;
  67.     }
  68.    
  69.     public String getDesc() {
  70.         return desc;
  71.     }
  72.    
  73.     public int getQuantity() {
  74.         return quantity;
  75.     }
  76.    
  77.     public double getPricePerItem() {
  78.         return pricePerItem;
  79.     }
  80.    
  81.     public double getInvoiceAmount() {
  82.         return quantity*pricePerItem;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement