//package de.tageloehner.feuerhake.nonsense;
//imports needed
import java.io.*;
/**
 * TrickyCalculator calculates addition, substraction,
 * multiplication and division. It will return a slightly wrong
 * result and therefore this class is completely useless.
 *
 * Since I do not know a lot about licenses I release this under the
 * GPL http://www.gnu.org/licenses/gpl.txt
 *
 * @date Berlin 30. Sept 2004
 * @author Joerg Feuerhake joerg.feuerhake@free.penguin.org
 **/
class TrickyCalculator{

//define our global variables
private double c = 0;
private double a = 0;
private double b = 0;
private char k = ' ';
String fn = null;
String ln = null;

/**
 * Mainmethod, trigger this, none of the arguments are evaluated
 **/
public static void main(String[] args){

TrickyCalculator Trick = new TrickyCalculator();
Trick.calculate();
System.exit(0);

}
/**
 * Construktor, does nothing
 **/
public TrickyCalculator(){};
/**
 * this would be this only method you need to call when dealing with
 * an instance of TrickyCalculator. Is void, takes no arbuments.
 **/
public void calculate(){
//just loop this nonsense until the user presses stop
char done = ' ';

System.out.println("***\nThis calculator is able to add, \nsubstract, multiply and divide. \nIt will give almost always the \nright answer. Try it out.\n\n***");

while(done != 'n'){
	this.setKind();

	a = Console.readDouble("Give "+fn+", please:");
	b = Console.readDouble("Give "+ln+", please:");

	switch(k){
		case 'a': c = add(a, b); break;
		case 's': c = substract(a, b); break;
		case 'm': c = mult(a, b); break;
		case 'd': c = div(a, b); break;
		default: System.out.println("Your choice of calculation kind\nmust have been wrong."); System.exit(-1);
		};
	System.out.println("\n\n***\nThe result is close to "+doTheTrick(c)+"\n***\n't was a pleasure.");

	done = Console.readChar("\n***\nTo stop this press 'n':");
 };
 return;
}
/**
 * addition method, takes two doubles, returns the result
 **/
private double add(double a, double b){
return a+b;
 }
/**
 * substraction method, takes two doubles, returns the result
 **/
private double substract(double a, double b){
return a-b;
 }
/**
 * multiplycation method, takes two doubles, returns the result
 **/
private double mult(double a, double b){
return a*b;
 }
/**
 * division method, takes two doubles, returns the result
 **/
private double div(double a, double b){
if(b==0){
System.out.println("Hey, weirdo! Division by Zero! Go, play elsewhere");
System.exit(-1);
}
return a/b;
 }
/**
 * this method figures out, what the user might want to have calculated
 * It does reask if wrong entries are entered. Method is void and takes no
 * arguments. It sets global variables.
 **/
 private void setKind(){
 k = Console.readChar("What do you want? \n\nadd(a), \nsubstract(s), \nmultiply(m), \ndivide(d):");
switch(k){
	case 'a': fn = "summand"; ln = fn; return;
	case 's': fn = "minuent"; ln = "subtrahent"; return;
	case 'm': fn = "factor"; ln = fn; return;
	case 'd': fn = "divident"; ln = "divisor"; return;
	default: System.out.println("Your choice of calculation kind\nmust have been wrong."); setKind();
 }
 return;
 }
/**
 * This method makes sure that the returned result is wrong.
 * Therefore it is the mostimpotant method.
 **/
 private double doTheTrick(double e){
 return e + Math.random();
 }

}

//here my code ends

/**
 * Console is a class with auxilliary methods to simplify
 * keyboard entries on consoles in java. This class is taken from
 * http://www.cs.sunysb.edu/~cse110/Console.java on 30.Sept 2004
 * but was implemented the same way more then a 100 times. Since it came
 * with no license whatsoever I consider it public domain and use it here ;-)
 *
 **/

class Console {
	public static void pause () {
		System.out.print ("\nHit Enter to continue: ");
		try {
		InputStreamReader reader = new InputStreamReader(System.in);
		BufferedReader buffer = new BufferedReader(reader);
                buffer.readLine();
		}
		catch (Exception e) {
		System.exit(0);
		}
	}
	
	public static int readInt(String prompt){
		int value=0;
		System.out.print (prompt);
		try {
		InputStreamReader reader = new InputStreamReader (System.in);
		BufferedReader buffer = new BufferedReader(reader);
                String s = buffer.readLine();
		value= Integer.parseInt(s);
		}
		catch (Exception e ) {
		System.out.println(e.toString());
		Console.pause();
		System.exit(0);
		}
		return value;
		}

	public static double readDouble(String prompt){
		double value=0;
		System.out.print (prompt);
		try{
		InputStreamReader reader = new InputStreamReader(System.in);
                BufferedReader buffer = new BufferedReader(reader);
		String s = buffer.readLine();
		value=(Double.valueOf(s)).doubleValue();
		}
		catch (Exception e){
		System.out.println(e.toString());
		Console.pause();
		System.exit(0);
		}
		return value;
		}

	public static char readChar(String prompt){
		char value=' ';
		System.out.print (prompt);
		try {
		InputStreamReader reader = new InputStreamReader(System.in);
		BufferedReader buffer = new BufferedReader(reader);
		String s = buffer.readLine();
		if (s.length()==0) throw new Exception("No character entered");
		value=s.charAt(0);
		}	
		catch (Exception e){
		System.out.println(e.toString());
		Console.pause();
		System.exit(0);
		}
		return value;
		}

	public static String readString(String prompt){
		String value="";
		System.out.print(prompt);
		try {
		InputStreamReader reader = new InputStreamReader(System.in);
		BufferedReader buffer=new BufferedReader(reader);
		value=buffer.readLine();
		}
		catch(Exception e){
		System.out.println(e.toString());
		Console.pause();
		System.exit(0);
		}
		return value;
		}
}
			
				


