-3

I am trying to convert a clock in and clock out program given by my lecturer at my universtiy to OOP. This is the original source code.

package gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class JAVASwingForm {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JAVASwingForm window = new JAVASwingForm();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public JAVASwingForm() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 730, 489);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        textField = new JTextField();
        textField.setBounds(228, 28, 86, 20);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JLabel lblName = new JLabel("Clock In");
        lblName.setBounds(65, 31, 100, 14);
        frame.getContentPane().add(lblName);

        JLabel lblPhone = new JLabel("Clock Out");
        lblPhone.setBounds(65, 68, 100, 14);
        frame.getContentPane().add(lblPhone);

        textField_1 = new JTextField();
        textField_1.setBounds(228, 65, 86, 20);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);

        JButton btnSubmit = new JButton("Calculate");

        btnSubmit.setBounds(65, 200, 89, 23);
        frame.getContentPane().add(btnSubmit);

        JLabel timeResult = new JLabel("Time Difference will show here");
        timeResult.setBounds(65, 115, 200, 14);
        frame.getContentPane().add(timeResult);

        btnSubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (textField.getText().isEmpty() || textField_1.getText().isEmpty())
                    JOptionPane.showMessageDialog(null, "Data Missing");
                else {
                    //Nicholas - implementation and customization starts from here
                    String time1 = textField.getText(); 
                    String time2 = textField_1.getText();

                    SimpleDateFormat format = new SimpleDateFormat("HH:mm");
                    Date date1 = null;
                    Date date2 = null;
                    try {
                        date1 = format.parse(time1);
                        date2 = format.parse(time2);
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        timeResult.setText(e.toString());
                    }
                    // Nicholas - Substraction from here in miliseconds
                    long diff = date2.getTime() - date1.getTime();

                    // Nicholas - convert milisecond to hours and minutes
                    long diffMinutes = diff / (60 * 1000) % 60;
                    long diffHours = diff / (60 * 60 * 1000) % 24;

                    timeResult.setText(diffHours + " hours, and " + diffMinutes + " Minutes");

                }

            }
        });

    }
}

And this is my conversion so far.

package gui.form;


public class Backend implements Clock {

    public double timelength;
    private double TimeIn , TimeOut;
    private double calcTime;

    public double timelength(){
        return calcTime;
}
    public Backend(){
    }
    public double calcTime(){
        return( TimeIn - TimeOut);
    }

    public double getTimelength() {
        return timelength;
    }

    public void setTimelength(double timelength) {
        this.timelength = timelength;
    }

    public double getTimeIn() {
        return TimeIn;
    }

    public void setTimeIn(double TimeIn) {
        this.TimeIn = TimeIn;
    }

    public double getTimeOut() {
        return TimeOut;
    }

    public void setTimeOut(double TimeOut) {
        this.TimeOut = TimeOut;
    }

    @Override
    public double setTime() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public double calcTime(double timelength) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    }


package gui.form;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class Form {
    private double timelength;
    private double TimeIn , TimeOut;


    /**
     * Creates new form Form
     */
    public Form() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        TItxt = new javax.swing.JTextField();
        TOtxt = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        LTimetxt = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Time In");

        jLabel2.setText("Time Out");

        TItxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TItxtActionPerformed(evt);
            }
        });

        jButton1.setText("Enter");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Length of Time");

        LTimetxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LTimetxtActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(29, 29, 29))
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1))
                    .addComponent(jLabel3))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(TOtxt, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                    .addComponent(TItxt)
                    .addComponent(LTimetxt))
                .addContainerGap(166, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(TItxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(TOtxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(LTimetxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 109, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        {
            TItxt.getText();
            TOtxt.getText();
        }
         return   LTimetxt.setText();


// TODO add your handling code here:

    }                                        

    private void TItxtActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
    }                                     

    private void LTimetxtActionPerformed(java.awt.event.ActionEvent evt) {                                         
     // TODO add your handling code here:
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Form().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JTextField LTimetxt;
    private javax.swing.JTextField TItxt;
    private javax.swing.JTextField TOtxt;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration                   

    public double getTimelength() {
        return (TimeIn - TimeOut);
    }

    public void setTimelength(double timelength) {
        this.timelength = (TimeIn - TimeOut);
    }

    public double getTimeIn() {
        return TimeIn;
    }

    public void setTimeIn(double TimeIn) {
        this.TItxt = TItxt;
    }

    public double getTimeOut() {
        return TimeOut;
    }

    public void setTimeOut(double TimeOut) {
        this.TOtxt = TOtxt;
    }

    public JTextField getTItxt() {
        return TItxt;
    }

    public void setTItxt(JTextField TItxt) {
        this.TItxt = TItxt;
    }

    public JTextField getTOtxt() {
        return TOtxt;
    }

    public void setTOtxt(JTextField TOtxt) {
        this.TOtxt = TOtxt;
    }

    public JButton getjButton1() {
        return jButton1;
    }

    public void setjButton1(JButton jButton1) {
        this.jButton1 = jButton1;
    }

    public JLabel getjLabel1() {
        return jLabel1;
    }

    public void setjLabel1(JLabel jLabel1) {
        this.jLabel1 = jLabel1;
    }

    public JLabel getjLabel2() {
        return jLabel2;
    }

    public void setjLabel2(JLabel jLabel2) {
        this.jLabel2 = jLabel2;
    }

    public JTextField getLTimetxt() {
        return LTimetxt;
    }

    public int setLTimetxt(JTextField LTimetxt) {
        this.LTimetxt = LTimetxt;
        return (int) (TimeIn - TimeOut);
    }



    private void setVisible(boolean b) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


}

package gui.form;

public interface Clock {// template of the class

    public double setTime(); //method to be implemented at the sub class

    public double calcTime(double timelength); //method to be implemented at the sub class

}

package gui.form;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class JAVASwingForm {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        Backend backend = new Backend();
                Form form = new Form();
    }
}

I have set up 2 classes, a main class and a interface, however, I cant seem to get it to work and always seem to hit an error. enter image description here

  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 05 '19 at 12:42

1 Answers1

-1

Your Problem as seen in your Image is that you return a value in a void method.

kSp
  • 224
  • 2
  • 13
  • The downvote might be because this kind of answer is more suited as a comment. (I am not the one who downvoted) – Mark Melgo Mar 05 '19 at 08:24