I have a very small problem with using classes for the first
time. I am currently making a railway timetable program which takes a
company name, train number, departure time and arrival time. I have
done this by designing Train and Timetable classes and using 2 other
given to me called DerbyIO and Time.
I got the programme to work up until the point of using the time
class to get the programme to work out the 24hr clock. I decided to
get the clock part of the programme to work on it own which i did
fine. I have had trouble putting the two together. If anyone could
spare me a few mins to see where i am going wrong i would be very
gratefull.
Below is all the code!
DERBYIO CLASS
import javax.swing.*;
import java.util.StringTokenizer;
public class DerbyIO
{
public static int getInt(String message) {
int x, h;
StringTokenizer st = new StringTokenizer(message, "\n");
h = st.countTokens();
String s;
if (h == 1) {
s = JOptionPane.showInputDialog(message);
}
else {
JEditorPane jta = new JEditorPane("text/plain",message);
jta.setEditable(false);
s = JOptionPane.showInputDialog(jta);
}
try {
x = Integer.parseInt(s);
}
catch (Exception e) {
x = 0;
}
return x;
}
public static double getDouble(String message) {
double x;
int h;
StringTokenizer st = new StringTokenizer(message, "\n");
h = st.countTokens();
String s;
if (h == 1) {
s = JOptionPane.showInputDialog(message);
}
else {
JEditorPane jta = new JEditorPane("text/plain",message);
jta.setEditable(false);
s = JOptionPane.showInputDialog(jta);
}
try {
x = Double.parseDouble(s);
}
catch (Exception e) {
x = 0;
}
return x;
}
public static float getFloat(String message) {
float x;
int h;
StringTokenizer st = new StringTokenizer(message, "\n");
h = st.countTokens();
String s;
if (h == 1) {
s = JOptionPane.showInputDialog(message);
}
else {
JEditorPane jta = new JEditorPane("text/plain",message);
jta.setEditable(false);
s = JOptionPane.showInputDialog(jta);
}
try {
x = Float.parseFloat(s);
}
catch (Exception e) {
x = 0;
}
return x;
}
public static String getString(String message) {
int h;
StringTokenizer st = new StringTokenizer(message, "\n");
h = st.countTokens();
String s;
if (h == 1) {
s = JOptionPane.showInputDialog(message);
}
else {
JEditorPane jta = new JEditorPane("text/plain",message);
jta.setEditable(false);
s = JOptionPane.showInputDialog(jta);
}
return s;
}
public static void showMessage(String message) {
int x, h, w = 0;
StringTokenizer st = new StringTokenizer(message, "\n");
h = st.countTokens();
String s;
if (h == 1) {
JOptionPane.showMessageDialog(null, message);
}
else {
JEditorPane jta = new JEditorPane("text/plain", message);
jta.setEditable(false);
JOptionPane.showMessageDialog(null,jta);
}
}
}
TIME CLASS
public class Time
{
private int minutes; // time in minutes from midnight
public Time() {
// default constructor - set to midnight
minutes = 0;
}
public Time(int minutes) {
// construct a Time from a number of minutes after midnight
this.minutes = minutes % (24 * 60);
}
public Time(int hours, int minutes) {
// construct a Time from hours and minutes
this.minutes = (hours * 60 + minutes) % (24 * 60);
}
public void setTime(int hours, int minutes) {
this.minutes = (hours * 60 + minutes) % (24 * 60);
}
public int getMinutes() {
return minutes;
}
public Time add(Time t) {
// add this Time to Time t
int minutes = this.minutes + t.minutes;
return new Time(minutes);
}
public Time subtract(Time t) {
// subtract Time t from this Time
int minutes = this.minutes - t.minutes;
return new Time(minutes);
}
public boolean equals(Time t) {
// check if this Time and Time t are equal
if (this.minutes == t.minutes) return true;
return false;
}
public int compare(Time t) {
// compare this Time with Time t
if (this.minutes == t.minutes) return 0;
if (this.minutes > t.minutes) return 1;
return -1;
}
public String toString() {
// convert the Time to a String
String time = "";
int hours = minutes / 60;
int mins = minutes % 60;
time = Integer.toString(hours) + ":";
if (mins < 10) time = time + "0";
time = time + Integer.toString(mins);
return time;
}
public static Time parseTime(String s) throws NumberFormatException
{
String a = "", b = "";
s = s.trim();
int i = 0, x = 0, y = 0;
// look for the ':' symbol in the String
while (i < s.length() && s.charAt(i) != ':') {
i++;
}
if (i == s.length() || i == 0 || i > 2){
// if there isn't one or it's in the wrong place ...
NumberFormatException ee = new NumberFormatException();
// throw a NumberFormatException to the next level
throw ee;
}
// split into hours and minutes
a = s.substring(0,i);
b = s.substring(i+1, s.length());
// convert to numbers
try {
x = Integer.parseInt(a);
y = Integer.parseInt(b);
}
catch (NumberFormatException e) {
throw e; // NumberFormatException thrown to next level
}
// return the Time object
return new Time(x, y);
}
}
TIMETABLE CLASS
import DerbyIO;
import Time;
public class Timetable
{
private static String trainName;
// create 20 empty trains
private static Train[] theAccounts = new Timetable[20];
private static int counter = 0;
public Timetable(String name){
trainName = name;
}
public String getTrainName() {
return trainName;
}
public static boolean createAccount(String name, long acno, long
depTime, long arrTime) {
// create a new account at the first empty space (if there is one)
if (counter == 20) return false;
theAccounts[counter] = new Timetable(name, trno, depTime,
arrTime);
counter++;
return true;
}
public static boolean deleteAccount(long trno) {
// search for this Train
int i = 0;
while(i < counter && theAccounts[i].getAccountNumber() != trno) {
i++;
}
if (i == counter) return false;
while (i < counter-1) {
theAccounts[i] = theAccounts[i+1];
i++;
}
theAccounts[counter] = null;
counter--;
return true;
}
public static String getDetails(long trno) {
// search for this Train
int i = 0;
while(i < counter && theAccounts[i].getAccountNumber() != trno) {
i++;
}
if (i == counter) return "No such account"; // failed to find
it
// returns the train details
return theAccounts[i].toString();
}
public static void main(String[] args)
{
trainName = "Railtrack";
String option = " ";
while (!option.equals("Q") && !option.equals("q")) {
option = DerbyIO.getString("a) Add a train\nb) Delete a
train\nc) Get account details\nq) Quit");
// create a new train and initialise it
if (option.equals("a") || option.equals("A")) {
String trname = DerbyIO.getString("Train Company:");
long trno = (long) DerbyIO.getInt("Train Number:");
Time t1 = new Time(); // create a Time object
Time t2 = new Time(), t3; // and two more
String s = DerbyIO.getString("Enter the time");
String s2 = DerbyIO.getString("Enter a 2nd time");
try {
t1 = Time.parseTime(s2);
t2 = Time.parseTime(s); // convert this String to a
Time
}
catch (NumberFormatException e) {
System.err.print(e);
System.exit(0);
}
String depTime = (s2);
String arrTime = (s);
if (createAccount(trname, trno, depTime, arrTime))
DerbyIO.showMessage("Account Created OK");
else
DerbyIO.showMessage("Unable to create account");
}
// delete an existing account
if (option.equals("b") || option.equals("B")){
long trno = (long) DerbyIO.getInt("Train Number:");
if (deleteAccount(trno))
DerbyIO.showMessage("Train Deleted OK");
else
DerbyIO.showMessage("Unable to delete train details");
}
if (option.equals("c") || option.equals("C")){
long trno = (long) DerbyIO.getInt("Train Number:");
String result = getDetails(trno);
DerbyIO.showMessage(result);
}
}
DerbyIO.showMessage("Thank you for using " + trainName);
System.exit(0);
}
}
TRAIN CLASS
public class Train
{
// class attributes - these are normally 'private'
private String company;
private long trainNumber;
private String departureTime;
private String arrivalTime;
// class methods - these are normally 'public'
public Train(String trName, long trNo, String depTime, String
arrTime) {
setDetails(trName, trNo, depTime, arrTime);
}
public void setDetails(String trName, long trNo, String depTime,
String arrTime) {
company = trName;
trainNumber = trNo;
departureTime = depTime;
arrivalTime = arrTime;
}
public String getcompany() {
return company;
}
public long getTrainNumber() {
return trainNumber;
}
public String getdepartureTime() {
return departureTime;
}
public String getarrivalTime () {
return arrivalTime;
}
public String toString() {
String result = "Account holder: " + company +
"\nAccount Number: " + Long.toString(trainNumber) +
"\nDeparture Time: " + Long.toString(departureTime) +
"\nArrival Time: " + Long.toString(arrivalTime);
return result;
}
}