I am a total newbie when it comes to coding. I have been plugging away at getting my mind around Java for about a week.
Background
I have been sick for a while with symptoms that resemble some form of chronic fatigue, and my doctor handed me a flow chart that tells me how much activity I should do before taking an equal sized break, based on how I feel.
One look at the flow chart and I realized that she could have just as well handed me a flowchart of a program heh. So I decided to make my first little program in Java.
The code its self was done in a couple of hours (I am a bit of a newbie, remember), but for about a week now, I have spent time learning and improving it when I learn something new and relevant to the project.
My goal here:
I want to learn to code and follow conventions correctly. So I dare not write another program without peer review first, in case I'm doing things wrong or inefficient. I would appreciate any and all comments that can help me improve or encourage me.
Disclaimer stuff:
- The time in minutes is shown as
x
simply because that's how it was on the flow chart. Though no, "x" isn't super 'readable'. :) - The last
StringBuilder at
the bottom should probably be turned into aSystem.out.printf
so that I can control those decimal points (I just ran the program and input 32 minutes and seen all those digits for the first time when the build was finished.. heh).
Other than that, I hope you can dissect this thing and offer some good corrections or alternative methods so I can start coding again! :)
package activity;
import java.util.Scanner;
// @author Jack
public class Activity {
//x; = time in minutes.
static int x;
//Flag to stop the loop which encloses the following indented.
static boolean timeInMinutesGiven = false;
static boolean validReply = false;
static String timeRealisticIs;
static boolean userAcceptsRejectsInput = false;
static String dayIs;
static boolean goodBadChoiceFlag = false;
//activityTimeModifier; = used in the calculation of stepUpTime.
static double activityTimeModifier;
//int stepUpTime; = the final time output.
static double stepUpTime;
static double subtractedByX;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/*
The while/do while loops handle prompting and information gathering
*/
while (!timeInMinutesGiven) {
//This loop prompts user to give a value for 'x', time in minutes.
do {
System.out.print("How long (in minutes) can you be active "
+ "today before symptoms start to appear?: ");
try {
Scanner input = new Scanner(System.in);
x = input.nextInt();
validReply = true; //If user gave an integer.
} catch (Exception e) {
System.out.println("ERROR: A whole number was expected.");
validReply = false; //If user gave !integer.
}
} while (!validReply);
//This loop prompts user for 'Are you sure?'.
do{
StringBuilder areYouSure = new StringBuilder();
areYouSure.append("Can you REALLY be active for ")
.append(x)
.append(" minutes at a time?: ");
System.out.print(areYouSure.toString());
Scanner yesNoInput = new Scanner(System.in);
timeRealisticIs = yesNoInput.nextLine();
//if and else if would both trigger this anyway.
userAcceptsRejectsInput = true;
if (timeRealisticIs.equalsIgnoreCase("yes")
|| timeRealisticIs.equalsIgnoreCase("y")) {
timeInMinutesGiven = true;
} else if (timeRealisticIs.equalsIgnoreCase("no")
|| timeRealisticIs.equalsIgnoreCase("n")) {
System.out.println("Sequence Restarted:");
} else {
System.out.println("Error: One of the following "
+ "must be entered:");
System.out.println("Yes, y, no, n");
System.out.println("Asking again:");
userAcceptsRejectsInput = false;
}
}while (userAcceptsRejectsInput == false);
}
System.out.println(x + " minutes accepted.");
//Asks user if dayIs good or bad.
//The result sets the activityTimeModifier variable.
do {
System.out.println("Is this a good or bad day?");
System.out.print("(Expected: Good/Bad): ");
Scanner inputDay = new Scanner(System.in);
dayIs = inputDay.nextLine();
/*
The if statements handle the results of the
information gathered during the while loops
*/
if (dayIs.equalsIgnoreCase("good")
|| dayIs.equalsIgnoreCase("g")) {
System.out.println("A good day!! How wonderful!");
System.out.print("Since you are feeling so good, ");
activityTimeModifier = 0.2;
goodBadChoiceFlag = true;
} else if (dayIs.equalsIgnoreCase("bad")
|| dayIs.equalsIgnoreCase("b")) {
activityTimeModifier = 0;
goodBadChoiceFlag = true;
System.out.print("Thats alright, then ");
} else {
StringBuilder goodBadExpected = new StringBuilder();
goodBadExpected.append("Error: 'Good' or 'bad' expected.\n")
.append("You typed: '").append(dayIs).append("'\n")
.append("Asking again:");
System.out.println(goodBadExpected.toString());
}
} while (!goodBadChoiceFlag);
/*
The following code runs the final calculation of the result
of the 'while' input gathering and the result of the
'if' input and variable initialization.
*/
stepUpTime = x + activityTimeModifier * x;
subtractedByX = stepUpTime - x;
StringBuilder balancedTimeString = new StringBuilder();
balancedTimeString.append("your balanced training time ")
.append("for today will be set to: ")
.append(stepUpTime)
.append(" minutes.\n")
.append("That is ")
.append(subtractedByX)
.append(" minutes longer than ")
.append(x)
.append(".");
System.out.println(balancedTimeString.toString());
}
}