Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class InvoiceTest {
- public static void main(String[] args) {
- ArrayList<Invoice> invoices = new ArrayList<Invoice>();
- Invoice invoice1, invoice2;
- invoice1 = new Invoice("1234","Hammer",2,14.95);
- invoice2 = new Invoice("5678","Paint Brush",-5,-9.99);
- invoices.add(invoice1);
- invoices.add(invoice2);
- for(Invoice n: invoices){
- n.getInvoiceAmount();
- System.out.println(n);
- }
- invoice1.setNumber("001234");
- invoice1.setDescription("Yellow Hammer");
- invoice1.setQuantity(3);
- invoice1.setPrice(19.49);
- invoice2.setQuantity(3);
- invoice2.setPrice(9.49);
- for(Invoice n: invoices){
- n.getInvoiceAmount();
- System.out.println(n);
- }
- }
- }
- ----------------------------------------------------------------------------------------------------------------------------
- public class Invoice {
- private String number;
- private String description;
- private int quantity;
- private double price;
- private double invoiceTotal;
- public Invoice(){
- number = "";
- description = "";
- quantity = 0;
- price = 0;
- }
- public Invoice(String n, String d, int q, double p) {
- number = n;
- description = d;
- quantity = q;
- price = p;
- }
- public String getNumber(){
- return number;
- }
- public void setNumber(String n){
- number = n;
- }
- public String getDescription(){
- return description;
- }
- public void setDescription(String d){
- description = d;
- }
- public int getQuantity(){
- return quantity;
- }
- public void setQuantity(int q){
- quantity = q;
- }
- public double getPrice(){
- return price;
- }
- public void setPrice(double p){
- price = p;
- }
- public double getInvoiceAmount(){
- if(quantity < 0){
- quantity = 0;
- }
- if(price < 0) {
- price = 0.0;
- }
- invoiceTotal = quantity * price;
- return invoiceTotal;
- }
- @Override
- public String toString(){
- return String.format(
- "Invoice with number %s is %s with quantity %d at %f each. costs a total of %f.\n",
- number, description, quantity, price, invoiceTotal);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment