Advertisement
Guest User

Atyom <3

a guest
Feb 19th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. package assignments;
  2.  
  3. import java.text.ParseException;
  4. import java.util.*;
  5.  
  6. import javax.swing.JOptionPane;
  7. import java.io.*;
  8. import java.util.concurrent.ThreadLocalRandom;
  9. import java.io.IOException;
  10. import java.time.format.DateTimeFormatter;
  11. import java.time.LocalDateTime;
  12.  
  13. public class Assignments {
  14. //useful links
  15. //https://howtodoinjava.com/java/io/how-to-create-a-new-file-in-java/
  16. //page page 738
  17. // https://d2l.cbe.ab.ca/d2l/le/content/556822/viewContent/7796065/View
  18.  
  19. public static String[] sales;
  20.  
  21. public static void main(String[] args) {
  22. int runOnce = 0;
  23. if (runOnce == 0) {
  24. runOnce = example();
  25. }
  26.  
  27. GUI();
  28.  
  29. }
  30.  
  31. public static int example() {
  32.  
  33. try {
  34. readFile("write.txt");
  35. } catch (IOException e) {
  36. System.out.println("error in example() " + e);
  37. }
  38.  
  39. return 1;
  40. }
  41.  
  42. //TODO call the sale, choose which box, remove box
  43. public static void GUI() {
  44.  
  45. int boxAmount = 5; //choose amount of boxes
  46. int[] scentBoxes = new int[boxAmount];
  47.  
  48. String employee = askForString("Please enter employee name"); //enter employee name
  49. String JAName = askForString("Please enter JA company name");
  50. scentBoxes = populateBoxesAskUser(scentBoxes, askForString("Please enter box scent number one"), askForString("Please enter box scent number two"));
  51. while (true) {
  52. int currentChoice = askForOptions3("Log a sale", "Show Boxes", "Save files and quit");
  53. switch (currentChoice) {
  54. case 0: //log a sale
  55.  
  56. int removedBox = logSale(employee, JAName, scents, scentBoxes);
  57.  
  58. scentBoxes[removedBox] = 0;
  59. break;
  60. case 1: // print out array
  61. printArray(scents, scentBoxes);
  62. break;
  63. case 2: // save and quit
  64. try {
  65. readLineAmount("write.txt");
  66. writeFile("write.txt", salesLogs);
  67. System.exit(0);
  68. } catch (Exception e) {
  69. try {
  70. FileWriter fw = new FileWriter("write.txt", true);
  71. System.out.println("New file created!");
  72. } catch (Exception B) {
  73.  
  74. }
  75. System.out.println("Error in case 3 GUI");
  76. }
  77. break;
  78. default:
  79. break;
  80.  
  81. }
  82. }
  83. }
  84. public static int salesAmount = 0;
  85. public static String[] salesLogs = new String[5];
  86. public static String[] scents = new String[3];
  87.  
  88. public static int logSale(String names, String JAName, String[] scents, int[] scentBoxes) {
  89. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
  90. LocalDateTime now = LocalDateTime.now();
  91. int boxChoice = Integer.parseInt(JOptionPane.showInputDialog("Choose the box no. you've sold"));
  92. if (scentBoxes[boxChoice] != 0) {
  93. System.out.println(names + " From the " + JAName + " Company " + "has sold a " + scents[scentBoxes[boxChoice]] + " scent cube no." + boxChoice + " On " + dtf.format(now));
  94. salesLogs[salesAmount] = names + " From the " + JAName + " Company " + "has sold a " + scents[scentBoxes[boxChoice]] + " scent cube no." + boxChoice + " On " + dtf.format(now);
  95. salesAmount += 1;
  96. } else {
  97. System.out.println("Error, box already sold");
  98. }
  99. return boxChoice;
  100. }
  101.  
  102. public static void readFile(String fileName) throws IOException {
  103.  
  104. String token1 = "";
  105.  
  106. Scanner inFile1 = new Scanner(new File(fileName)).useDelimiter("\\s*null\\s*");
  107.  
  108. List<String> temps = new ArrayList<String>();
  109.  
  110. // while loop
  111. while (inFile1.hasNext()) {
  112. // find next line
  113. token1 = inFile1.next();
  114. temps.add(token1);
  115. }
  116. inFile1.close();
  117.  
  118. String[] tempsArray = temps.toArray(new String[0]);
  119.  
  120. for (String s : tempsArray) {
  121. System.out.println(s);
  122. }
  123. }
  124.  
  125. public static void writeFile(String filename, String[] x) throws IOException {
  126.  
  127. FileWriter fw = new FileWriter(filename, true);
  128. for (int i = 0; i < linecount; i++) {
  129. fw.write(String.format("%n"));
  130. }
  131. for (int i = 0; i < x.length; i++) {
  132.  
  133. fw.write(x[i] + "" + String.format("%n"));
  134.  
  135. }
  136.  
  137. fw.close();
  138. }
  139.  
  140. public static int linecount;
  141.  
  142. public static void readLineAmount(String fileName) throws Exception {
  143. File f1 = new File(fileName);
  144. linecount = 0; //Intializing linecount as zero
  145. FileReader fr = new FileReader(f1); //Creation of File Reader object
  146. BufferedReader br = new BufferedReader(fr); //Creation of File Reader object
  147. String s;
  148. while ((s = br.readLine()) != null) //Reading Content from the file line by line
  149. {
  150. linecount++; //For each line increment linecount by one
  151.  
  152. }
  153. fr.close();
  154. System.out.println("Number of lines in the Files:" + linecount);
  155. }
  156.  
  157. public static void printArray(String[] scents, int[] scentBoxes) {
  158. for (int i = 0; i < scentBoxes.length; i++) {
  159. System.out.println("Box no." + i + " is " + scents[scentBoxes[i]]);
  160. }
  161.  
  162. }
  163.  
  164. public static int askForOptions(String option1, String option2) {
  165. int choice = 0;
  166.  
  167. String[] options = new String[]{option1, option2};
  168. choice = JOptionPane.showOptionDialog(null, option1 + " or " + option2 + "?", "Choose an option",
  169. JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
  170.  
  171. return choice;
  172. }
  173.  
  174. public static int askForOptionsTitle(String option1, String option2, int boxNumber) {
  175. int choice = 0;
  176.  
  177. String[] options = new String[]{option1, option2};
  178. choice = JOptionPane.showOptionDialog(null, option1 + " or " + option2 + "?", "Choose box no." + boxNumber,
  179. JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
  180.  
  181. return choice;
  182. }
  183.  
  184. public static int askForOptions3(String option1, String option2, String option3) {
  185. int choice = 0;
  186.  
  187. String[] options = new String[]{option1, option2, option3};
  188. choice = JOptionPane.showOptionDialog(null, option1 + " or " + option2 + "?", "Choose an option",
  189. JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
  190.  
  191. return choice;
  192. }
  193.  
  194. public static String askForString(String info) {
  195.  
  196. boolean ok = false;
  197. String i = "";
  198. while (!ok) {
  199. try {
  200. i = (JOptionPane.showInputDialog(info));
  201. ok = true;
  202. return i;
  203. } catch (Exception e) {
  204. JOptionPane.showMessageDialog(null, "Input invalid, please try again.");
  205. }
  206.  
  207. }
  208. return i;
  209.  
  210. }
  211.  
  212. public static int[] populateBoxesAskUser(int[] scentBoxes, String box1, String box2) {
  213. int currentChoice = 0;
  214. scents[0] = "none";
  215. scents[1] = box1;
  216. scents[2] = box2;
  217. for (int i = 0; i < scentBoxes.length; i++) {
  218. currentChoice = askForOptionsTitle(box1, box2, i);
  219.  
  220. if (currentChoice == 0)//compensate for the fact that scent 0 is null
  221. {
  222. currentChoice = 1;
  223. } else if (currentChoice == 1) {
  224. currentChoice = 2;
  225. }
  226.  
  227. scentBoxes[i] = currentChoice;
  228. }
  229. return scentBoxes;
  230. }
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement