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 {
// made a transaction
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);
}
}
King1010 0 Newbie Poster
King1010 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.