PeriodicTable.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package eu.tankernn.periodictable;
  2. import java.io.FileNotFoundException;
  3. import java.util.Collection;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Optional;
  7. import java.util.Scanner;
  8. public class PeriodicTable {
  9. private static Map<String, Element> table = new HashMap<String, Element>();
  10. static Scanner in = new Scanner(System.in);
  11. static boolean interrupt = false;
  12. public static void main(String[] args) {
  13. System.out.println("How many decimals should be used for molarMass values?");
  14. try {
  15. table = TableLoader.loadData(in.nextInt());
  16. } catch (FileNotFoundException e) {
  17. e.printStackTrace();
  18. return;
  19. }
  20. System.out.println("Enter a command, alternatively the short name, full name or atomic number of any element.");
  21. while (!interrupt) {
  22. try {
  23. System.out.print(">");
  24. executeCommand(in.nextLine());
  25. } catch (Exception ex) {
  26. System.out.println("There was an exception: " + ex.getMessage());
  27. ex.printStackTrace();
  28. }
  29. }
  30. in.close();
  31. }
  32. static void executeCommand(String command) {
  33. switch (command) {
  34. case "molarMass":
  35. System.out.println(new Substance(in.nextLine()).getMolarMass());
  36. break;
  37. case "info":
  38. System.out.println(new Substance(in.nextLine()).getComponentListing());
  39. break;
  40. case "concentration":
  41. System.out.println("Enter a chemical term for any substance, eg. 'NaCl': ");
  42. Substance term = new Substance(in.nextLine());
  43. System.out.println("How many grams of " + term.getSmallName() + "?");
  44. term.setMass(in.nextDouble());
  45. Solution so = new Solution(term);
  46. System.out.println("What volume of water? (dm3)");
  47. so.setVolume(in.nextDouble());
  48. System.out.println(so.getConcentration());
  49. break;
  50. case "help":
  51. System.out.println("concentration: calculates concentration of a solutiuon." + "\n"
  52. + "molarMass: calculates molar mass of given substance." + "\n"
  53. + "exit: exits the program.");
  54. break;
  55. case "exit":
  56. interrupt = true;
  57. break;
  58. default:
  59. try {
  60. System.out.println(getElement(command));
  61. } catch (NullPointerException ex) {
  62. System.out.println("No such command or element. Type 'help' for a list of commands.");
  63. }
  64. break;
  65. }
  66. }
  67. static Element getElement(String query) throws NullPointerException {
  68. try {
  69. if (table.get(query) != null)
  70. return table.get(query);
  71. } catch (NullPointerException ex) {
  72. System.out.println("No element with short name " + query);
  73. }
  74. Collection<Element> elements = table.values();
  75. Optional<Element> element = elements.stream().filter(e -> e.getFullName().equals(query)).findFirst();
  76. try {
  77. int i = Integer.parseInt(query);
  78. element = elements.stream().filter(e -> e.getAtomicNumber() == i).findFirst();
  79. } catch (NumberFormatException ex) {
  80. }
  81. if (element.isPresent())
  82. return element.get();
  83. else
  84. throw new NullPointerException("The element " + query + " was not found.");
  85. }
  86. }