Guest User

Untitled

a guest
Oct 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. //Inheritance
  2. MusicVideo item3 = new MusicVideo("Music", 80, "Lil Wayne", "R&B");
  3. item3.show();
  4.  
  5. class MusicVideo extends VideoTape {
  6. String artistN;
  7. String category;
  8.  
  9. public MusicVideo(String ttl, int lngth, String artistN, String category) {
  10. super(ttl, lngth);
  11. this.artistN = artistN;
  12. this.category = category;
  13. }
  14.  
  15. public void show() {
  16. super.show();
  17. System.out.println("artist : " + artistN + " " + "\ncategory : "+ category);
  18. }
  19. }
  20.  
  21. //POLYMORPH
  22. Scanner sc = new Scanner(System.in);
  23. Saloon[] s1 = new Saloon[3];
  24. for (int i = 0; i < s1.length; i++) {
  25. s1[i] = new BodyTreatment(customerNumber, customerName, member, date, treatmentType);
  26. s1[i] = new HairTreatment(customerNumber, customerName, member, date, rebonding, cutting, washing, dyeing);
  27. }
  28. for (int i = 0; i < s1.length; i++) {
  29. s1[i].show();
  30. System.out.println("Treatment Cost \t: RM " + s1[i].computePayment() + "\n========================================\n");
  31. }
  32. sc.close();
  33.  
  34.  
  35. //GUI
  36.  
  37. package ex2;
  38. import javax.swing.*;
  39. import javax.swing.event.ChangeEvent;
  40.  
  41. import java.awt.*;
  42. import java.awt.event.*;
  43. import java.io.*;
  44.  
  45. class MainMethod extends JFrame implements ActionListener{
  46. // Default frame width
  47. private static final int FRAME_WIDTH = 350;
  48. // Default frame height
  49. private static final int FRAME_HEIGHT = 200;
  50. // x coordinate of the frame default original point
  51. private static final int FRAME_X_ORIGIN = 150;
  52. // y coordinate of the frame default original point
  53. private static final int FRAME_Y_ORIGIN = 250;
  54. /**
  55. * The Swing button
  56. */
  57. private JButton btnCalculate;
  58. private JButton btnClear;
  59. /**
  60. * The JTextField for the user to enter the numbers
  61. */
  62. private JTextField txtNum1;
  63. private JTextField txtNum2;
  64. private JTextField txtTotal;
  65.  
  66. //Checkbox
  67. private JCheckBox btnAlmond;
  68. private JCheckBox btnMazola;
  69.  
  70. //Radio button
  71. private JRadioButton delivery;
  72. private JRadioButton noDelivery;
  73.  
  74.  
  75. /**
  76. * The JLabel for prompting the user
  77. */
  78. private JLabel lblNum1;
  79.  
  80.  
  81. private JLabel lblNum2;
  82. private JLabel lblTotal;
  83. //------------------------------------
  84. // Main Method
  85. //------------------------------------
  86. public static void main(String[] args) throws IOException {
  87. MainMethod calculator = new MainMethod();
  88. calculator.setVisible(true);
  89. }
  90. //------------------------------------
  91. // Constructors
  92. //------------------------------------
  93. public MainMethod()
  94. {
  95. Container contentPane;
  96. //set the frame properties
  97. setSize (FRAME_WIDTH, FRAME_HEIGHT);
  98. setResizable(true);
  99. setTitle ("Mini Calculator");
  100. setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
  101. contentPane = getContentPane();
  102. contentPane.setLayout(new GridLayout(6,2));
  103.  
  104. JMenuBar mb = new JMenuBar();
  105. setJMenuBar(mb);
  106. JMenu fileMenu = new JMenu("File", false);
  107. fileMenu.add(new JMenuItem("new"));
  108. fileMenu.add(new JMenuItem("open"));
  109. fileMenu.addSeparator();
  110. fileMenu.add(new JMenuItem("print"));
  111. fileMenu.add(new JMenuItem("exit"));
  112. fileMenu.addSeparator();
  113.  
  114. JMenu helpMenu = new JMenu("Help", true);
  115. helpMenu.add(new JCheckBoxMenuItem("Check it"));
  116. JMenu softwareHelpSubMenu = new JMenu("Software");
  117. JMenu hardwareHelpSubMenu = new JMenu("Hardware");
  118. helpMenu.add(softwareHelpSubMenu);
  119. helpMenu.add(hardwareHelpSubMenu);
  120. softwareHelpSubMenu.add(new JMenuItem("Unix"));
  121. softwareHelpSubMenu.add(new JMenuItem("NT"));
  122. softwareHelpSubMenu.add(new JMenuItem("Win95"));
  123. hardwareHelpSubMenu.add(new JMenuItem("MacOS"));
  124.  
  125. mb.add(fileMenu);
  126. mb.add(helpMenu);
  127.  
  128.  
  129. JPopupMenu jPopupMenu = new JPopupMenu();
  130. JPopupMenu(new JMenuItem("New"));
  131. JPopupMenu(new JMenuItem("Open"));
  132. jPopupMenu.show(lblNum1, 500, 500);
  133.  
  134.  
  135.  
  136. // add the labels
  137. lblNum1 = new JLabel();
  138. lblNum1.setText("Types");
  139. lblNum1.setSize(150, 25);
  140. lblNum2 = new JLabel();
  141. lblNum2.setText("Quantity/Box");
  142. lblNum2.setSize(150, 25);
  143. lblTotal = new JLabel();
  144. lblTotal.setText("Total Price:");
  145. lblTotal.setSize(150, 25);
  146.  
  147. //add check box
  148. btnAlmond = new JCheckBox("Almond London");
  149. btnMazola = new JCheckBox("Mazola Cookies");
  150.  
  151.  
  152. //add radio button
  153. delivery = new JRadioButton("Delivery");
  154. delivery.setSelected(true);
  155. noDelivery = new JRadioButton("No Delivery");
  156. noDelivery.setSelected(true);
  157.  
  158. // add the text fields
  159. txtNum1 = new JTextField();
  160. txtNum1.setColumns(15);
  161. txtNum1.setFont(new Font("Courier", Font.PLAIN, 14));
  162. txtNum2 = new JTextField();
  163. txtNum2.setColumns(15);
  164. txtNum2.setFont(new Font("Courier", Font.PLAIN, 14));
  165. txtTotal = new JTextField();
  166. txtTotal.setColumns(27);
  167. txtTotal.setFont(new Font("Courier", Font.PLAIN, 14));
  168.  
  169. //create and place buttons on the frame
  170. btnCalculate = new JButton("CALCULATE");
  171. btnClear = new JButton("CLEAR");
  172.  
  173. //add the objects to the content pane
  174. contentPane.add(lblNum1);
  175. contentPane.add(lblNum2);
  176.  
  177. contentPane.add(btnAlmond);
  178. contentPane.add(txtNum1);
  179.  
  180. contentPane.add(btnMazola);
  181. contentPane.add(txtNum2);
  182.  
  183.  
  184. //Radio button added in group
  185. ButtonGroup group = new ButtonGroup();
  186. delivery = new JRadioButton("DELIVERY ");
  187. delivery.setSelected(true);
  188. noDelivery = new JRadioButton("NO DELIVERY ");
  189. group.add(delivery );
  190. group.add(noDelivery );
  191. contentPane.add(delivery);
  192. contentPane.add(noDelivery);
  193.  
  194.  
  195. contentPane.add(lblTotal);
  196. contentPane.add(txtTotal);
  197.  
  198. //add the button objects to the content pane
  199. contentPane.add(btnCalculate);
  200. contentPane.add(btnClear);
  201.  
  202.  
  203. //register this frame as an action listener of the buttons
  204. txtNum1.addActionListener(this);
  205. txtNum2.addActionListener(this);
  206. btnAlmond.addActionListener(this);
  207. btnMazola.addActionListener(this);
  208. btnCalculate.addActionListener(this);
  209. btnClear.addActionListener(this);
  210. setDefaultCloseOperation( EXIT_ON_CLOSE);
  211. }
  212.  
  213.  
  214. private void JPopupMenu(JMenuItem jMenuItem) {
  215. // TODO Auto-generated method stub
  216.  
  217. }
  218. public void actionPerformed(ActionEvent event) {
  219. if (event.getSource() == btnCalculate ) {
  220. double total = 0.0;
  221. int almond = Integer.parseInt(txtNum1.getText());
  222. int mazola = Integer.parseInt(txtNum2.getText());
  223. if (delivery.isSelected() == true) {
  224. if (btnAlmond.isSelected() == true) {
  225. total += (20 * almond);
  226. }
  227. if (btnMazola.isSelected() == true) {
  228. total += (15 * mazola);
  229. }
  230.  
  231. txtTotal.setText("RM " + (total + 5));
  232.  
  233. } else if (noDelivery.isSelected() == true) {
  234. if (btnAlmond.isSelected() == true) {
  235. total += (20 * almond);
  236. }
  237. if (btnMazola.isSelected() == true) {
  238. total += (15 * mazola);
  239. }
  240. System.out.println("here");
  241. txtTotal.setText("RM " + total );
  242. }
  243. } else if ( event.getSource() == btnClear) {
  244. txtNum1.setText(" " + 0);
  245. txtNum2.setText(" " + 0);
  246. txtTotal.setText(" " + 0);
  247. }
  248. } // end of ActionPerformed
  249. }
Add Comment
Please, Sign In to add comment