Question

Please help me with this Java exercise.

Class Exercise Java Swing 1. Create the following user interface: X Bicycle Exercise Customer Name: Number of days Model 1 Mo

Requirements:

1. The user will enter name and number of days.

2. Choose a bike model and  a picture should be displayed for every model option.

3. User will have an option if he or she is a senior citizen or a member.

4. The user will enter compute button and the program will output the total.

5. The total is based on the following:

a. Model 1 is 14 dollars per day

b. Model 2 is 12 dollars per day

c. Model 3 is 10 dollars per day.

d. If the user rents for 6 to 10 days, a discount of 8% is given.

e. If the user rents for more than 10 days, a discount of 15 percent is given.

6. Provide validations for entries.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*Java GUI program that allows user to enter
* the name of the customer and number of days
* as input text field values. Then select the model
* of the bicycle and then allows to select the check box
* and then allow to press compute button and then
* display a message box of the name, days and cost */

//BicycleExercise.java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class BicycleExercise extends JFrame
{
  
   private JTextField txtName=new JTextField(20);
   private JTextField txtDays=new JTextField(20);
   private JRadioButton rdModel1=new JRadioButton("Model1");
   private JRadioButton rdModel2=new JRadioButton("Model2");
   private JRadioButton rdModel3=new JRadioButton("Model3");
   private JCheckBox seniorCheckBox=new JCheckBox("Senior Citizen");
   private JCheckBox memberCheckBox=new JCheckBox("Member");
   private ImageIcon icon = new ImageIcon("model1.PNG");
   private JButton btnCompute = new JButton("Compute");
   private JLabel imgIcon=new JLabel();

   //main method
   public static void main(String[] args)
   {
       new BicycleExercise();
   }
   /*constructor to set up the window */
   public BicycleExercise()
   {
       setLayout(new BorderLayout());
       setTitle("Bicycle Exercise");
       setSize(600, 400);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       //panel to hold name and number of days
       JPanel topPanel=new JPanel();
       topPanel.setLayout(new GridLayout(2, 2,10,10));
       topPanel.add(new JLabel("Customer Name:"));
       topPanel.add((txtName));
       topPanel.add(new JLabel("Number of days"));
       topPanel.add(txtDays);
       add(topPanel ,BorderLayout.NORTH);

       //panel to hold radio buttons and check boxes
       JPanel centerPanel=new JPanel();
       centerPanel.setLayout(new GridLayout(2, 4));
       imgIcon.setPreferredSize(new Dimension(500, 500));
       imgIcon.setIcon(icon);
       centerPanel.add(imgIcon);
       ButtonGroup group=new ButtonGroup();
       group.add(rdModel1);
       group.add(rdModel2);
       group.add(rdModel3);  

       //add action listener to the radio buttons to update the model
       rdModel1.addActionListener(new RadioListener());
       rdModel2.addActionListener(new RadioListener());
       rdModel3.addActionListener(new RadioListener());

       centerPanel.add(rdModel1);
       centerPanel.add(rdModel2);
       centerPanel.add(rdModel3);      
       centerPanel.add(new JLabel());
       centerPanel.add(seniorCheckBox);
       centerPanel.add(memberCheckBox);         
       add(centerPanel ,BorderLayout.CENTER);

       //panel to hold total and compute button on bottom
       JPanel bottomPanel=new JPanel();
       bottomPanel.setLayout(new GridLayout(1, 2,10,10));
       bottomPanel.add(new JLabel("Total"),SwingConstants.CENTER);
      
       //add action listener to button to compute the cost
       btnCompute.addActionListener(new ButtonListener());
       bottomPanel.add(btnCompute);
       add(bottomPanel ,BorderLayout.SOUTH);      
       setVisible(true);
   }
   /*Inner class for the Radio buttons*/
   private class RadioListener implements ActionListener
   {
       //Override
       public void actionPerformed(ActionEvent ae)
       {
           JRadioButton rad=(JRadioButton)ae.getSource();
           if(rad==rdModel1)
           {
               imgIcon.setIcon(new ImageIcon("model1.PNG"));          
           }
           if(rad==rdModel2)
           {
               imgIcon.setIcon(new ImageIcon("model2.jpg"));          
           }
           if(rad==rdModel3)
           {
               imgIcon.setIcon(new ImageIcon("model3.jpg"));          
           }
       }
   }
   /*Inner class for the Button */
   private class ButtonListener implements ActionListener
   {
       //Override
       public void actionPerformed(ActionEvent ae)
       {
           final double DISCOUNT1=0.08;
           final double DISCOUNT2=0.15;
           double discount=0;
           final int MODEL1=14;
           final int MODEL2=12;
           final int MODEL3=10;
           double cost=0;

           try
           {
               String name=txtName.getText();
               int numDays=Integer.parseInt(txtDays.getText());
               //find the discount percent
               if(numDays>=6 && numDays<=10)
                   discount=DISCOUNT1;
               else if(numDays>10)
                   discount=DISCOUNT2;
               //find the user selected the radio button and calculate cost
               if(rdModel1.isSelected())
               {
                   cost=numDays*MODEL1-(numDays*MODEL1*discount);
               }
               else if(rdModel2.isSelected())
               {
                   cost=numDays*MODEL2-(numDays*MODEL2*discount);
               }
               else if(rdModel2.isSelected())
               {
                   cost=numDays*MODEL3-(numDays*MODEL3*discount);
               }
               //display the cost message
               JOptionPane.showMessageDialog(null,
                       "Name: "+name
                       +"\nNumber of Days: "+numDays
                       +"\nTotal cost,$ : "+cost,
                       "Total Cost",                   
                       JOptionPane.INFORMATION_MESSAGE);
           }
           catch (Exception e)
           {
               JOptionPane.showMessageDialog(null,
                       "Enter valid input",
                       "Error",                   
                       JOptionPane.ERROR_MESSAGE);
           }


       }

   }

}
------------------------------------------Sample Run#----------------------------------------

- х Bicycle Exercise Customer Name: Number of days Model1 Model2 Model3 Senior Citizen Member Total Compute

Enter name, days, select model2 and check member and click on compute

- Bicycle Exercise Customer Name: MARK Number of days 10 Model1 Model2 Model3 Total Cost i Name: MARK Number of Days: 10 Tota

Add a comment
Know the answer?
Add Answer to:
Please help me with this Java exercise. Requirements: 1. The user will enter name and number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program called Flying.java that, firstly, prompts (asks) the user to enter an input...

    Write a Java program called Flying.java that, firstly, prompts (asks) the user to enter an input file name. This is the name of a text file that can contain any number of records. A record in this file is a single line of text in the following format: Num of passengers^range^name^manufacturer^model where: Num of passengers is the total number of people in the plane. This represents an integer number, but remember that it is still part of the String so...

  • This is a Java beginner class. Write the following exercise to check the user inputs to...

    This is a Java beginner class. Write the following exercise to check the user inputs to be: Valid input and positive with using try-catch (and/or throw Exception ) ************************************************************************** Write a program to prompt the user for hours and rate per hour to compute gross pay. Calculate your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Your code should have at least the following methods to calculate the pay. (VOID and...

  • Please design a Java GUI application with two JTextField for user to enter the first name...

    Please design a Java GUI application with two JTextField for user to enter the first name and last name. Add two JButtons with action events so when user click on one button to generate a full name message from the user input and put it in a third JTextField which is not editable; and click on the other button to clear all three JTextField boxes. Please run the attached nameFrame.class file (Note: because there is an actionhandler inner class in...

  • Please do it in Java, thanks FIA izle PX(19314) Functional requirements: 1) Enter the student numbers,...

    Please do it in Java, thanks FIA izle PX(19314) Functional requirements: 1) Enter the student numbers, names, and grades of m courses for n students. 2) Calculate the average score. Output the score list in descending order of average score. 3) The average score, the highest score and the lowest score of all subjects in the whole group are output. 4) Enter the name query results 5) Can connect to the database and realize the functions of query, addition, deletion...

  • Java Programming. Please make sure program compiles, the code is copyable, and screenshots of the output...

    Java Programming. Please make sure program compiles, the code is copyable, and screenshots of the output are provided for 5 stars :-) Calculation for speed is just supposed to be google searched 1) 18 points] You've been hired by Pedal Punchers to write a Java console application that estimates bicycle speed in miles per hour. Use a validation loop to prompt for and get from the user the wheel diameter of a bicycle in inches in the range 10-50. Then...

  • (Java) Rewrite the following exercise to check the user inputs to be: Valid input and positive...

    (Java) Rewrite the following exercise to check the user inputs to be: Valid input and positive with using try-catch (and/or throw Exception ) ************************************************************************** Write a program to prompt the user for hours and rate per hour to compute gross pay. Calculate your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Your code should have at least the following methods to calculate the pay. (VOID and NON-STATIC) getInputs calculatePay printPay Create...

  • Create the following programs in Java {Java1 difficulty} [Please create as simple as possible] a. Ask...

    Create the following programs in Java {Java1 difficulty} [Please create as simple as possible] a. Ask the user to enter their favorite number and favorite word. Pass both of these values to a method that will print the favorite word vertically the favorite number of times. b. Ask the user to enter 2 integers. Pass the integers to 2 different methods: one method to add the integers and print the sum (from the method); the second method to multiply the...

  • C++ please! (1) Prompt the user to enter the name of the input file. The file...

    C++ please! (1) Prompt the user to enter the name of the input file. The file will contain a text on a single line. Store the text in a string. Output the string. Ex: Enter file name: input1.txt File content: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! (2) Implement a PrintMenu() function,...

  • JAVA Developing a graphical user interface in programming is paramount to being successful in the business...

    JAVA Developing a graphical user interface in programming is paramount to being successful in the business industry. This project incorporates GUI techniques with other tools that you have learned about in this class. Here is your assignment: You work for a flooring company. They have asked you to be a part of their team because they need a computer programmer, analyst, and designer to aid them in tracking customer orders. Your skills will be needed in creating a GUI program...

  • Create a Console application for a library and name it FineForOverdueBooks. The Main( ) method asks...

    Create a Console application for a library and name it FineForOverdueBooks. The Main( ) method asks the user to input the number of books that are overdue and the number of days they are overdue. Pass those values to a method that displays the library fine, which is 10 cents per book per day for the first seven days a book is overdue, then 20 cents per book per day for each additional day. Grading criteria 1. Create a Console...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT