Monday, February 3, 2014

Code Java Store

import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.IOException;

public class Store
{
int x = 0, y = 0;
double tax = 0.525;
double stotal;
double total;
double answer;



void menu()
{
try
{
Scanner kb = new Scanner(System.in);


System.out.println("\nYou basket COntains:" + x + "banana(s) and" + y + " cherry(ies)\n");

System.out.println("Please choose one of these option, (1-3)"
+ "\n\t1. Browse Products" + "\n\t2. Check out" + "\n3. Cancel");
byte ch = kb.nextByte();

if (ch == 1)
pro();
else if (ch == 2)
check();
else if (ch == 3)
System.exit(0);
else if (ch <= 0 || ch > 3)
{
System.out.println("Invalid input");
menu();
}
}

catch (Exception e)
{
System.out.println("Invalid input");
menu();
}
}

void pro()
{
try
{
Scanner kb = new Scanner(System.in);
System.out.println("Please choose one of these option, (1-3)"
+ "\n\t1. Banana: $2.00" + "\n\t2. Cherry: $1.00" + "\n3. Cancel");
byte ans = kb.nextByte();

if (ans == 1)
{
Scanner kb1 = new Scanner(System.in);
System.out.println("Do you want to buy this item? (y/n");
String a1 = kb1.nextLine();

char a2 = a1.charAt(0);

if (a2 == 'y' || a2 == 'Y')
{
Scanner kb2 = new Scanner(System.in);
System.out.println("How many do you wish to buy? ");
x = kb2.nextInt();
menu();
}

else if (a2 == 'n' || a2 == 'N')
{
pro();
}

else
{
System.out.println("Invalid input");
pro();
}
}

else if (ans == 2)
{
Scanner kb3 = new Scanner(System.in);
System.out.println("Do you want to buy this item? (y/n");
String a3 = kb3.nextLine();

char a4 = a3.charAt(0);

if (a4 == 'y' || a4 == 'Y')
{
Scanner kb4 = new Scanner(System.in);
System.out.println("How many do you wish to buy? ");
y = kb4.nextInt();
menu();
}

else if (a4 == 'n' || a4 == 'N')
{
pro();
}

else
{
System.out.println("Invalid input");
pro();
}
}

else if (ans == 3)
System.exit(0);
else if (ans <= 0 || ans > 3)
{
System.out.println("Invalid input");
pro();
}
}
catch (Exception e)
{
System.out.println("Invalid input");
menu();
}
}

void check()
{
Scanner kb = new Scanner(System.in);

DecimalFormat f = new DecimalFormat("$#,##0.00");

if (x > 0 && y == 0)
{
total = x * 2.00;

stotal = total * tax;

answer = stotal + total;

System.out.println("You've purchased " + x + "banana(s) for" + f.format(answer)
+ "including tax.");
}


else if (y > 0 && x == 0)
{
total = y * 1.00;

stotal = total * tax;

answer = stotal + total;

System.out.println("You've purchased " + y + "cherry(ies) for" + f.format(answer)
+ "including tax.");
}

else if (y > 0 && x >0)
{
total = x * 2.00;

stotal = total * tax;

answer = stotal + total;

double total1, stotal1;

total1 = y * 1.00;

stotal1 = total1 * tax;

double answer1 = stotal + total1;

double answer2 = answer + answer1;

System.out.println("You've purchased " + x + "banana(s) and " + y + "cherry(ies) for" + f.format(answer2)
+ "including tax.");
}

else
{

try
{
System.out.println("You didn't chose anything. Choose ont of these options. (1-2)"
+ "\n\t1. Browse Products" + "\n\t2. Exit/Cancel");

byte var = kb.nextByte();


if (var == 1)
pro();
else if (var == 2)

System.exit(0);

}
catch (Exception e)

{
System.out.println("Invalid input.\n");
check();

}

}


}

public static void main(String[] args)
{
Store f = new Store();
f.menu();
}
}

No comments:

Post a Comment