Invoice

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.6 kB
0
Indexable
Never
public class Invoice {

    private String partNumber, partDescription;
    private int quantity;
    private double price;

    private static double totalInvoiceAmount = 0;

    public double getTotalAmount() {
        return totalInvoiceAmount;
    }

    private void updateTotalAmount(double a, double b) {
        totalInvoiceAmount -= a;

        totalInvoiceAmount += b;
    }

    public void resetTotalAmount() {
        totalInvoiceAmount = 0;
    }

    public static void setTotalInvoiceAmount(int totalInvoiceAmount) {
        Invoice.totalInvoiceAmount = totalInvoiceAmount;
    }

    public static double getTotalInvoiceAmount() {
        return totalInvoiceAmount;
    }

    public double getInvoiceAmount() {

        if (this.price < 0) {
            price = 0.0;
        }

        double amount = this.price * this.quantity;

        if (amount < 0) {
            amount = 0.0;
        }
        return amount;
    }

    public void setPartNumber(String partNumber) {
        this.partNumber = partNumber;
    }

    public void setPartDescription(String partDescription) {
        this.partDescription = partDescription;
    }

    public void setQuantity(int quantity) {
        updateTotalAmount(getInvoiceAmount(), quantity);
    }

    public void setPrice(double price) {
        if (price < 0.0) {
            price = 0.0;
        } else {
            this.price = price;
        }
    }

    public String getPartNumber() {
        return partNumber;
    }

    public String getPartDescription() {
        return partDescription;
    }

    public int getQuantity() {
        return quantity;
    }

    public double getPrice() {
        return price;
    }

    Invoice(int quantity, double price) {
        updateTotalAmount(0.0, this.quantity * this.price);
    }

    Invoice() {
        partNumber = "";
        partDescription = "";
        quantity = 0;
        price = 0.0;
    }

    Invoice(String partNumber, String partDescription, int quantity, double price) {

        this.partNumber = partNumber;
        this.partDescription = partDescription;
        if (quantity < 0) {
            quantity = 0;
        } else {
            this.quantity = quantity;
        }
        if (price < 0.0) {
            price = 0.0;
        } else {
            this.price = price;
        }
    }

    @Override
    public String toString() {

        return String.format("partNumber: %s%npartDescription: %s%nquantity: %d%nprice per one: %.2f%nTotal: %.2f%n", this.partNumber, this.partDescription, this.quantity, this.price, getInvoiceAmount());
    }
}//end class

class InvoiceTest {

    public static void main(String[] args) {

        Invoice mic = new Invoice("637221", "microphone", 3, 30);
        Invoice keyboard = new Invoice("131567", "keyboard", 4, 150);
        Invoice mouse = new Invoice("001234", "mouse", 1, 400);
        Invoice x = new Invoice();

        System.out.println("Item 1: \n" + mic);
        System.out.println("**********");
        System.out.println("");
        System.out.println("Item 2: \n" + keyboard);
        System.out.println("**********");
        System.out.println("");
        System.out.println("Item 3: \n" + mouse);
        System.out.println("**********");
        System.out.println("");
        System.out.println("Item 4: \n" + x);
        System.out.println("**********");
        System.out.println("");
    }

}//end class InvoiceTest