Created
March 7, 2014 20:21
-
-
Save gersande/9419236 to your computer and use it in GitHub Desktop.
A super simple intro to java cashier's calculator that i did for a blog post assignment thingy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------------------- | |
// Cashier Calculator | |
// Written by Gersande La Fleche | |
//----------------------------------------- | |
/* | |
*/ | |
import java.util.Scanner; | |
import java.text.DecimalFormat; // Just in case I decide to use it | |
import java.util.Arrays; | |
public class CashierCalculator { | |
// Initialize the calculator | |
public static void main(String[] args) { | |
// Initialize a new Scanner object | |
Scanner scan = new Scanner(System.in); | |
// Going to define the minimums and maximums that don't change in this bit | |
int minnumofitems = 1; | |
int maxnumofitems = 10; | |
double mingrate = 0; | |
double maxgrate = 18; | |
double minqrate = 0; | |
double maxqrate = 22; | |
double minprice = 1; | |
double maxprice = 1000; | |
// These are user defined | |
int numofitems = 0; | |
double[] pricearray; | |
// these are function defined | |
double subtotal; | |
double gst = -1; | |
double qst = -1; | |
double totalprice; | |
// strings asking the user for stuff | |
String enteritems = "Please enter the number of items bought [1...10] : "; | |
String enterprice = "Please enter the price of item "; | |
String enterprice1 = " [1...1000] : "; | |
String entergst = "Please enter the tax rate of GST in %% [0...100] : "; | |
String enterqst = "Please enter the tax rate of QST in %% [0...100] : "; | |
// now let's deal with input | |
int error1 = -1; | |
do { | |
System.out.print(enteritems); | |
numofitems = scan.nextInt(); | |
error1 ++; | |
} while (numofitems > 10 || numofitems < 1 || numofitems == 0); | |
// let's build the array of prices | |
int error2 = 0; | |
pricearray = new double[numofitems]; | |
int count = 0; | |
int n = 1; | |
do { | |
do { | |
System.out.print(enterprice + n + enterprice1); | |
pricearray[count] = scan.nextDouble(); | |
if (pricearray[count] < minprice || pricearray[count] > maxprice ) { | |
error2 ++; | |
} | |
} while (pricearray[count] < minprice || pricearray[count] > maxprice || pricearray[count] == 0); | |
count ++; | |
n ++; | |
} while (count != numofitems); | |
// let's figure out what is the QST RATE | |
int error3 = -1; | |
do { | |
System.out.print(entergst); | |
gst = scan.nextDouble(); | |
error3 ++; | |
} while (gst == -1 || gst < mingrate || gst > maxgrate); | |
int error4 = -1; | |
do { | |
System.out.print(enterqst); | |
qst = scan.nextDouble(); | |
error4 ++; | |
} while (qst == -1 || qst < minqrate || qst > maxqrate); | |
// TIME TO ADD THINGS OMG | |
int totalerrors = error1 + error2 + error3 + error4; | |
if (totalerrors > 0) { | |
System.out.println("You entered " + totalerrors + " wrong inputs."); | |
} | |
// A function to display the output | |
subtotal = getSubtotal(pricearray, numofitems); | |
System.out.println("Sub total : $" + subtotal); | |
double gst1 = getPercent(gst); | |
double gstcalc = calcGST(gst1, subtotal); | |
System.out.println("GST is : $" + gstcalc); | |
double qrt1 = getPercent(qst); | |
double qstcalc = calcQST(qst, subtotal, gstcalc); | |
System.out.println("QST is : $" + qstcalc); | |
totalprice = getTotal(subtotal,gstcalc,qstcalc); | |
System.out.println("Total is : $" + totalprice); | |
} | |
// A function to calculate the subtotal | |
public static double getSubtotal(double[] n, int a) { | |
int c = 0; | |
double value = 0; | |
while (c != a) { | |
value = value + n[c]; | |
c ++; | |
} | |
return value; | |
} | |
// A function to calculate the GST | |
public static double getPercent(double n) { | |
double a = n / 100; | |
return a; | |
} | |
public static double calcGST(double n, double a) { | |
double z = a * n; | |
return z; | |
} | |
// A function to calculate the QST | |
public static double calcQST(double a, double b, double c) { | |
double z = (b + c) * a; | |
return z; | |
} | |
// A function to calculate the total | |
public static double getTotal(double a, double b, double c) { | |
double z = a + b + c; | |
return z; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment