Skip to content

Instantly share code, notes, and snippets.

@LBRapid
Created September 28, 2009 15:46
Show Gist options
  • Save LBRapid/195523 to your computer and use it in GitHub Desktop.
Save LBRapid/195523 to your computer and use it in GitHub Desktop.
/***************************************
"The Final Frontier" Web Store (Assignment 1)
Author: John Dyer
Class: CS401
Section: Wednesday
Version: 1.0
Date: 9/24/09
Description: Basic simulation of an "online" store with it's theme being the Final Frontier.
****************************************/
import java.util.*;
import java.lang.*;
import java.text.*;
public class FinalFrontier
{
public static void main(String[] args) {
// define the variables used to store the user's infomartion and purchases
// items are set to -1 by default
String name, state, cc;
int numTelescope = -1, numShuttle = -1, numTrek = -1, addFreeDisc = -1, freeDiscs = 0, again = 1;
int shuttlePrice = 50;
boolean discounts = false;
double subTotal = 0, total = 0;
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);
// create a Scanner for user input
Scanner keyboard = new Scanner(System.in);
// start main loop of program (repeats until there are no more customers)
while(again == 1) {
name = "";
state = "";
cc = "";
// get some basic information from the user before showing store contents
System.out.println("Welcome to The Final Frontier Web Store!\n");
System.out.print("Please enter your name to continue: ");
name = keyboard.next();
System.out.print("\nPlease enter your state of residence to continue: ");
state = keyboard.nextLine();
// display the menu for the store
System.out.print("\n\n\n\n\n-------------------------------------------------\n");
System.out.print("| Product | Price ------------\n");
System.out.print("-------------------------------------------------\n");
System.out.print("| Mini Hubble Telescope | $100.00 ------------\n");
System.out.print("| Space Shuttle model | $50.00 ------------\n");
System.out.print("| Star Trek Blu-ray | $20.00 ------------\n");
System.out.print("-------------------------------------------------\n");
System.out.println("***Current Discounts***");
System.out.println("Every 6th Star Trek disc purchased is free");
System.out.println("***");
System.out.println("If 2 or more Space Shuttles are purchased,\nthe price is reduced to $40.00 a piece for each model");
System.out.println("***");
System.out.println("If the total bill is $200.00 (before tax), take 10% off\n");
// ask the user how many of each item they would like
while(numTelescope < 0) {
System.out.println("How many Mini Hubble Telescopes would you like to purchase?");
numTelescope = keyboard.nextInt();
}
while(numShuttle < 0) {
System.out.println("How many Space Shuttle models would you like to purchase?");
numShuttle = keyboard.nextInt();
}
while(numTrek < 0) {
System.out.println("How many Star Trek Blu-ray discs would you like to purchase?");
numTrek = keyboard.nextInt();
}
// check to see if the user would like a free star trek blu-ray
if(numTrek % 5 == 0 && numTrek != 0) {
System.out.println("You qualify for an additional Star Trek Blu-ray at no extra charge!");
System.out.print("Would you like to add one more? (1=yes,2=no) ");
addFreeDisc = keyboard.nextInt();
if(addFreeDisc == 1) {
numTrek += 1;
}
}
// show the subtotal before any discounts or taxes
// if no items were selected, no subtotal will be shown
if(numTelescope > 0 || numShuttle > 0 || numTrek > 0){
// calculate subtotal
subTotal = (100 * numTelescope) + (shuttlePrice * numShuttle) + (20 * numTrek);
System.out.println("\n\nSubtotal: (without discounts) ");
if(numTelescope > 0) {
System.out.println("Mini Hubble Telescopes - $100.00x" + numTelescope);
}
if(numShuttle > 0) {
System.out.println("Space Shuttle Models - $50.00x" + numShuttle);
}
if(numTrek > 0) {
System.out.println("Star Trek Blu-ray discs - $20.00x" + numTrek);
}
System.out.println("Your subtotal is: " + formatter.format(subTotal));
}
// check for discounts and set the discounts boolean to 1 if any are existing
if(numTrek > 4 || numShuttle >= 2 || subTotal > 200 || state == "PA"){
discounts = true;
}
// display discounts available if discounts bool == 1 and calculate total
if(discounts == true) {
System.out.println("\nDiscounts you qualify for");
System.out.println("---------------------------");
if(numShuttle >= 2) {
System.out.println("Each space shuttle model will only cost you $40.00");
shuttlePrice = 40;
}
if(numTrek >= 5) {
System.out.println("You qualify for " + (numTrek / 5) + " free Star Trek Blu-ray discs");
freeDiscs = (numTrek / 5);
}
subTotal = (100 * numTelescope) + (shuttlePrice * numShuttle) + ((20 * numTrek) - (20 * freeDiscs));
// calculate subTotal again with discounts and check to see if the customer
// qualifies for 10% off their order
if(subTotal > 200) {
System.out.println("You qualify for 10% off your entire order");
subTotal = subTotal - (subTotal * .1);
}
if(state == "PA") {
System.out.println("Because you live in PA, 6% tax was added to your order");
subTotal += (subTotal * .06);
}
// calculate and display final total after discounts and taxes
System.out.println("\n\nYour total is now: " + formatter.format(subTotal));
}
if(subTotal > 0) {
System.out.println("Please enter your credit card number to complete your purchase: ");
cc = keyboard.next();
System.out.println("Thank you for your purchase!");
System.out.println("Would you like to make another purchase? (1=yes, 0=no) ");
again = keyboard.nextInt();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment