Guest User

Untitled

a guest
Jul 11th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.75 KB | None | 0 0
  1. Scenario
  2. You are a Junior Developer employed by Turtle Shell Software Solutions who has recently been approached by a local printer firm to, to help them with booking and pricing.
  3. The local printer company have asked for a calculator to work out the costs per order and size of paper the client might need. They would like you to create a Java Application for booking staff to use.
  4. The table below details the different costs that need to be considered when creating the application.
  5. Initial one off Fee A1 A2 A3 A4 How many copies Req.
  6. Week days £0.40 £1.00 0.65 0.45 0.25
  7. Weekends £1.00 £1.20 0.75 0.55 0.35
  8. Urgent Prints £1.80 £2.00 0.95 0.65 0.45
  9.  
  10. Requirements of the application
  11. The application should be able to:
  12. 1. Allow the user to select the print required and create a subtotal.
  13. 2. Allow the user to set the amount of copies required.
  14. 3. Allow the user to set the date of the print and adjust the subtotal accordingly.
  15. 4. Use a tick box to identify that the date of the booking is an Urgent Print and adjust the cost accordingly.
  16. 5. Allow the user to input additional detail about the order including name and number then save it into a text file.
  17. 6. Allow the admin user to view previous bookings that have been saved.
  18.  
  19. The quality of the application design and code will also be a factor of the grades
  20. Allow the user to set number of copies required.
  21. Calculate the subtotal.
  22. A select a predefined week day
  23.  
  24. Allow the user to set the particular date of the booking and adjust the subtotal accordingly according to paper size
  25. Use a tick box to identify that the date of the booking is Urgent adjust it can adjust the cost accordingly.
  26. Allow the client to save the correct transaction cost relating what size paper order, how many sheets were requested and on what part of the week
  27. Allow the user to input additional detail about the customer including name and number then save it into a text file.
  28. Allow the admin to view previous bookings that have been saved.
  29. ----------------------------------------------------------------------------------------------------------------------------------------
  30. package college.calculator;
  31.  
  32. import javax.swing.JFrame;
  33. import javax.swing.JPanel;
  34. import javax.swing.border.EmptyBorder;
  35.  
  36. import javax.swing.JComboBox;
  37. import javax.swing.JFileChooser;
  38. import javax.swing.JLabel;
  39. import javax.swing.JOptionPane;
  40. import javax.swing.JCheckBox;
  41. import javax.swing.JTextField;
  42. import javax.swing.JPasswordField;
  43. import javax.swing.JRadioButton;
  44. import javax.swing.JButton;
  45. import java.awt.event.ActionListener;
  46. import java.io.BufferedReader;
  47. import java.io.FileInputStream;
  48. import java.io.FileWriter;
  49. import java.io.IOException;
  50. import java.io.InputStreamReader;
  51. import java.nio.charset.StandardCharsets;
  52. import java.awt.event.ActionEvent;
  53. import java.awt.Font;
  54. import javax.swing.DefaultComboBoxModel;
  55. import javax.swing.SwingConstants;
  56.  
  57. public class Gui extends JFrame {
  58.  
  59. private JPanel contentPane;
  60. private JTextField txtLogin;
  61. private JPasswordField passwordField;
  62. private JTextField textFieldQntyA1;
  63. private JTextField textField_name;
  64. private JTextField textField_number;
  65. private JTextField textField_subtotal;
  66. private JTextField textField_initialfee;
  67. private double sumSubtotal = 0;
  68. private double initialFee = 0;
  69. private int q1 = 0, q2 = 0, q3 = 0, q4 = 0; //quantity = 0;
  70. private JTextField textFieldQntyA2;
  71. private JTextField textFieldQntyA3;
  72. private JTextField textFieldQntyA4;
  73. private JTextField textField_total;
  74.  
  75.  
  76.  
  77. /**
  78. * Launch the application.
  79. */
  80. /**
  81. * Create the frame.
  82. */
  83. public Gui() {
  84. setTitle("Calculator");
  85.  
  86.  
  87. JLabel lblPriceA1 = new JLabel("");
  88. lblPriceA1.setBounds(168, 107, 46, 14);
  89.  
  90. JLabel lblPriceA2 = new JLabel("");
  91. lblPriceA2.setBounds(168, 137, 46, 14);
  92.  
  93. JLabel lblPriceA3 = new JLabel("");
  94. lblPriceA3.setBounds(168, 166, 46, 14);
  95.  
  96. JLabel lblPriceA4 = new JLabel("");
  97. lblPriceA4.setBounds(168, 192, 46, 14);
  98.  
  99. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  100. setBounds(100, 100, 399, 484);
  101. contentPane = new JPanel();
  102. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  103. setContentPane(contentPane);
  104. contentPane.setLayout(null);
  105.  
  106.  
  107. JComboBox cmboSelectDate = new JComboBox(); // create ComboBox
  108. cmboSelectDate.setEnabled(false);
  109. cmboSelectDate.setModel(new DefaultComboBoxModel(new String[] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}));
  110. cmboSelectDate.setBounds(49, 56, 78, 21);
  111. contentPane.add(cmboSelectDate);
  112.  
  113. JLabel lblSelectPrintDate = new JLabel("Select Print Date OR ");
  114. lblSelectPrintDate.setBounds(137, 59, 126, 14);
  115. contentPane.add(lblSelectPrintDate);
  116.  
  117. JCheckBox chckbxUrgentPrints = new JCheckBox("Urgent Print");
  118. chckbxUrgentPrints.setEnabled(false);
  119. chckbxUrgentPrints.setBounds(256, 55, 97, 23);
  120. contentPane.add(chckbxUrgentPrints);
  121.  
  122. JLabel lblUsername = new JLabel("Username");
  123. lblUsername.setBounds(10, 15, 78, 14);
  124. contentPane.add(lblUsername);
  125.  
  126. JLabel lblPassword = new JLabel("Password");
  127. lblPassword.setBounds(161, 15, 68, 14);
  128. contentPane.add(lblPassword);
  129.  
  130. txtLogin = new JTextField();
  131. txtLogin.setBounds(71, 12, 80, 20);
  132. contentPane.add(txtLogin);
  133. txtLogin.setColumns(10);
  134.  
  135. passwordField = new JPasswordField();
  136. passwordField.setToolTipText("");
  137. passwordField.setBounds(221, 12, 80, 20);
  138. contentPane.add(passwordField);
  139.  
  140.  
  141. JRadioButton rdbtnA1 = new JRadioButton("A1");
  142. rdbtnA1.setEnabled(false);
  143. rdbtnA1.addActionListener(new ActionListener() {
  144. public void actionPerformed(ActionEvent e) {
  145. if (rdbtnA1.isSelected()) { //setEnabled true if rdbtnA1 selected
  146. textFieldQntyA1.setEnabled(true);
  147. q1=Integer.parseInt(textFieldQntyA1.getText()); // get quantity from textFieldQntyA1
  148. }else if (!rdbtnA1.isSelected()){ //if rdbtnA1 not selected - textFieldQntyA1 hidden and set textFieldQntyA1 "0"
  149. textFieldQntyA1.setEnabled(false);
  150. textFieldQntyA1.setText("0");
  151. }
  152. }
  153. });
  154. rdbtnA1.setBounds(49, 107, 86, 23);
  155. contentPane.add(rdbtnA1);
  156.  
  157. JRadioButton rdbtnA2 = new JRadioButton("A2");
  158. rdbtnA2.setEnabled(false);
  159. rdbtnA2.addActionListener(new ActionListener() { //see rdbtnA1.addActionListener(new ActionListener())
  160. public void actionPerformed(ActionEvent e) {
  161. if (rdbtnA2.isSelected()) {
  162. textFieldQntyA2.setEnabled(true);
  163. q2=Integer.parseInt(textFieldQntyA2.getText());
  164. }else if (!rdbtnA2.isSelected()){
  165. textFieldQntyA2.setEnabled(false);
  166. textFieldQntyA2.setText("0");
  167. }
  168. }
  169. });
  170. rdbtnA2.setBounds(49, 133, 86, 23);
  171. contentPane.add(rdbtnA2);
  172.  
  173. JRadioButton rdbtnA3 = new JRadioButton("A3");
  174. rdbtnA3.setEnabled(false);
  175. rdbtnA3.addActionListener(new ActionListener() { //see rdbtnA1.addActionListener(new ActionListener())
  176. public void actionPerformed(ActionEvent e) {
  177. if (rdbtnA3.isSelected()) {
  178. textFieldQntyA3.setEnabled(true);
  179. q3=Integer.parseInt(textFieldQntyA3.getText());
  180. }else if (!rdbtnA3.isSelected()){
  181. textFieldQntyA3.setEnabled(false);
  182. textFieldQntyA3.setText("0");
  183. }
  184. }
  185. });
  186. rdbtnA3.setBounds(49, 162, 86, 23);
  187. contentPane.add(rdbtnA3);
  188.  
  189. JRadioButton rdbtnA4 = new JRadioButton("A4");
  190. rdbtnA4.setEnabled(false);
  191. rdbtnA4.addActionListener(new ActionListener() {//see rdbtnA1.addActionListener(new ActionListener())
  192. public void actionPerformed(ActionEvent e) {
  193. if (rdbtnA4.isSelected()) {
  194. textFieldQntyA4.setEnabled(true);
  195. q4=Integer.parseInt(textFieldQntyA4.getText());
  196. }
  197. else if (!rdbtnA4.isSelected()){
  198. textFieldQntyA4.setEnabled(false);
  199. textFieldQntyA4.setText("0");
  200. }
  201. }
  202. });
  203. rdbtnA4.setBounds(49, 188, 86, 23);
  204. contentPane.add(rdbtnA4);
  205.  
  206. textFieldQntyA1 = new JTextField();
  207. textFieldQntyA1.setHorizontalAlignment(SwingConstants.CENTER);
  208. textFieldQntyA1.setEnabled(false);
  209. textFieldQntyA1.setText("0");
  210. textFieldQntyA1.setBounds(266, 108, 30, 20);
  211. contentPane.add(textFieldQntyA1);
  212. textFieldQntyA1.setColumns(10);
  213.  
  214. JLabel lblQuantity = new JLabel("Quantity");
  215. lblQuantity.setFont(new Font("Tahoma", Font.BOLD, 12));
  216. lblQuantity.setBounds(252, 88, 57, 14);
  217. contentPane.add(lblQuantity);
  218.  
  219. textField_name = new JTextField();
  220. textField_name.setEnabled(false);
  221. textField_name.setBounds(93, 354, 86, 20);
  222. contentPane.add(textField_name);
  223. textField_name.setColumns(10);
  224.  
  225. textField_number = new JTextField();
  226. textField_number.setEnabled(false);
  227. textField_number.setBounds(255, 354, 86, 20);
  228. contentPane.add(textField_number);
  229. textField_number.setColumns(10);
  230.  
  231. JLabel lblName = new JLabel("Name");
  232. lblName.setBounds(37, 357, 46, 14);
  233. contentPane.add(lblName);
  234.  
  235. JLabel lblNumber = new JLabel("Number");
  236. lblNumber.setBounds(199, 357, 46, 14);
  237. contentPane.add(lblNumber);
  238.  
  239. JButton btnSubtotal = new JButton("Calculate");
  240. btnSubtotal.setEnabled(false);
  241. btnSubtotal.addActionListener(new ActionListener() {
  242. public void actionPerformed(ActionEvent e) {
  243. sumSubtotal = 0;
  244. initialFee = 0;
  245. double p1 = 0, p2 = 0, p3 = 0, p4 = 0;
  246. textField_subtotal.setText("");
  247. textField_initialfee.setText("");
  248. textField_total.setText("");
  249. //if one of paper sizes enabled - check combobox or chckbxUrgentPrints and set price p1, p2, p3, p4
  250. if (textFieldQntyA1.isEnabled()||textFieldQntyA2.isEnabled()||textFieldQntyA3.isEnabled()||textFieldQntyA4.isEnabled()) {
  251. if (chckbxUrgentPrints.isSelected()) { // urgent prints
  252. initialFee = 1.80; //set initial fee for urgent prints
  253. p1 = 2.00; p2 = 0.95; p3 = 0.65; p4 = 0.45;
  254. }else if (cmboSelectDate.getSelectedIndex() == 5 || cmboSelectDate.getSelectedIndex() == 6) { //Saturday or Sunday
  255. initialFee = 1.00; //set initial fee on the Saturday or Sunday
  256. p1 = 1.20; p2 = 0.75; p3 = 0.55; p4 = 0.35; // set price on the Saturday or Sunday
  257. }else { //other days
  258. initialFee = 0.40; ////set initial fee on other days
  259. p1 = 1.00; p2 = 0.65; p3 = 0.45; p4 = 0.25; //set price on other days
  260. }
  261.  
  262. q1=Integer.parseInt(textFieldQntyA1.getText()); //get quantity for A1
  263. q2=Integer.parseInt(textFieldQntyA2.getText()); //get quantity for A2
  264. q3=Integer.parseInt(textFieldQntyA3.getText()); //get quantity for A3
  265. q4=Integer.parseInt(textFieldQntyA4.getText()); //get quantity for A4
  266.  
  267. if (q1 < 0 || q2 < 0 || q3 < 0 || q4 < 0) { //if one of the quantities < 0 - show message "Quantity mast be >= 0!"
  268. JOptionPane.showMessageDialog(null, "Quantity mast be >= 0!");
  269. }else {
  270. sumSubtotal += q1*p1 + q2*p2 + q3*p3 + q4*p4; //else calculate subtotal
  271. }
  272. }
  273.  
  274. lblPriceA1.setText("ВЈ " + String.format("%.2f", p1)); //set A1 price to the label1 (example ВЈ0.25)
  275. lblPriceA2.setText("ВЈ " + String.format("%.2f", p2)); //set A2 price
  276. lblPriceA3.setText("ВЈ " + String.format("%.2f", p3)); //set A3 price
  277. lblPriceA4.setText("ВЈ " + String.format("%.2f", p4)); //set A4 price
  278.  
  279. textField_subtotal.setText(String.format("%.2f", sumSubtotal)); //set subtotal
  280. textField_initialfee.setText(String.format("%.2f",initialFee)); // set initial fee
  281. textField_total.setText(String.format("%.2f", sumSubtotal+initialFee)); // set subtotal + initial fee
  282. }
  283. });
  284. btnSubtotal.setBounds(156, 300, 89, 23);
  285. contentPane.add(btnSubtotal);
  286.  
  287. JButton btnSave = new JButton("SAVE");
  288. btnSave.setEnabled(false);
  289. btnSave.addActionListener(new ActionListener() {
  290. public void actionPerformed(ActionEvent arg0) { //write to the file orders.txt
  291. try (FileWriter writer = new FileWriter("src//college//calculator//orders.txt", false)){
  292. writer.write("Name "+ "Subtotal "+"Initail Fee"+" Total "+"Number " + "\r\n");
  293. writer.write(textField_name.getText()+" " + textField_subtotal.getText()+" "
  294. + textField_initialfee.getText()+" " +textField_total.getText()+" "+textField_number.getText());
  295. writer.close();
  296. } catch (IOException e) {
  297. e.printStackTrace();
  298. }
  299. }
  300. });
  301. btnSave.setBounds(151, 408, 89, 23);
  302. contentPane.add(btnSave);
  303.  
  304. JButton btnOpen = new JButton("OPEN");
  305. btnOpen.addActionListener(new ActionListener() {
  306. public void actionPerformed(ActionEvent arg0) { // open orders.txt as notepad
  307. JFileChooser chooser = new JFileChooser();
  308. int returnVal = chooser.showOpenDialog(null);
  309. if(returnVal == JFileChooser.APPROVE_OPTION) {
  310. Runtime runtime = Runtime.getRuntime();
  311. try {
  312. runtime.exec("notepad "+ chooser.getSelectedFile().getPath());
  313. } catch (IOException e) {
  314. e.printStackTrace();
  315. }
  316. }
  317. }
  318. });
  319. btnOpen.setEnabled(false);
  320. btnOpen.setBounds(30, 408, 91, 23);
  321. contentPane.add(btnOpen);
  322.  
  323. textField_subtotal = new JTextField();
  324. textField_subtotal.setHorizontalAlignment(SwingConstants.CENTER);
  325. textField_subtotal.setEditable(false);
  326. textField_subtotal.setBounds(49, 252, 50, 20);
  327. contentPane.add(textField_subtotal);
  328. textField_subtotal.setColumns(10);
  329.  
  330. textField_initialfee = new JTextField();
  331. textField_initialfee.setHorizontalAlignment(SwingConstants.CENTER);
  332. textField_initialfee.setEditable(false);
  333. textField_initialfee.setBounds(159, 252, 50, 20);
  334. contentPane.add(textField_initialfee);
  335. textField_initialfee.setColumns(10);
  336.  
  337. JButton btnExit = new JButton("EXIT");
  338. btnExit.setEnabled(false);
  339. btnExit.addActionListener(new ActionListener() {
  340. public void actionPerformed(ActionEvent arg0) {
  341. System.exit(0);
  342. }
  343. });
  344. btnExit.setBounds(270, 408, 91, 23);
  345. contentPane.add(btnExit);
  346.  
  347. JButton btnNewButton = new JButton("login");
  348. btnNewButton.addActionListener(new ActionListener() { // check user login and password in the file users.txt
  349. public void actionPerformed(ActionEvent arg0){
  350. String userName = txtLogin.getText();
  351. String userPassword = passwordField.getText();
  352. try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("src//college//calculator//users.txt")))){
  353. String line;
  354. boolean flag = false;
  355. while ((line = reader.readLine()) != null) {
  356. if((userName + " " + userPassword).equals(line)) {
  357. cmboSelectDate.setEnabled(true);
  358. chckbxUrgentPrints.setEnabled(true);
  359. btnSubtotal.setEnabled(true);
  360. btnOpen.setEnabled(true);
  361. btnSave.setEnabled(true);
  362. btnExit.setEnabled(true);
  363. rdbtnA1.setEnabled(true);
  364. rdbtnA2.setEnabled(true);
  365. rdbtnA3.setEnabled(true);
  366. rdbtnA4.setEnabled(true);
  367. textField_name.setEnabled(true);
  368. textField_number.setEnabled(true);
  369. JOptionPane.showMessageDialog(null, "Welcome, " + txtLogin.getText() + "!");
  370. flag = true;
  371. }
  372. }
  373. if (flag == false) { // if user not found in the file users.txt - "Enter correct Username and Password!"
  374. JOptionPane.showMessageDialog(null, "Enter correct Username and Password!");
  375. }
  376.  
  377. reader.close();
  378. } catch (IOException e) {
  379. e.printStackTrace();
  380. }
  381. }
  382. });
  383. btnNewButton.setBounds(313, 11, 68, 23);
  384. contentPane.add(btnNewButton);
  385.  
  386. JLabel label = new JLabel("+");
  387. label.setFont(new Font("Tahoma", Font.PLAIN, 14));
  388. label.setBounds(122, 250, 10, 21);
  389. contentPane.add(label);
  390.  
  391. JLabel lblPaperSize = new JLabel("Paper Size");
  392. lblPaperSize.setFont(new Font("Tahoma", Font.BOLD, 12));
  393. lblPaperSize.setBounds(49, 88, 68, 14);
  394. contentPane.add(lblPaperSize);
  395.  
  396. textFieldQntyA2 = new JTextField();
  397. textFieldQntyA2.setHorizontalAlignment(SwingConstants.CENTER);
  398. textFieldQntyA2.setEnabled(false);
  399. textFieldQntyA2.setText("0");
  400. textFieldQntyA2.setColumns(10);
  401. textFieldQntyA2.setBounds(266, 134, 30, 20);
  402. contentPane.add(textFieldQntyA2);
  403.  
  404. textFieldQntyA3 = new JTextField();
  405. textFieldQntyA3.setHorizontalAlignment(SwingConstants.CENTER);
  406. textFieldQntyA3.setEnabled(false);
  407. textFieldQntyA3.setText("0");
  408. textFieldQntyA3.setColumns(10);
  409. textFieldQntyA3.setBounds(266, 163, 30, 20);
  410. contentPane.add(textFieldQntyA3);
  411.  
  412. textFieldQntyA4 = new JTextField();
  413. textFieldQntyA4.setHorizontalAlignment(SwingConstants.CENTER);
  414. textFieldQntyA4.setEnabled(false);
  415. textFieldQntyA4.setText("0");
  416. textFieldQntyA4.setColumns(10);
  417. textFieldQntyA4.setBounds(266, 189, 30, 20);
  418. contentPane.add(textFieldQntyA4);
  419.  
  420. JLabel label_1 = new JLabel("= \u00A3");
  421. label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
  422. label_1.setBounds(238, 250, 63, 21);
  423. contentPane.add(label_1);
  424.  
  425. JLabel lblSubtotal = new JLabel("Subtotal");
  426. lblSubtotal.setBounds(49, 227, 69, 14);
  427. contentPane.add(lblSubtotal);
  428.  
  429. JLabel lblInitailFee = new JLabel("Initail fee");
  430. lblInitailFee.setBounds(161, 227, 58, 14);
  431. contentPane.add(lblInitailFee);
  432.  
  433. JLabel lblTotal = new JLabel("Total");
  434. lblTotal.setBounds(303, 227, 50, 14);
  435. contentPane.add(lblTotal);
  436.  
  437. textField_total = new JTextField();
  438. textField_total.setHorizontalAlignment(SwingConstants.CENTER);
  439. textField_total.setEditable(false);
  440. textField_total.setBounds(295, 252, 50, 20);
  441. contentPane.add(textField_total);
  442. textField_total.setColumns(10);
  443.  
  444. JLabel lblPrice = new JLabel("Price");
  445. lblPrice.setFont(new Font("Tahoma", Font.BOLD, 12));
  446. lblPrice.setBounds(168, 88, 46, 14);
  447. contentPane.add(lblPrice);
  448. contentPane.add(lblPriceA1);
  449. contentPane.add(lblPriceA2);
  450. contentPane.add(lblPriceA3);
  451. contentPane.add(lblPriceA4);
  452. }
  453. }
  454.  
  455. package college.calculator;
  456.  
  457. import java.awt.EventQueue;
  458.  
  459. public class Main {
  460. public static void main(String[] args) {
  461. EventQueue.invokeLater(new Runnable() {
  462. public void run() {
  463. try {
  464. Gui frame = new Gui();
  465. frame.setVisible(true);
  466. } catch (Exception e) {
  467. e.printStackTrace();
  468. }
  469. }
  470. });
  471. }
  472. }
Add Comment
Please, Sign In to add comment