Assignemnt #102 Chains Fr
Code
/// Name: Caelan Dorstad
/// Period: 6
/// Program Name: ChainzFr
/// File Name: ChainzFr.java
/// Date Finished: 4/28/2016
import java.util.Scanner;
import java.util.InputMismatchException;
public class ChainzFr{
static Scanner keyboard = new Scanner(System.in);
static int currentKey = 0;
public static void main(String[] args){
System.out.println("WELCOME");
int choice = 0;
do{
showMenu();
do choice = choice();
while (choice ==0);
if (choice == 1) addKey();
if ( choice == 2) removeKey();
if (choice == 3) viewOrder();
}
while (choice!=4);
checkOut();
System.out.println();
}
public static void showMenu(){
System.out.println("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
System.out.println("1) Add Keychains to Order");
System.out.println("2) Remove Keycahins from Order");
System.out.println("3) View Current Order");
System.out.println("4) Check Out");
System.out.println("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
}
public static int choice() {
int choice = 0;
System.out.print("Choice: ");
choice = keyboard.nextInt();
System.out.println();
if (choice < 1 || choice > 4) choice = 0;
return choice;
}
public static void addKey(){
System.out.print("How many?: ");
try{
int add = keyboard.nextInt();
if ( add>0)
{
currentKey += add;
System.out.println();
System.out.println(add + " KEYCHAIN ADDED");
System.out.println("***********************");
chainz();
}
else System.out.println("ERROR");
}
catch (InputMismatchException e)
{
System.out.println("ERROR");
}
}
public static void removeKey(){
System.out.print("How many?: ");
try {
int del = keyboard.nextInt();
if ( del > 0 && del <= currentKey){
currentKey -= del;
System.out.println();
System.out.println(del + " KEYCHAIN REMOVED");
System.out.println("***********************");
chainz();
}
else System.out.println("ERROR");
}
catch (InputMismatchException e){
System.out.println("ERROR");
}
}
public static void viewOrder(){
System.out.println("YOU HAVE "+currentKey+" KEY CHAINS.");
int cost = (10*currentKey);
System.out.println("***********************");
System.out.println("TOTAL COST:$"+cost);
}
public static void checkOut(){
System.out.print("What is your name? ");
String name = keyboard.next();
chainz();
System.out.println("Keychains cost $10 each.");
int cost = (10*currentKey);
System.out.println("Total cost is $"+cost);
System.out.println("Thanks for your order, "+name+" !");
}
public static void chainz() {
System.out.println("YOU HAVE "+currentKey+" KEYCHAINS.");
}
}
Picture of the output