Hi All
Im working on an exercise from a book im learning java with.
It is a console app, i have to make a menu 1-4 and then evaluate what number the user entered, carry out the command and then return to the menu.
My problem is that i tried to put the menu in a loop and it wouldnt work so i placed the if & else if statements in a loop and that works but i want the menu to reappear once the command has completed.
Here is the code for the reactor tester class
import java.io.*;
import java.util.*;
class ReactorTester
{
private static BufferedReader buff;
private static StringTokenizer st;
private static String s;
private static char[] ca;
private static char c;
// define main method
public static void main(String[] args) throws IOException
{
//buff = new BufferedReader(new InputStreamReader( System.in ));
InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader stdin = new BufferedReader( isr );
char reply;
Reactor b = new Reactor (); //generate object to test
// increase, decrease, display, help, quit
System.out.println("****************************************");
System.out.println("[1] Increase Temperature");
System.out.println("[2] Decrease Temperature");
System.out.println("[3] Display Current Temperature");
System.out.println("[4] HELP");
System.out.println("[5] Quit");
// get user input
String input = stdin.readLine();
// convert string into an int value
int choice = Integer.parseInt( input );
for (int i = 0;choice != 4;i++)
{
//use switch statement to determine what to do
if (choice == 1)
{
boolean error = b.increaseTemp();
if (error) //check if increase raised an error
{
System.out.println("WARNING: TEMPERATURE EXCEEDING SAFE: ALARM RAISED!!!!");
}
else
{
System.out.println("Temperature after increase is ");
System.out.println(b.getTemperature());
}
}
else if (choice == 2)
{
boolean colderror = b.decreaseTemp();
if (colderror) // check if decrease caused error
{
System.out.println("WARNING: Temp at minimum, cannot decrease!!!!");
}
else
{
System.out.println("Temperature after decrease is ");
System.out.println(b.getTemperature());
}
}
else if (choice == 3)
{
System.out.println("Coming soon");
}
}
}
}
Here is the code for the Reactor class
// controls reactor temperature ensuring it doesnt go over some maximum
class Reactor
{
private static final int MAX=10;
private int temperature;
public Reactor()
{
temperature = 0; // set initial level
}
public int getTemperature ()
{
// return current level
return temperature;
}
public boolean decreaseTemp()
{
// decrease temp if temp > 0 else leave at 0 and produce error warning
boolean coldAlarm;
if (temperature > 0)
{
temperature --;
coldAlarm = false;
}
else
{
coldAlarm = true;
}
return coldAlarm;
}
public boolean increaseTemp()
{
// increase temperature if safe, drop to zero and raise alarm if not
boolean hotAlarm;
if (temperature < MAX)
{
temperature ++;
hotAlarm = false;
}
else
{
temperature =0;
hotAlarm = true;
}
return hotAlarm;
}
}
I am not very good with loops so i keep messing it up.
Any help would be greatly appreciated
Many thanks
HLA91