Saturday, April 4, 2015

Invoicing application

Sample Java code for an Invoice class, this was part of an invoicing application for Android I was working on...
import java.text.NumberFormat; import java.util.Scanner; public class InvoiceApp { public static void main(String[] args) { // display a welcome message System.out.println("Welcome to the Invoice Total Calculator"); System.out.println(); // print a blank line Scanner sc = new Scanner(System.in); String choice = "y"; while(choice.equalsIgnoreCase("y")) { // get user entries String customerType = Validator.getString(sc, "Enter customer type (r/c): "); double subtotal = Validator.getDouble(sc, "Enter subtotal: ", 0, 10000); // calculate the discount amount and total double discountPercent = getDiscountPercent(subtotal, customerType); double discountAmount = subtotal * discountPercent; double total = subtotal - discountAmount; // format the values NumberFormat currency = NumberFormat.getCurrencyInstance(); NumberFormat percent = NumberFormat.getPercentInstance(); String sSubtotal = currency.format(subtotal); String sCustomerType = ""; if (customerType.equalsIgnoreCase("r")) sCustomerType = "Retail"; else if (customerType.equalsIgnoreCase("c")) sCustomerType = "College"; String sDiscountPercent = percent.format(discountPercent); String sDiscountAmount = currency.format(discountAmount); String sTotal = currency.format(total); // format and display the results String message = "Subtotal: " + sSubtotal + "\n" + "Customer type: " + sCustomerType + "\n" + "Discount percent: " + sDiscountPercent + "\n" + "Discount amount: " + sDiscountAmount + "\n" + "Total: " + sTotal + "\n"; System.out.println(); System.out.println(message); System.out.print("Continue? (y/n): "); choice = sc.next(); System.out.println(); } ---------------------------------------------------------------------------- the Invoice class import java.text.NumberFormat; public class Invoice { //instance variables private double discountPercent; private int discountAmount; private double invoiceTotal; // the constructor public Invoice(SubTotal, CustomerType) { this.SubTotal; this.CustomerType; } http://www.java-forums.org/java-applets/5965-help-invoice-app.html public double setdiscountPercent(double discountPercent) { this.discountPercent = discountPercent; } public double getdiscountPercent() { return discountPercent; } public double setdiscountAmount(int discountAmount) { this.discountAmount = discountAmount; } public int getdiscountAmount() { return discountAmount; } public double setinvoiceTotal(double invoiceTotal) { this.invoiceTotal = invoiceTotal; } public double getinvoiceTotal() { this.calculateTotal(); return invoiceTotal; } private void calculateTotal() { double invoiceTotal = discountPercent * discountAmount; } public String getInvoice() { NumberFormat currency = NumberFormat.getCurrencyInstance(); return currency.format(this.getinvoiceTotal()); } }