Skip to content

Instantly share code, notes, and snippets.

@carbonrobot
Created August 21, 2015 01:02
Show Gist options
  • Save carbonrobot/87eab9569b601c40db8f to your computer and use it in GitHub Desktop.
Save carbonrobot/87eab9569b601c40db8f to your computer and use it in GitHub Desktop.
import com.company.Product;
import com.company.Tax;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
// create products
Product[] products = new Product[]{
new Product("10001", "Surge", 12, 4.99),
new Product("20001", "Crystal Pepsi", 12, 4.99),
new Product("30001", "Hubba Bubba", 12, 4.99),
new Product("40001", "Coke 2", 12, 4.99),
new Product("50001", "Slice", 12, 4.99)
};
// create tax brackets
Tax[] taxes = new Tax[]{
new Tax("CT", 7.5),
new Tax("VT", 7.5),
new Tax("WI", 7.5),
new Tax("CA", 7.5),
new Tax("WA", 7.5),
};
// Print out Menu for user to reference
String format = "%-15s%-15s%-15s%-15s\r\n";
System.out.format(format, new String[] { "Soda Product", "Product Code", "Case Quantity", "Cost" });
System.out.format(format, new String[] { "------------", "------------", "-------------", "----" });
for (Product p : products) {
System.out.format("%-15s", p.getName());
System.out.format("%-15s", p.getCode());
System.out.format("%-15s", p.getCaseCount());
System.out.format("%-15s\r\n", p.getCost());
}
System.out.println("");
// ask questions
Scanner input = new Scanner (System.in);
Product selectedProduct = null;
while(selectedProduct == null) {
System.out.println("Please enter the Product ID: ");
String selection = input.nextLine();
// determine the product that was selected
for (Product p : products) {
if (p.getCode().equalsIgnoreCase(selection)) {
selectedProduct = p;
}
}
}
int count = 0;
while(count <= 0) {
System.out.println("Please enter the number of cases you wish to purchase: ");
String cases = input.nextLine();
try {
count = Integer.parseInt(cases);
} catch (NumberFormatException e){
count = 0;
}
}
Tax selectedTax = null;
while(selectedTax == null) {
System.out.println("Please enter the state from which you are purchasing from. (CT, VT, WI, CA, WA)");
String state = input.nextLine();
// find tax
for (Tax t : taxes) {
if (t.getState().equalsIgnoreCase(state)) {
selectedTax = t;
}
}
}
int caseCount = selectedProduct.getCaseCount();
double productPrice = selectedProduct.getCost();
double taxApplied = selectedTax.getTax();
// Calculating the Quantity of individual items ordered
double totalItems = (count * caseCount);
// Calculating the Sub total
double subTotal = (count * productPrice);
// Calculating the Sales Tax on the items
double tax = (subTotal * taxApplied)/100;
// Calculating the Grand Total
double grandTotal = (subTotal + tax);
//Output Results
System.out.println("The soda you have selected is: " + selectedProduct.getName());
System.out.println("The number of cases you ordered is: " + count);
System.out.println("The number of individual items you ordered is: " + String.format("%.0f", totalItems));
System.out.println("Your Subtotal before tax is: $" + String.format("%.2f", subTotal));
System.out.println("Your Sales Tax on these items is: $" + String.format("%.2f", tax));
System.out.println("Your Grand Total with Tax is: $" + String.format("%.2f", grandTotal));
}
}
package com.company;
public class Product {
public Product(String code, String name, int caseCount, double cost){
_code = code;
_name = name;
_caseCount = caseCount;
_cost = cost;
}
public String getCode()
{
return _code;
}
public String getName()
{
return _name;
}
public int getCaseCount()
{
return _caseCount;
}
public double getCost()
{
return _cost;
}
private String _code;
private String _name;
private double _cost;
private int _caseCount;
}
package com.company;
public class Tax {
public Tax(String state, double tax){
_state = state;
_tax = tax;
}
public String getState(){
return _state;
}
public double getTax(){
return _tax;
}
private String _state;
private double _tax;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment