We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,464 Members — Technology Publication meets Social Media

# How to add transaction subtotals?

Inline Code Example Here
package mariabakeshop;

import java.text.Numb
* @param message
*            the prompt string to display
* @return a String input value
*/
public static String prompt(String message) {
String value = "";
Scanner in = new Scanner(System.in);

System.out.print(message);
value = in.nextLine();

return value;
}

/**
* Convert a double to a currency format
*
* @param value
*            the double to format
* @return a currency formatted String
*/
public static String currency(double value) {
NumberFormat formatter = NumberFormat.getCurrencyInstance();

return formatter.format(value);
}

/**
* Determine the discount amount. If the sub-total is greater than or equal
* to 50, then calculate a discount amount.
*
* @param subTotal
*            the sub total of goods purchased
* @param discountRate
*            the rate of discount
* @return the discount amount
*/
public static double getDiscount(double subTotal) {
char applyDiscount = 'n';
double discountAmount = 0;
final double DISCOUNT_RATE = 0.10;

// should we apply the discount?
if (subTotal >= 50) {
applyDiscount = prompt("\nApply 10% discount (y/n)? ")
.toLowerCase().charAt(0);

if (applyDiscount == 'y') {
discountAmount = subTotal * DISCOUNT_RATE;
} else {
System.out.println("Discount not applied.");
}
}

return discountAmount;
}

/**
* Display a receipt
*
* @param product
*            the product purchased
* @param subTotal
*            the sub total of goods purchased
* @param discount
*            the discount amount
* @param gst
*            the GST
* @param total
*            the total of purchase
*
*            PRODUCT: 10x Bread @ \$3.00 \$30.00 PRODUCT: 10x Cake @ \$4.00
*            \$40.00 SUB-TOTAL: \$70.00 DISCOUNT: -\$7.00 GST: \$3.15 =======
*            TOTAL: \$66.15
*
*/
public static void printReceipt(String[] products, int[] quantities,
double[] prices, int numOfproducts, double subTotal,
double discount, double gst, double total) {

for (int i = 0; i < numOfproducts; i += 1) {
System.out.println("PRODUCT:   " + quantities[i] + "x "
+ products[i] + " @ " + currency(prices[i]) + " "
+ currency(quantities[i] * prices[i]));
}
System.out.println("SUB-TOTAL: " + rightAlign(currency(subTotal), 9));
if (discount > 0) {
System.out.println("DISCOUNT:  "
+ rightAlign("-" + currency(discount), 9));
}
System.out.println("GST:       " + rightAlign(currency(gst), 9));
System.out.println("TOTAL:     " + rightAlign(currency(total), 9));
}

/**
* Right-align a string value
*
* @param value
*            the value to align
* @param width
*            the column width
* @return the aligned value
*/
public static String rightAlign(String value, int width) {
StringBuilder sb = new StringBuilder(value);

while (sb.length() < width) {
sb.insert(0, ' ');
}

return sb.toString();

}

public static void printTransaction(int numOfTransactions, double subTotal,
int numOfProducts, final int MAX_TRANSACTIONS, double grossProfit,
double discountAmount, int transactions[]) {

grossProfit += subTotal - discountAmount;

do {
for (int i = 0; i < MAX_TRANSACTIONS; i += 1) {
// display the totals
System.out.println("Total transactions processed: "
+ numOfTransactions);
System.out.println("Total gross profit (excluding GST): "
+ currency(grossProfit));
break;

}

} while (numOfTransactions < MAX_TRANSACTIONS && ("Another transaction (y/n)? ").toLowerCase().charAt(
0) == 'y');

}

public static void main(String[] args) {
// local variables
String productName = "";
double price = -1.0;
double gstAmount = -1.0;
double totalPrice = -1.0;
double subTotal = -1.0;
// initialize to zero for correct application of discount
double discountAmount = 0.0;
int quantity = -1;
// always process at least one transaction
int numOfTransactions = 0;
// total gross profit init to 0 for summing
double grossProfit = 0;
// the tax rate as a percentage
final double GST_RATE = 0.05;
// the discount as a percentage
// maximum number of products per transaction
final int MAX_PRODUCTS = 10;

// Maximum number of transactions in a day
final int MAX_TRANSACTIONS = 100;

// parallel arrays for each transaction
String[] productNames = new String[MAX_PRODUCTS];
int[] quantities = new int[MAX_PRODUCTS];
double[] prices = new double[MAX_PRODUCTS];
int transactions[] = new int[MAX_TRANSACTIONS];

int numOfProducts = 0;

// display the title
displayTitle();

do {
numOfTransactions += 1;

// reset transaction variables
numOfProducts = 0;
subTotal += 1;

System.out.println("Begin Transaction " + numOfTransactions + "\n");

do {
// prompt for required input (simple data validation)
while ((productName = prompt("Enter the product name:  "))
.trim().equals(""))
;
while ((quantity = Integer
.parseInt(prompt("Enter the quantity:      "))) <= 0)
;
while ((price = Double
.parseDouble(prompt("Enter the product price: \$"))) <= 0)
;

// place values in the arrays
productNames[numOfProducts] = productName;
quantities[numOfProducts] = quantity;
prices[numOfProducts] = price;
transactions[numOfProducts] = numOfTransactions;

// increment the num of products
numOfProducts = numOfProducts + 1;

// perform the calculations
subTotal = quantity * price;
if (subTotal>=50){
System.out.println(getDiscount(discountAmount));
}

} while ((numOfProducts < MAX_PRODUCTS)
&& (prompt("Another product (y/n)? ").toLowerCase().charAt(
0) == 'y'));

gstAmount = GST_RATE * (subTotal - discountAmount);
totalPrice = subTotal - discountAmount + gstAmount;

// display the output
printReceipt(productNames, quantities, prices, numOfProducts,
subTotal, discountAmount, gstAmount, totalPrice);

System.out.println("\nTransaction End " + numOfTransactions);

} while (prompt("\nQuit the program (y/n)? ").toLowerCase().charAt(0) != 'y');

printTransaction(numOfTransactions, subTotal, numOfProducts,
MAX_TRANSACTIONS, grossProfit, discountAmount, transactions);
}
}
1
Contributor
1
1 Minute
Discussion Span
7 Months Ago
Last Updated
2
Views
King1010
Newbie Poster
2 posts since Nov 2012
Reputation Points: 0
Skill Endorsements: 0

Whoops posted before I could explain what I needed, I need it to show in the output Transaction 1 subtotal:
Transaction 2 subtotal:
etc,etc. I cannot figure out how to make that show in the output, the best I can get is just for the output to have the math already done and only show the total.

King1010
Newbie Poster
2 posts since Nov 2012
Reputation Points: 0