Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignments;
- import java.text.ParseException;
- import java.util.*;
- import javax.swing.JOptionPane;
- import java.io.*;
- import java.util.concurrent.ThreadLocalRandom;
- import java.io.IOException;
- import java.time.format.DateTimeFormatter;
- import java.time.LocalDateTime;
- public class Assignments {
- //useful links
- //https://howtodoinjava.com/java/io/how-to-create-a-new-file-in-java/
- //page page 738
- // https://d2l.cbe.ab.ca/d2l/le/content/556822/viewContent/7796065/View
- public static String[] sales;
- public static void main(String[] args) {
- int runOnce = 0;
- if (runOnce == 0) {
- runOnce = example();
- }
- GUI();
- }
- public static int example() {
- try {
- readFile("write.txt");
- } catch (IOException e) {
- System.out.println("error in example() " + e);
- }
- return 1;
- }
- //TODO call the sale, choose which box, remove box
- public static void GUI() {
- int boxAmount = 5; //choose amount of boxes
- int[] scentBoxes = new int[boxAmount];
- String employee = askForString("Please enter employee name"); //enter employee name
- String JAName = askForString("Please enter JA company name");
- scentBoxes = populateBoxesAskUser(scentBoxes, askForString("Please enter box scent number one"), askForString("Please enter box scent number two"));
- while (true) {
- int currentChoice = askForOptions3("Log a sale", "Show Boxes", "Save files and quit");
- switch (currentChoice) {
- case 0: //log a sale
- int removedBox = logSale(employee, JAName, scents, scentBoxes);
- scentBoxes[removedBox] = 0;
- break;
- case 1: // print out array
- printArray(scents, scentBoxes);
- break;
- case 2: // save and quit
- try {
- readLineAmount("write.txt");
- writeFile("write.txt", salesLogs);
- System.exit(0);
- } catch (Exception e) {
- try {
- FileWriter fw = new FileWriter("write.txt", true);
- System.out.println("New file created!");
- } catch (Exception B) {
- }
- System.out.println("Error in case 3 GUI");
- }
- break;
- default:
- break;
- }
- }
- }
- public static int salesAmount = 0;
- public static String[] salesLogs = new String[5];
- public static String[] scents = new String[3];
- public static int logSale(String names, String JAName, String[] scents, int[] scentBoxes) {
- DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
- LocalDateTime now = LocalDateTime.now();
- int boxChoice = Integer.parseInt(JOptionPane.showInputDialog("Choose the box no. you've sold"));
- if (scentBoxes[boxChoice] != 0) {
- System.out.println(names + " From the " + JAName + " Company " + "has sold a " + scents[scentBoxes[boxChoice]] + " scent cube no." + boxChoice + " On " + dtf.format(now));
- salesLogs[salesAmount] = names + " From the " + JAName + " Company " + "has sold a " + scents[scentBoxes[boxChoice]] + " scent cube no." + boxChoice + " On " + dtf.format(now);
- salesAmount += 1;
- } else {
- System.out.println("Error, box already sold");
- }
- return boxChoice;
- }
- public static void readFile(String fileName) throws IOException {
- String token1 = "";
- Scanner inFile1 = new Scanner(new File(fileName)).useDelimiter("\\s*null\\s*");
- List<String> temps = new ArrayList<String>();
- // while loop
- while (inFile1.hasNext()) {
- // find next line
- token1 = inFile1.next();
- temps.add(token1);
- }
- inFile1.close();
- String[] tempsArray = temps.toArray(new String[0]);
- for (String s : tempsArray) {
- System.out.println(s);
- }
- }
- public static void writeFile(String filename, String[] x) throws IOException {
- FileWriter fw = new FileWriter(filename, true);
- for (int i = 0; i < linecount; i++) {
- fw.write(String.format("%n"));
- }
- for (int i = 0; i < x.length; i++) {
- fw.write(x[i] + "" + String.format("%n"));
- }
- fw.close();
- }
- public static int linecount;
- public static void readLineAmount(String fileName) throws Exception {
- File f1 = new File(fileName);
- linecount = 0; //Intializing linecount as zero
- FileReader fr = new FileReader(f1); //Creation of File Reader object
- BufferedReader br = new BufferedReader(fr); //Creation of File Reader object
- String s;
- while ((s = br.readLine()) != null) //Reading Content from the file line by line
- {
- linecount++; //For each line increment linecount by one
- }
- fr.close();
- System.out.println("Number of lines in the Files:" + linecount);
- }
- public static void printArray(String[] scents, int[] scentBoxes) {
- for (int i = 0; i < scentBoxes.length; i++) {
- System.out.println("Box no." + i + " is " + scents[scentBoxes[i]]);
- }
- }
- public static int askForOptions(String option1, String option2) {
- int choice = 0;
- String[] options = new String[]{option1, option2};
- choice = JOptionPane.showOptionDialog(null, option1 + " or " + option2 + "?", "Choose an option",
- JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
- return choice;
- }
- public static int askForOptionsTitle(String option1, String option2, int boxNumber) {
- int choice = 0;
- String[] options = new String[]{option1, option2};
- choice = JOptionPane.showOptionDialog(null, option1 + " or " + option2 + "?", "Choose box no." + boxNumber,
- JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
- return choice;
- }
- public static int askForOptions3(String option1, String option2, String option3) {
- int choice = 0;
- String[] options = new String[]{option1, option2, option3};
- choice = JOptionPane.showOptionDialog(null, option1 + " or " + option2 + "?", "Choose an option",
- JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
- return choice;
- }
- public static String askForString(String info) {
- boolean ok = false;
- String i = "";
- while (!ok) {
- try {
- i = (JOptionPane.showInputDialog(info));
- ok = true;
- return i;
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Input invalid, please try again.");
- }
- }
- return i;
- }
- public static int[] populateBoxesAskUser(int[] scentBoxes, String box1, String box2) {
- int currentChoice = 0;
- scents[0] = "none";
- scents[1] = box1;
- scents[2] = box2;
- for (int i = 0; i < scentBoxes.length; i++) {
- currentChoice = askForOptionsTitle(box1, box2, i);
- if (currentChoice == 0)//compensate for the fact that scent 0 is null
- {
- currentChoice = 1;
- } else if (currentChoice == 1) {
- currentChoice = 2;
- }
- scentBoxes[i] = currentChoice;
- }
- return scentBoxes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement