Leejiaxin

Lab 7

May 20th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3.  
  4. public class InvoiceTest {
  5.  
  6.     public static void main(String[] args) {
  7.        
  8.         ArrayList<Invoice> invoices = new ArrayList<Invoice>();
  9.  
  10.         Invoice invoice1, invoice2;
  11.        
  12.         invoice1 = new Invoice("1234","Hammer",2,14.95);
  13.         invoice2 = new Invoice("5678","Paint Brush",-5,-9.99);
  14.        
  15.         invoices.add(invoice1);
  16.         invoices.add(invoice2);
  17.        
  18.         for(Invoice n: invoices){
  19.             n.getInvoiceAmount();
  20.             System.out.println(n);
  21.         }
  22.        
  23.         invoice1.setNumber("001234");
  24.         invoice1.setDescription("Yellow Hammer");
  25.         invoice1.setQuantity(3);
  26.         invoice1.setPrice(19.49);
  27.        
  28.         invoice2.setQuantity(3);
  29.         invoice2.setPrice(9.49);
  30.        
  31.         for(Invoice n: invoices){
  32.             n.getInvoiceAmount();
  33.             System.out.println(n);
  34.         }
  35.     }
  36.  
  37. }
  38.  
  39. ----------------------------------------------------------------------------------------------------------------------------
  40.  
  41.  
  42. public class Invoice {
  43.    
  44.     private String number;
  45.     private String description;
  46.     private int quantity;
  47.     private double price;
  48.     private double invoiceTotal;
  49.    
  50.     public Invoice(){
  51.        
  52.         number = "";
  53.         description = "";
  54.         quantity = 0;
  55.         price = 0;
  56.     }
  57.    
  58.     public Invoice(String n, String d, int q, double p) {
  59.        
  60.         number = n;
  61.         description = d;
  62.         quantity = q;
  63.         price = p;
  64.     }
  65.  
  66.     public String getNumber(){
  67.         return number;
  68.     }
  69.    
  70.     public void setNumber(String n){
  71.         number = n;
  72.     }
  73.    
  74.     public String getDescription(){
  75.         return description;
  76.     }
  77.    
  78.     public void setDescription(String d){
  79.         description = d;
  80.     }
  81.    
  82.     public int getQuantity(){
  83.         return quantity;
  84.     }
  85.    
  86.     public void setQuantity(int q){
  87.         quantity = q;
  88.     }
  89.    
  90.     public double getPrice(){
  91.         return price;
  92.     }
  93.    
  94.     public void setPrice(double p){
  95.         price = p;
  96.     }
  97.    
  98.     public double getInvoiceAmount(){
  99.        
  100.         if(quantity < 0){
  101.             quantity = 0;
  102.         }
  103.        
  104.         if(price < 0) {
  105.             price = 0.0;
  106.         }
  107.        
  108.         invoiceTotal = quantity * price;
  109.        
  110.         return invoiceTotal;
  111.     }
  112.    
  113.     @Override
  114.     public String toString(){
  115.         return String.format(
  116.                 "Invoice with number %s is %s with quantity %d at %f each. costs a total of %f.\n",
  117.                 number, description, quantity, price, invoiceTotal);
  118.     }
  119.    
  120. }
Advertisement
Add Comment
Please, Sign In to add comment