Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.34 KB | None | 0 0
  1. package ib2;
  2.  
  3. import java.awt.Scrollbar;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.MouseListener;
  6. import java.io.BufferedReader;
  7. import java.io.BufferedWriter;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileOutputStream;
  12. import java.io.FileReader;
  13. import java.io.FileWriter;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.OutputStream;
  17. import java.io.PrintWriter;
  18. import java.util.Scanner;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21. import java.awt.event.KeyEvent;
  22. import java.awt.event.MouseEvent;
  23. import java.awt.event.MouseListener;
  24.  
  25. import javax.swing.JButton;
  26. import javax.swing.JFrame;
  27. import javax.swing.JLabel;
  28. import javax.swing.JLayeredPane;
  29. import javax.swing.JMenu;
  30. import javax.swing.JMenuItem;
  31. import javax.swing.JPanel;
  32. import javax.swing.JTextField;
  33.  
  34. public class Gui extends JFrame implements ActionListener, MouseListener {
  35. private JMenu menu, submenu;
  36. private JMenuItem Decks, Options;
  37. private static String loc = "C:\\Users\\huber\\OneDrive\\Dokumenty\\Folder szkolny\\Forms\\Program Data";
  38. private JLayeredPane screen;
  39. private static Boolean b = true;
  40.  
  41. void Menubar() {
  42. }
  43.  
  44. static String getLoc() {
  45. return loc;
  46. }
  47.  
  48. static void setB(boolean b) {
  49.  
  50. }
  51.  
  52. static boolean getB() {
  53. return b;
  54. }
  55.  
  56. static char[] setLocal(String a) {
  57. char[] ch = new char[a.length()];
  58. for (int i = 0; i < a.length(); i++) {
  59. ch[i] = a.charAt(i);
  60. if (ch[i] == '\'' && i == a.length() - 1) {
  61. ch[i + 1] = '\'';
  62. i++;
  63.  
  64. }
  65.  
  66. }
  67. return ch;
  68. }
  69.  
  70. static String setLoc(String loc) {
  71. loc = Gui.loc;
  72. return loc;
  73. }
  74.  
  75. public static void main(String[] args) throws IOException {
  76.  
  77. System.out.println("Current location of files: " + getLoc());
  78. System.out.println("Do you want to change it?\n" + "\t1. Yes\n" + "\t2. No");
  79. Scanner c = new Scanner(System.in);
  80. if (c.nextInt() == 1) {
  81. System.out.println(
  82. "\nInput a new location (Warning: you will have to manually transport all files from current location to a new location.");
  83. Scanner s = new Scanner(System.in);
  84. char[] ch = setLocal(s.nextLine());
  85. setLoc(new String(ch));
  86. if (getLoc().equals(new String(ch)))
  87. System.out.println("Your new location for the program's files is " + getLoc());
  88. File g = new File(getLoc() + "\\directory.txt");
  89. g.createNewFile();
  90. FileWriter mn = new FileWriter(g);
  91. BufferedWriter pl = new BufferedWriter(mn);
  92. PrintWriter writer = new PrintWriter(g);
  93. writer.print("");
  94. writer.close();
  95. pl.write(getLoc());
  96. pl.close();
  97. c.close();
  98. s.close();
  99. }
  100.  
  101. File gp = new File(getLoc() + "\\directory.txt");
  102. gp.createNewFile();
  103. FileWriter mnp = new FileWriter(gp);
  104. BufferedWriter plp = new BufferedWriter(mnp);
  105. plp.write(getLoc());
  106. setLoc(gp.toString());
  107. plp.close();
  108.  
  109. System.out.println("Welcome to Flashcard Program vBeta\n" + "What you want to do?\n"
  110. + "\t1. Display a list of decks\n" + "\t2. Create a new deck\n"
  111. // + "\t3. Set destination folder for flashcards (recommended for the first
  112. // time)\n"
  113. + "\t4. What you want to be shown first in traditional view of flashbacks?\n"
  114. + "\t5. Import deck from a deck");
  115.  
  116. Scanner scan = new Scanner(System.in);
  117. int sc = scan.nextInt();
  118.  
  119. if (sc == 1) {
  120. System.out.println("Choose a deck");
  121. File f = new File(getLoc() + "\\flashcardList.txt");
  122. f.createNewFile();
  123. BufferedReader br1 = new BufferedReader(new FileReader(f));
  124. int liczbalinijek = 0;
  125. while (br1.readLine() != null) {
  126. liczbalinijek++;
  127. }
  128. br1.close();
  129. BufferedReader br2 = new BufferedReader(new FileReader(f));
  130. String[] s = new String[liczbalinijek];
  131. if (liczbalinijek == 0) {
  132. System.out.println("There are no decks currentely created.");
  133. }
  134. for (int i = 0; i < liczbalinijek; i++) {
  135. s[i] = br2.readLine();
  136. System.out.println(s[i]);
  137. }
  138. br2.close();
  139. Scanner dn = new Scanner(System.in);
  140. String dnt = dn.nextLine();
  141. for (int i = 0; i < liczbalinijek; i++) {
  142. if (s[i].equals(dnt)) {
  143. File d = new File(getLoc() + "\\" + s[i] + ".txt");
  144. BufferedReader subbf1 = new BufferedReader(new FileReader(d));
  145. int ll = 0;
  146. BufferedReader subbf2 = new BufferedReader(new FileReader(d));
  147. while (subbf1.readLine() != null) {
  148. ll++;
  149.  
  150. System.out.println(subbf2.readLine() + "\n");
  151. }
  152. subbf1.close();
  153. subbf2.close();
  154.  
  155. System.out.println("Options:" + "\n1. Switch to the traditional view."
  156. + "\n2. Launch a matching app" + "\n3. Add a new flashcard");
  157.  
  158. Scanner flashcard = new Scanner(System.in);
  159.  
  160. Flashcard[] fclist = new Flashcard[ll];
  161. Deck deck = new Deck(dnt, fclist);
  162. deck.setFclist(d);
  163. if (flashcard.nextInt() == 1) {
  164. System.out.println("Which flashcard you'd want to see? (Write it's number)");
  165. Scanner sy = new Scanner(System.in);
  166. int y = sy.nextInt() - 1;
  167.  
  168. if (getB()) {
  169. System.out.println(deck.getFclist()[y].getTerm() + "\n");
  170. } else {
  171. System.out.println(deck.getFclist()[y].getDefinition() + "\n");
  172. }
  173. System.out.println("\t1. Show the other side of the flashcard");
  174.  
  175. Scanner trview = new Scanner(System.in);
  176. if (trview.nextInt() == 1)
  177. if (getB()) {
  178. System.out.println("\n" + deck.getFclist()[y].getDefinition() + "\n");
  179. } else {
  180. System.out.println("\n" + deck.getFclist()[y].getTerm() + "\n");
  181. }
  182. }
  183. if (flashcard.nextInt() == 2) {
  184. String[] a = new String[2 * ll];
  185. // int[] bt = new int[ll];
  186. // int[] bd = new int[ll];
  187. // for (int l = 0; l < ll; l++) {
  188. // bt[l] = ll + 1;
  189. // bd[l] = ll + 1;
  190. // }
  191. int checkterm = 0;
  192. for (int t = 0; t < 2 * ll; t++) {
  193.  
  194. int p = (int) Math.random();
  195.  
  196. if (p % 2 == 0) {
  197. if (checkterm != ll) {
  198. a[t] = deck.getFclist()[(int) (Math.random() * (ll))].getTerm();
  199. boolean check1 = true;
  200. int check2 = 0;
  201. for (int g = 0; check2 == 2 * ll; g++) {
  202.  
  203. // if (p == bt[g]) {
  204. // p = (int) Math.random();
  205. // g = 0;
  206. // }
  207. // }
  208. if (a[t].equals(a[g])) {
  209. a[t] = deck.getFclist()[(int) (Math.random() * (ll))].getTerm();
  210. check1 = false;
  211. }
  212. if (check1)
  213. check2++;
  214. if (g == ll - 1) {
  215. g = 0;
  216. }
  217.  
  218. // bt[t] = p;
  219.  
  220. }
  221. System.out.println(a[t]);
  222. checkterm++;
  223. }
  224. }
  225.  
  226. else {
  227. a[t] = deck.getFclist()[(int) (Math.random() * (ll))].getDefinition();
  228. boolean check1 = true;
  229. int check2 = 0;
  230. for (int g = 0; check2 == 2 * ll; g++) {
  231.  
  232. // if (p == bt[g]) {
  233. // p = (int) Math.random();
  234. // g = 0;
  235. // }
  236. // }
  237. if (a[t].equals(a[g])) {
  238. a[t] = deck.getFclist()[(int) (Math.random() * (ll))].getDefinition();
  239. check1 = false;
  240. }
  241. if (check1)
  242. check2++;
  243. if (g == ll - 1) {
  244. g = 0;
  245. }
  246.  
  247. // bt[t] = p;
  248.  
  249. }
  250. System.out.println(a[t]);
  251. }
  252. }
  253. double score = 0;
  254. int check = 0;
  255.  
  256. while (check != 2 * ll) {
  257. System.out.println("Your current score: 0\n"
  258. + "join together term and definition (seperate them by tabulator)");
  259. Scanner v = new Scanner(System.in);
  260. char[] t1 = new char[v.nextLine().length()];
  261. char[] d1 = new char[v.nextLine().length()];
  262. int k = 0;
  263. for (int z = 0; z < v.nextLine().length(); z++) {
  264. t1[z] = v.nextLine().charAt(z);
  265. if (v.nextLine().charAt(z) == '\t' || k == 1) {
  266. d1[z] = v.nextLine().charAt(z);
  267. k = 1;
  268. }
  269. }
  270. String T1 = new String(t1);
  271. String D1 = new String(d1);
  272. int ff = 0;
  273. int fail = 0;
  274. for (int z = 0; z < ll; z++) {
  275. if (T1.equals(fclist[z].getTerm()) && D1.equals(fclist[z].getDefinition())) {
  276. System.out.println("That's the correct answer!");
  277. score++;
  278. check++;
  279. ff = z;
  280. } else {
  281. fail++;
  282. }
  283. if (a[z].equals(fclist[ff].getTerm()) || a[z].equals(fclist[ff].getDefinition())) {
  284. a[z] = "";
  285. }
  286. }
  287. if (fail == ll) {
  288. System.out.println("I'm sorry, but that was a wrong answer.");
  289. score--;
  290. }
  291. }
  292. System.out.println("Type down your name.");
  293. Scanner nameUser = new Scanner(System.in);
  294. UserApp u = new UserApp(nameUser.nextLine(), score);
  295. File rl = new File(getLoc() + "\\rankingList.txt");
  296. if (rl.createNewFile()) {
  297. BufferedWriter bw1 = new BufferedWriter(new FileWriter(rl));
  298. for (int j = 1; j <= 10; j++) {
  299. if (j == 1)
  300. bw1.write(j + "." + "\t" + u.getUserName() + "\t" + u.getScore() + "\n");
  301. else
  302. bw1.write(j + ".\t\t\n");
  303. }
  304.  
  305. } else {
  306. setUserList(rl, u); // make it later
  307. }
  308. System.out.println("Your score is " + score + " points.");
  309. System.out.println("\tRanking:");
  310. BufferedReader br3 = new BufferedReader(new FileReader(rl));
  311. while (br3.readLine() != null)
  312. System.out.println(br3.readLine());
  313. br3.close();
  314. }
  315. if (flashcard.nextInt() == 3) {
  316. FileWriter fw = new FileWriter(d);
  317. BufferedWriter bw = new BufferedWriter(fw);
  318. System.out.println("How many flashcards you'd want to add?");
  319. Scanner s1 = new Scanner(System.in);
  320. for (int z = 0; z < s1.nextInt(); z++) {
  321. System.out.println("Write the term of additional flashcard number " + z + ".");
  322. Scanner t = new Scanner(System.in);
  323. System.out.println("Write the definition of additional flashcard number " + z + ".");
  324. Scanner def = new Scanner(System.in);
  325. bw.write(t.nextLine() + "\t" + def.nextLine() + "\n");
  326. }
  327. bw.close();
  328. }
  329. flashcard.close();
  330. break;
  331. }
  332.  
  333. }
  334. dn.close();
  335. }
  336. if (sc == 2) {
  337. String name;
  338.  
  339. System.out.println("Write the name of the deck");
  340. Scanner s1 = new Scanner(System.in);
  341. name = s1.nextLine();
  342. System.out.println("Write how many flashcards do you want to be in the deck");
  343. Scanner s2 = new Scanner(System.in);
  344. int ll = s2.nextInt();
  345. Flashcard[] fclist = new Flashcard[ll];
  346. File ff = new File(getLoc() + "//" + name + ".txt");
  347. ff.createNewFile();
  348. String term = "";
  349. String definition = "";
  350. FileWriter deckSource = new FileWriter(ff);
  351. BufferedWriter bw = new BufferedWriter(deckSource);
  352. for (int i = 0; i < ll; i++) {
  353. System.out.println("Write the term of the flashcard number " + i + ".");
  354. Scanner t = new Scanner(System.in);
  355. fclist[i] = new Flashcard(term, definition);
  356. if (!t.toString().equals("")) {
  357.  
  358. fclist[i].setTerm(t.nextLine());
  359. }
  360.  
  361. System.out.println("Write the definition of the flashcard number " + i + ".");
  362. Scanner de = new Scanner(System.in);
  363. if (!de.toString().equals(""))
  364. fclist[i].setDefinition(de.nextLine());
  365. bw.write(fclist[i].getTerm() + "\t" + fclist[i].getDefinition() + "\n");
  366. }
  367. bw.close();
  368. File e = new File(getLoc() + "//flashcardList.txt");
  369. e.createNewFile();
  370. FileWriter deckListSource = new FileWriter(e);
  371. BufferedWriter wb = new BufferedWriter(deckListSource);
  372. wb.write(name + "\n");
  373. wb.close();
  374.  
  375. }
  376. // if (sc == 3) {
  377. // System.out.println("Current location of files:" + getLoc()
  378. // + "\nInput a new location (Warning: you will have to manually transport all files from current location to a new location.");
  379. // Scanner s = new Scanner(System.in);
  380. // char[] ch = setLocal(s.nextLine());
  381. // setLoc(new String(ch));
  382. // if (getLoc().equals(new String(ch)))
  383. // System.out.println("Your new location for the program's files is" + getLoc());
  384. //
  385. // }
  386. if (sc == 4)
  387.  
  388. {
  389. Scanner p = new Scanner(System.in);
  390. int pint = p.nextInt();
  391. if (getB()) {
  392. System.out.println(
  393. "Currently, the term is shown first" + "\nDo you want to change it?" + "\n1.Yes" + "\n2.No");
  394. if (pint == 1)
  395. setB(false);
  396. } else {
  397. System.out.println("Currently, the definition is shown first" + "\nDo you want to change it?"
  398. + "\n1.Yes" + "\n2.No");
  399. if (pint == 1)
  400. setB(false);
  401. }
  402. if (pint == 2) {
  403.  
  404. }
  405.  
  406. }
  407. if (sc == 5) {
  408. System.out.println("Write down the path direction of the file");
  409. Scanner scr = new Scanner(System.in);
  410. System.out.println("Write down the of the file");
  411. Scanner name = new Scanner(System.in);
  412. File fff = new File(setLocal(scr.nextLine()) + name.nextLine() + ".txt");
  413. fff.createNewFile();
  414. InputStream is = null;
  415. OutputStream os = null;
  416.  
  417. try {
  418. is = new FileInputStream(new String(setLocal(scr.nextLine())));
  419. os = new FileOutputStream(getLoc());
  420. byte[] buffer = new byte[1024];
  421. int length;
  422. while ((length = is.read(buffer)) > 0) {
  423. os.write(buffer, 0, length);
  424. }
  425. } finally {
  426. is.close();
  427. os.close();
  428. }
  429. File e = new File(getLoc() + "//flashcardList.txt");
  430. FileWriter deckListSource = new FileWriter(e);
  431. BufferedWriter wb = new BufferedWriter(deckListSource);
  432. wb.write(name.nextLine() + "\n");
  433. wb.close();
  434. }
  435. scan.close();
  436.  
  437. }
  438.  
  439. private static void setUserList(File rl, UserApp u) throws IOException {
  440. Scanner lr = new Scanner(rl);
  441. BufferedReader brbr = new BufferedReader(new FileReader(rl));
  442. char[] nch;
  443. char[] sch;
  444. UserApp[] ua = new UserApp[10];
  445. int check = 0;
  446. while (check != 1) {
  447. String s = brbr.readLine();
  448. nch = new char[s.length()];
  449. for (int i = 3; i < s.length(); i++) {
  450. nch[i] = s.charAt(i);
  451.  
  452.  
  453. }
  454. }
  455.  
  456. }
  457.  
  458. static class Deck extends Gui {
  459.  
  460. private String name;
  461. private JLabel[] termList, definitionList;
  462. private Scrollbar sFlashcards;
  463. private JLabel termAndDefinition, message;
  464. private JButton addNewFlashcard, switching, cancel, done, Previous, Next, showOtherSide;
  465. private JLayeredPane pane;
  466. private JTextField nameOrTerm;
  467. private int ll = 0;
  468. private Flashcard[] fclist = new Flashcard[ll];
  469.  
  470. Flashcard[] setFclist(File f) throws IOException {
  471. BufferedReader br1 = new BufferedReader(new FileReader(f));
  472. BufferedReader br2 = new BufferedReader(new FileReader(f));
  473. while (br1.readLine() != null)
  474. ll++;
  475. Flashcard[] fclist = new Flashcard[ll];
  476. String s;
  477. for (int z = 0; (s = br2.readLine()) != null; z++) {
  478. fclist[z] = new Flashcard("", "");
  479. char[] ch = new char[s.length()];
  480. char[] ch2 = new char[s.length()];
  481. int index = 0;
  482. for (int i = 0; s.charAt(i) != '\t'; i++) {
  483. ch[i] = s.charAt(i);
  484. index = i + 2;
  485. }
  486. fclist[z].setTerm(new String(ch));
  487. for (int x = index; x < s.length(); x++) {
  488. ch2[x] = s.charAt(x);
  489. }
  490. fclist[z].setDefinition(new String(ch2));
  491. }
  492. br1.close();
  493. br2.close();
  494. this.fclist = fclist;
  495. return fclist;
  496. }
  497.  
  498. public String getName() {
  499. return name;
  500. }
  501.  
  502. Flashcard[] getFclist() {
  503. return fclist;
  504.  
  505. }
  506.  
  507. Deck(String name, Flashcard[] fclist) {
  508. this.name = name;
  509. this.fclist = fclist;
  510. }
  511.  
  512. void deckNamePrompt() {
  513. // generate a prompt for inputting the name upon creating new deck
  514. // flashcardListOverview() flashcardListTraditional() //two methods for
  515. // different presentation of flashcards
  516. }
  517.  
  518. }
  519.  
  520. static class Flashcard extends Gui {
  521.  
  522. private String term, definition;
  523. private JTextField definitionShow;
  524. private JButton addNextFlashcard;
  525.  
  526. Flashcard(String term, String definition) {
  527. setTerm(term);
  528. setDefinition(definition);
  529. }
  530.  
  531. String getTerm() {
  532. return term;
  533.  
  534. }
  535.  
  536. String getDefinition() {
  537. return definition;
  538.  
  539. }
  540.  
  541. void setTerm(String term) {
  542. this.term = term;
  543.  
  544. }
  545.  
  546. void setDefinition(String definition) {
  547. this.definition = definition;
  548.  
  549. }
  550.  
  551. void flashcardPrompt() {
  552. // generating a prompt for inputting a term and definition for a flashcard
  553. }
  554.  
  555. }
  556.  
  557. static class Decklist extends Gui {
  558.  
  559. private Deck[] deckArray;
  560. private JButton[] theDeckList;
  561. private JLabel nameOfDecks;
  562. private Scrollbar sDeckList;
  563.  
  564. void decklistInterface() {
  565.  
  566. }
  567.  
  568. Decklist(Deck[] deckArray) {
  569. this.deckArray = deckArray;
  570. }
  571.  
  572. void setDeckArray(File decklistNameSource) {
  573.  
  574. }
  575.  
  576. Deck[] getDeckArray() {
  577. return deckArray;
  578. }
  579.  
  580. }
  581.  
  582. class Prompts extends Gui {
  583.  
  584. private JButton ok;
  585. private JLabel message;
  586. private JLayeredPane prompt;
  587.  
  588. void promptsWindow(String m) {
  589.  
  590. }
  591. }
  592.  
  593. @Override
  594. public void mouseClicked(MouseEvent e) {
  595. // TODO Auto-generated method stub
  596.  
  597. }
  598.  
  599. @Override
  600. public void mousePressed(MouseEvent e) {
  601. // TODO Auto-generated method stub
  602.  
  603. }
  604.  
  605. @Override
  606. public void mouseReleased(MouseEvent e) {
  607. // TODO Auto-generated method stub
  608.  
  609. }
  610.  
  611. @Override
  612. public void mouseEntered(MouseEvent e) {
  613. // TODO Auto-generated method stub
  614.  
  615. }
  616.  
  617. @Override
  618. public void mouseExited(MouseEvent e) {
  619. // TODO Auto-generated method stub
  620.  
  621. }
  622.  
  623. @Override
  624. public void actionPerformed(ActionEvent e) {
  625. // TODO Auto-generated method stub
  626.  
  627. }
  628.  
  629. }
  630.  
  631. class Ranking extends Gui {
  632.  
  633. private UserApp[] userList;
  634. private JLabel[] rankingList;
  635. private JLabel name, timeFinish, message;
  636. private JLayeredPane window;
  637. private JTextField direction;
  638.  
  639. void rankingInterface() {
  640.  
  641. }
  642.  
  643. void prompt() {
  644.  
  645. }
  646.  
  647. void setUserList(File rl, UserApp u) {
  648.  
  649. }
  650.  
  651. UserApp[] getUserList() {
  652. return userList;
  653. }
  654.  
  655. }
  656.  
  657. class UserApp extends Gui {
  658.  
  659. private String userName;
  660. private double score;
  661.  
  662. UserApp(String userName, double score) {
  663. setUserName(userName);
  664. setScore(score);
  665. }
  666.  
  667. String getUserName() {
  668. return userName;
  669. }
  670.  
  671. double getScore() {
  672. return score;
  673. }
  674.  
  675. void setUserName(String userName) {
  676.  
  677. }
  678.  
  679. void setScore(double score) {
  680. this.score = score;
  681.  
  682. }
  683. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement