Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. package ta;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.LinkedList;
  5.  
  6. import javax.swing.AbstractListModel;
  7.  
  8. public class MyListModel extends AbstractListModel<String>{
  9.  
  10. private static final long serialVersionUID = 1L;
  11. private LinkedList<BigDecimal> listBets;
  12.  
  13. public LinkedList<BigDecimal> getListBets() {
  14. return listBets;
  15. }
  16.  
  17. public void setListBets(LinkedList<BigDecimal> listBets) {
  18. this.listBets = listBets;
  19. }
  20.  
  21. public MyListModel(LinkedList<BigDecimal> list) {
  22. setList(list);
  23. }
  24.  
  25. public void add(BigDecimal value){
  26. getList().add(value);
  27. refresh();
  28. }
  29.  
  30. public String getElementAt(int index) {
  31. return String.format("%.8f", getList().get(index));
  32. }
  33.  
  34. public int getSize() {
  35. return getList().size();
  36. }
  37.  
  38. public LinkedList<BigDecimal> getList(){
  39. return listBets;
  40. }
  41.  
  42. public void setList(LinkedList<BigDecimal> list){
  43. this.listBets = list;
  44. refresh();
  45. }
  46.  
  47. public void clear() {
  48. getList().clear();
  49. refresh();
  50. }
  51.  
  52. public void removeSide() {
  53. getList().removeLast();
  54. getList().removeFirst();
  55. refresh();
  56. }
  57.  
  58. void removeLast(){
  59. getList().removeLast();
  60. refresh();
  61. }
  62.  
  63. public void refresh() {
  64. fireContentsChanged(this, 0, getSize());
  65. }
  66. }
  67.  
  68. package ta;
  69.  
  70. import java.awt.EventQueue;
  71.  
  72. import javax.swing.JFrame;
  73. import javax.swing.JPanel;
  74. import java.awt.BorderLayout;
  75. import java.awt.GridLayout;
  76. import java.util.LinkedList;
  77. import java.util.NoSuchElementException;
  78. import java.util.Random;
  79. import java.util.Scanner;
  80.  
  81. import javax.swing.JTextArea;
  82.  
  83. import javax.swing.JScrollPane;
  84. import javax.swing.JList;
  85. import javax.swing.JOptionPane;
  86. import javax.swing.JButton;
  87. import java.awt.event.ActionListener;
  88. import java.math.BigDecimal;
  89. import java.text.ParseException;
  90. import java.awt.event.ActionEvent;
  91.  
  92. public class TA {
  93.  
  94. private JFrame frame;
  95. private MyListModel listModelBets;
  96. private JList<String> list;
  97. private JTextArea textArea;
  98. private final String defaultBets = "0.00000001n0.00000001n0.00000001n0.00000001n0.00000001";
  99. private final boolean win = true;
  100. private final boolean lose = false;
  101.  
  102. /**
  103. * Launch the application.
  104. */
  105. public static void main(String[] args) {
  106. EventQueue.invokeLater(new Runnable() {
  107. public void run() {
  108. try {
  109. TA window = new TA();
  110. window.frame.setVisible(true);
  111. } catch (Exception e) {
  112. e.printStackTrace();
  113. }
  114. }
  115. });
  116. }
  117.  
  118. /**
  119. * Create the application.
  120. */
  121. public TA() {
  122. listModelBets = new MyListModel(new LinkedList<>());
  123. initialize();
  124. }
  125.  
  126. /**
  127. * Initialize the contents of the frame.
  128. */
  129. private void initialize() {
  130. frame = new JFrame();
  131. frame.setBounds(100, 100, 450, 300);
  132. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  133. frame.getContentPane().setLayout(new BorderLayout(0, 0));
  134.  
  135. JPanel panel_2 = new JPanel();
  136. frame.getContentPane().add(panel_2);
  137. panel_2.setLayout(new GridLayout(0, 2, 0, 0));
  138.  
  139. JPanel panel_1 = new JPanel();
  140. panel_2.add(panel_1);
  141. panel_1.setLayout(new BorderLayout(0, 0));
  142.  
  143. JScrollPane scrollPane = new JScrollPane();
  144. panel_1.add(scrollPane);
  145.  
  146. textArea = new JTextArea(defaultBets);
  147. scrollPane.setViewportView(textArea);
  148.  
  149. JPanel panel = new JPanel();
  150. panel_2.add(panel);
  151. panel.setLayout(new BorderLayout(0, 0));
  152.  
  153. JScrollPane scrollPane_1 = new JScrollPane();
  154. panel.add(scrollPane_1);
  155.  
  156. list = new JList<String>(listModelBets);
  157. scrollPane_1.setViewportView(list);
  158.  
  159. JPanel panel_3 = new JPanel();
  160. frame.getContentPane().add(panel_3, BorderLayout.SOUTH);
  161.  
  162. JButton btnNewButton_1 = new JButton("Clear");
  163. btnNewButton_1.addActionListener(new ActionListener() {
  164. public void actionPerformed(ActionEvent arg0) {
  165. listModelBets.clear();
  166. }
  167. });
  168. panel_3.add(btnNewButton_1);
  169.  
  170. JButton btnPlay = new JButton("Play");
  171. btnPlay.addActionListener(new ActionListener() {
  172. public void actionPerformed(ActionEvent arg0) {
  173. Thread th = new Thread(new Runnable() {
  174.  
  175. @Override
  176. public void run() {
  177. for (int i = 0; i < 1000; i++) {
  178. boolean result = play();
  179. if (result) {
  180. if (listModelBets.getSize() == 1) {
  181. listModelBets.removeLast();
  182. } else if (listModelBets.getSize() > 1) {
  183. listModelBets.removeSide();
  184. }
  185. } else {
  186. listModelBets.add(getBet());
  187. }
  188. }
  189. }
  190. });
  191. th.start();
  192. }
  193. });
  194. panel_3.add(btnPlay);
  195. }
  196.  
  197. void addBets() {
  198. Scanner sc = new Scanner(textArea.getText());
  199. sc.useDelimiter("\n");
  200. while (sc.hasNext()) {
  201. String s = sc.next();
  202. if (s != null && !s.equals("")) {
  203. try {
  204. BigDecimal b = new BigDecimal(s);
  205. listModelBets.add(b);
  206. } catch (NumberFormatException ex) {
  207. // JOptionPane.showMessageDialog(frame, "Enter number
  208. // value.nSample 0.00000001");
  209. }
  210.  
  211. }
  212. }
  213. sc.close();
  214. }
  215.  
  216. boolean play() {
  217. Random rnd = new Random();
  218. return rnd.nextBoolean();
  219. }
  220.  
  221. BigDecimal getBet() {
  222. BigDecimal sum = null;
  223. if (listModelBets.getSize() > 0) {
  224. sum = calcSum();
  225. } else {
  226. addBets();
  227. sum = calcSum();
  228. }
  229. return sum;
  230. }
  231.  
  232. BigDecimal calcSum() {
  233. BigDecimal sum = null;
  234. try {
  235. BigDecimal first = listModelBets.getList().getFirst();
  236. BigDecimal last = listModelBets.getList().getLast();
  237. sum = first.add(last);
  238. } catch (NoSuchElementException ex) {
  239. // stopGame();
  240. JOptionPane.showMessageDialog(frame, "Enter number valuenin editor bets.nSample 0.00000001");
  241. }
  242. return sum;
  243. }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement