Log in

View Full Version : Need some advices to achieve solution for my homework


thientanchuong
Nov 22, 2007, 09:26 PM
Dear all,

I created Java code for User Interface but I do not know how to do the operators in the mutator and accessor methods. Can you help me to achieve solution of this assignment?

This is my Assignment

Requirements

The requirements are to produce an interface that will:
1. permit the ticket clerk to sell tickets
a. to any of 5 named destinations - you decide the train company and the names and prices of the five destinations
b. that are first or second class where first class is 2.6 times the second class fare
c. that are single or return, where a standard return is 1.9 times the single fare and an economy return specifies the dates of
travel and costs 0.8 times of the price of a standard return
2. print the ticket to the standard output
3. provide a summary of the transactions comprising:
a. total money taken
b. a printout of all the tickets issued
4. handle erroneous input with suitable error messages.

Note that the system is only used for journeys starting from the railway station where it is installed so only the destination station needs to be entered.

Design suggestions

There could be a class TicketMachine that generates the user interface.
There could also be a class Ticket with fields (together with accessor and mutator methods) for the price, class of travel (first or second) and destination, as well as another field called details to hold all of these details in the form of a string for printing.
A subclass of Ticket called ReturnTicket could calculate the price of a Return ticket as being 1.9 times that of a Ticket and make reference to the fact that it is a Return Ticket in the field details.
A subclass of ReturnTicket called EconomyReturnTicket could have extra fields to hold the dates of departure and return and make reference in the field details to the fact that it is an Economy Return Ticket that is only valid on certain dates.
TicketMachine could create a new Ticket or appropriate subclass each time a ticket is issued and store the tickets in an array list.

Remember to test your solution fully . You should implement and test the following:
• All input, output and error message boxes display correctly
• Strings are entered for the departure and return dates
o Error message is displayed if either string is empty for an Economy Return
o Error message is displayed if either string is not empty for a Single Ticket or Standard Return
• Any positive number is accepted in the number of passengers box
o Error message is displayed for negative or zero entries
o Error message is displayed for a string that is not an integer
o Error message is displayed for an empty string
o Re-entry is requested if there is an error
• All information is displayed correctly on each travel ticket
o Destination matches entry
o Travel type matches entry
o Fares are correctly calculated and match destination
 First Class calculations are correct
 Return fare calculations are correct
 Economy Return fare calculations are correct
o Total number of passengers is correct
o Total ticket cost is correct
• All information is calculated and displayed correctly on the Summary output box
o List of all tickets issued
o Correct total money collected

This is is my code created by using BlueJ program:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TicketMachine extends JFrame
{

private JComboBox dropDownList, dropDownList1;
private JButton bt1, bt2, bt3, bt4;
private JLabel lb, lb2, lb3, lb4, lb5, lb6;
private JPanel p1,p2,p3,p4;
private JTextField t1, t2, t3;

private final String destination[] = { "London", "Cambridge", "Bournemouth",
"Brighton", "Oxford"};
private final String type[] = { "1st Class", "2nd class"};


/**
* Constructor for objects of class ComboBoxTest
*/
public TicketMachine()
{
super(" TicketMachine");

setLayout(new FlowLayout ());
p1=new JPanel ();
p2=new JPanel ();
p2.setLayout(new GridLayout(4,2,5,10));
p3=new JPanel ();
p3.setLayout(new FlowLayout(FlowLayout.CENTER,5,10));
p4 = new JPanel();
p4.setLayout(new GridLayout(3,2,5,10));
add(p1);
add(p2);
add(p3);
add(p4);

lb= new JLabel (" PVH Railway Company");
lb2= new JLabel ("Destination");
dropDownList = new JComboBox(destination);
p1.add(lb);
p2.add(lb2);
p2.add(dropDownList);

lb3= new JLabel ("Type of Class");
dropDownList1 = new JComboBox( type );
p2.add(lb3);
p2.add(dropDownList1);

lb4=new JLabel ("Number of Passengers");
t1= new JTextField(10);
p2.add(lb4);
p2.add(t1);


bt1 = new JButton("Single way");
bt2= new JButton ("Standard Return");
bt3= new JButton ("Economic Return");
ButtonHandler handler = new ButtonHandler();
bt1.addActionListener( handler );
bt2.addActionListener( handler );
bt3.addActionListener( handler );
p3.add(bt1);
p3.add(bt2);
p3.add(bt3);

lb5 =new JLabel ("Departute Date");
lb6 =new JLabel ("Return Date");
t2= new JTextField (10);
t3= new JTextField (10);
bt4 = new JButton ("Ticket Receipt");
p4.add(lb5);
p4.add(t2);
p4.add(lb6);
p4.add(t3);
p4.add(bt4);

setSize( 400, 500 );
setVisible( true );
}
// inner class for button event handling,
This method is put for nothing, while I am waiting for the real code to replace later.
{
// handle button event
public void actionPerformed( ActionEvent event )
{
destination();
}
} // end private inner class ButtonHandler

public void destination()
{
JOptionPane.showMessageDialog( this,
"Your Destination is : " + dropDownList.getSelectedItem() );
}

}


If you have any advices or solutions , please , send it to me via email: [email protected]

Thanks a lot