CWE Program update v 1.0.1

Here's the update:

http://www.megaupload.com/?d=0HTLNTRR

And the code:

Code:
import java.io.*;
import java.util.*;
import java.text.*;
public class Tylenol
{
  public static void main(String[] args)
  {
    DecimalFormat a = new DecimalFormat(".00");
    Date date = new Date();
    double n = 1;
    do{
      try{
        System.out.println("    _/    _/    _/_/_/    _/_/    ");
        System.out.println("   _/  _/    _/        _/    _/   ");
        System.out.println("  _/_/        _/_/    _/_/_/_/    ");
        System.out.println(" _/  _/          _/  _/    _/     ");
        System.out.println("_/    _/  _/_/_/    _/    _/      ");
        System.out.println(date.toString()+"\n");
        System.out.println("Welcome to Ksa's disolved acetaminophen calculator!\n");
        System.out.println("WARNING: This program outputs toxicity information for acetaminophen only.");
        System.out.println("         Daily use of acetaminophen can be hazardous regardless of output.");
        System.out.println("");
        System.out.println("Select a topic and press enter:");
        System.out.println("");
        System.out.println("1. Check if a solution has safe amounts of disolved acetaminophen.");
        System.out.println("2. Give me the acetaminophen water solubility value for a certain temperature.");
        System.out.println("3. Exit");
        Scanner input = new Scanner(System.in);
        double entry = input.nextDouble();
        n = 2;
        if(entry==1)
        {
          Scanner input3 = new Scanner(System.in);
          System.out.println("");
          System.out.println("Enter solution temperature before filtration:");
          double temperature = input3.nextDouble();
          if(temperature < 0)
          {
            System.out.println("Water freezes at zero degrees celsius.(Invalid value)");
            System.out.println("Press Enter to abord.");
            Scanner input4 = new Scanner(System.in);
            String entrya = input4.nextLine();
            System.exit(0);
          }
          System.out.println("");
          System.out.println("Enter amount of pills:");
          double pills = input.nextDouble();
          if(pills < 0)
          {
            System.out.println("Invalid entry.");
            System.out.println("Press Enter to abord.");
            Scanner input4 = new Scanner(System.in);
            String entrya = input4.nextLine();
            System.exit(0);
          }
          System.out.println("");
          System.out.println("Enter acetaminophen content of one pill, in miligrams (mg):");
          double apap = input.nextDouble();
          if(apap < 0)
          {
            System.out.println("Invalid entry.");
            System.out.println("Press Enter to abord.");
            Scanner input4 = new Scanner(System.in);
            String entrya = input4.nextLine();
            System.exit(0);
          }
          System.out.println("");
          System.out.println("Enter the total water volume, in millilitres (mL):");
          double water = input.nextDouble();
          if(water < 0)
          {
            System.out.println("Invalid entry.");
            System.out.println("Press Enter to abord.");
            Scanner input4 = new Scanner(System.in);
            String entrya = input4.nextLine();
            System.exit(0);
          }
          double solubility = (0.0006*Math.pow(temperature,2) + 0.0165*temperature + 0.7221)*10;
          double intake = pills*apap;
          double intake2 = water*solubility;
          if(temperature > 50)
          {
            System.out.println("Your temperature is unsafe and may result in high amounts of disolved acetaminophen.\n Please lower the temperature below 50 degrees Celsius.");
            System.out.println("Press Enter to abord.");
            Scanner input4 = new Scanner(System.in);
            String entrya = input4.nextLine();
            System.exit(0);
          }
          if(pills > 50)
          {
            System.out.println("You have entered "+ pills+" pills. That may be unsafe for other reasons.");
            System.out.println("Press Enter to abord.");
            Scanner input5 = new Scanner(System.in);
            String entrya = input5.nextLine();
            System.exit(0);
          }
          if(pills*apap < water*solubility && pills*apap>8000)
          {
            System.out.println("The solution contains "+ a.format(intake/1000)+ " g of disolved acetaminophen.");
            System.out.println("Ingestion can result in fatal acetaminophen overdose.");
            System.out.println("Death occurs aproximatly 8 hours after ingestion if no antidote is provided.");
            System.out.println("Reason of death: Liver failure.");
            System.out.println("Using "+ 2.5*pills+" mL of water instead of "+water+" mL should render the solution safe to ingest.");
            System.out.println("Press Enter to abord.");
            Scanner input6 = new Scanner(System.in);
            String entrya = input6.nextLine();
            System.exit(0);
          }
          else if(pills*apap >= water*solubility && water*solubility>8000)
          {
            System.out.println("The solution contains "+ a.format(intake2/1000)+ " g of disolved acetaminophen.");
            System.out.println("Ingestion can result in fatal acetaminophen overdose.");
            System.out.println("Death occurs aproximatly 8 hours after ingestion if no antidote is provided.");
            System.out.println("Reason of death: Liver failure.");
            System.out.println("Using "+ a.format(2.5*pills)+" mL of water instead of "+a.format(water)+" mL should render the solution safe to ingest.");
            System.out.println("Press Enter to abord.");
            Scanner input7 = new Scanner(System.in);
            String entrya = input7.nextLine();
            System.exit(0);
          }
          else if(pills*apap < water*solubility && pills*apap>2000)
          {
            System.out.println("The solution contains "+ a.format(intake/1000)+ " g of disolved acetaminophen.");
            System.out.println("Ingestion is hazardous and can result in permanent liver damage.");
            System.out.println("Using "+ a.format(2.5*pills)+" mL of water instead of "+a.format(water)+" mL should render the solution safe to ingest.");
            System.out.println("Press Enter to abord.");
            Scanner input8 = new Scanner(System.in);
            String entrya = input8.nextLine();
            System.exit(0);
          }
          else if(pills*apap >= water*solubility && water*solubility>2000)
          {
            System.out.println("The solution contains "+ a.format(intake2/1000)+ " g of disolved acetaminophen.");
            System.out.println("Ingestion is hazardous and can result in permanent liver damage.");
            System.out.println("Using "+ a.format(2.5*pills)+" mL of water instead of "+a.format(water)+" mL should render the solution safe to ingest.");
            System.out.println("Press Enter to abord.");
            Scanner input9 = new Scanner(System.in);
            String entrya = input9.nextLine();
            System.exit(0);
          }
          else{
            System.out.println(pills*apap < water*solubility ? "The solution contains "+a.format(intake/1000)+ " g of disolved acetaminophen." : "The solution contains "+a.format(intake2/1000)+ " g of disolved acetaminophen.");
            System.out.println("Ingesting the solution is safe occasionally, given it only contains water and acetaminophen ");
            System.out.println("Press Enter to abord.");
            Scanner input9 = new Scanner(System.in);
            String entrya = input9.nextLine();
            System.exit(0);
          }
        }
        else if(entry==2)
        {
          System.out.println("");
          System.out.println("Enter water temperature in degrees Celsius from 0 to 100C:");
          Scanner input2 = new Scanner(System.in);
          double temperature = input2.nextDouble();
          if(temperature < 0)
          {
            System.out.println("Water freezes at zero degrees Celsius(Invalid value)");
            System.out.println("Press Enter to abord.");
            Scanner input4 = new Scanner(System.in);
            String entrya = input4.nextLine();
            System.exit(0);
          }
          else if(temperature > 100)
          {
            System.out.println("Water boils at 100 degrees Celsius. (Invalid value)");
            System.out.println("Press Enter to abord.");
            Scanner input4 = new Scanner(System.in);
            String entrya = input4.nextLine();
            System.exit(0);
          }
          else{
            double solubility = 0.0006*Math.pow(temperature,2) + 0.0165*temperature + 0.7221;
            System.out.println("The solubility of acetaminophen in water at "+temperature+" degrees Celsius is "+a.format(solubility)+"g/100mL");
            System.out.println("Press Enter to abord.");
            Scanner input9 = new Scanner(System.in);
            String entrya = input9.nextLine();
            System.exit(0);
          }
        }
        else{System.exit(0);}
      }
      catch(Exception e){
        System.out.println("");
        System.out.println("Invalid entry. Please try again.\n");
      }
    }while(n==1);
  }
}
 
The changes have to do with clarity but the algorithm is the same as in the first version, it's unbeatable lol. Works every time.
 
Top