From 8409bec46c34550a3b39f3decf780fad66d829ec Mon Sep 17 00:00:00 2001 From: ryan-redfern <78377972+ryan-redfern@users.noreply.github.com> Date: Fri, 5 Feb 2021 15:13:38 +0000 Subject: [PATCH] Add files via upload --- CycleEntry.java | 83 ++++++++++++++ Entry.java | 88 ++++++++++++++ SprintEntry.java | 79 +++++++++++++ SwimEntry.java | 76 ++++++++++++ TrainingRecord.java | 106 +++++++++++++++++ TrainingRecordGUI.java | 255 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 687 insertions(+) create mode 100644 CycleEntry.java create mode 100644 Entry.java create mode 100644 SprintEntry.java create mode 100644 SwimEntry.java create mode 100644 TrainingRecord.java create mode 100644 TrainingRecordGUI.java diff --git a/CycleEntry.java b/CycleEntry.java new file mode 100644 index 00000000..1d9601aa --- /dev/null +++ b/CycleEntry.java @@ -0,0 +1,83 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.stir.cscu9t4practical1; + +import java.util.Calendar; + +/** + * + * @author 44752 + */ +public class CycleEntry extends Entry { + private String name; + private Calendar dateAndTime; + private float distance; + + public CycleEntry (String n, int d, int m, int y, int h, int min, int s, float dist, String ter, String temp) { + super(n, d, m, y, h, min, s,dist); + Calendar inst = Calendar.getInstance(); + inst.set(y,m-1,d,h,min,s); + distance = dist; + dateAndTime = inst; + name = n; + + + tempo = temp; + terrain = ter; + } //constructor + @Override + public String getName () { + return this.name; + } //getName + @Override + public int getDay () { + return this.dateAndTime.get(Calendar.DATE); + } //getDay + @Override + public int getMonth () { + int month = this.dateAndTime.get(Calendar.MONTH) + 1; + return month; + } //getMonth + @Override + public int getYear () { + return this.dateAndTime.get(Calendar.YEAR); + } //getYear +@Override + public int getHour () { + return this.dateAndTime.get(Calendar.HOUR); + } //getHour +@Override + public int getMin () { + return this.dateAndTime.get(Calendar.MINUTE); + } //getMin +@Override + public int getSec () { + return this.dateAndTime.get(Calendar.SECOND); + } //getSec +@Override + public float getDistance () { + return this.distance; + } //getYear + @Override + public String getTerrain(){ + return this.terrain; + } + @Override + public String getTempo(){ + return this.tempo; + } + + +@Override + public String getEntry () { + String result = getName()+" cycled " + getDistance() + " km in " + +getHour()+":"+getMin()+":"+ getSec() + " on " + +getDay()+"/"+getMonth()+"/"+getYear()+" on "+getTerrain()+" at "+getTempo()+" tempo\n"; + return result; + } //getEntry + + +} diff --git a/Entry.java b/Entry.java new file mode 100644 index 00000000..f6b5829a --- /dev/null +++ b/Entry.java @@ -0,0 +1,88 @@ +// This class holds information about a single training session +package com.stir.cscu9t4practical1; + +import java.util.Calendar; +public class Entry { + private String name; + private Calendar dateAndTime; + private float distance; + String terrain; + String tempo; + int repetitions; + int recovery; + String location; + + public Entry (String n, int d, int m, int y, int h, int min, int s, float dist) { + name = n; + Calendar inst = Calendar.getInstance(); + inst.set(y,m-1,d,h,min,s); + dateAndTime = inst; + distance = dist; + } //constructor + + public String getName () { + return name; + } //getName + + public int getDay () { + return dateAndTime.get(Calendar.DATE); + } //getDay + + public int getMonth () { + int month = dateAndTime.get(Calendar.MONTH) + 1; + return month; + } //getMonth + + public int getYear () { + return dateAndTime.get(Calendar.YEAR); + } //getYear + + public int getHour () { + return dateAndTime.get(Calendar.HOUR); + } //getHour + + public int getMin () { + return dateAndTime.get(Calendar.MINUTE); + } //getMin + + public int getSec () { + return dateAndTime.get(Calendar.SECOND); + } //getSec + + public float getDistance () { + return distance; + } //getYear + public String getTempo(){ + return tempo; + + } + public String getTerrain(){ + return terrain; + + } + public int getRepetitions(){ + return repetitions; + + } + public int getRecovery(){ + return recovery; + + } + public String getWhere(){ + return location; + + } + + + + + public String getEntry () { + String result = getName()+" ran " + getDistance() + " km in " + +getHour()+":"+getMin()+":"+ getSec() + " on " + +getDay()+"/"+getMonth()+"/"+getYear()+"\n"; + return result; + } //getEntry + + + +} // Entry \ No newline at end of file diff --git a/SprintEntry.java b/SprintEntry.java new file mode 100644 index 00000000..7b2744ea --- /dev/null +++ b/SprintEntry.java @@ -0,0 +1,79 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.stir.cscu9t4practical1; + +import java.util.Calendar; + +/** + * + * @author 44752 + */ +public class SprintEntry extends Entry { + private String name; + private Calendar dateAndTime; + private float distance; + + public SprintEntry (String n, int d, int m, int y, int h, int min, int s, float dist, int rep, int rec) { + super(n, d, m, y, h, min, s, dist); + recovery = rec; + repetitions = rep; + Calendar inst = Calendar.getInstance(); + inst.set(y,m-1,d,h,min,s); + distance = dist; + dateAndTime = inst; + name = n; + } //constructor + @Override + public String getName () { + return name; + } //getName + @Override + public int getDay () { + return dateAndTime.get(Calendar.DATE); + } //getDay + @Override + public int getMonth () { + int month = dateAndTime.get(Calendar.MONTH) + 1; + return month; + } //getMonth + @Override + public int getYear () { + return dateAndTime.get(Calendar.YEAR); + } //getYear +@Override + public int getHour () { + return dateAndTime.get(Calendar.HOUR); + } //getHour +@Override + public int getMin () { + return dateAndTime.get(Calendar.MINUTE); + } //getMin +@Override + public int getSec () { + return dateAndTime.get(Calendar.SECOND); + } //getSec +@Override + public float getDistance () { + return distance; + } //getYear + @Override + public int getRepetitions(){ + return this.repetitions; + } + @Override + public int getRecovery(){ + return this.recovery; + } + +@Override + public String getEntry () { + String result = getName()+" sprinted " + getDistance() + " km in " + +getHour()+":"+getMin()+":"+ getSec() + " on " + +getDay()+"/"+getMonth()+"/"+getYear()+" with " + getRecovery() + " minutes recovery \n"; + return result; + } //getEntry + +} diff --git a/SwimEntry.java b/SwimEntry.java new file mode 100644 index 00000000..c8996cf4 --- /dev/null +++ b/SwimEntry.java @@ -0,0 +1,76 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.stir.cscu9t4practical1; + +import java.util.Calendar; + +/** + * + * @author 44752 + */ +public class SwimEntry extends Entry { + private String name; + private Calendar dateAndTime; + private float distance; + private String location; + public SwimEntry (String n, int d, int m, int y, int h, int min, int s, float dist, String loc) { + super(n, d, m, y, h, min, s, dist); + location = loc; + Calendar inst = Calendar.getInstance(); + inst.set(y,m-1,d,h,min,s); + distance = dist; + dateAndTime = inst; + name = n; + } //constructor + @Override + public String getName () { + return name; + } //getName + @Override + public int getDay () { + return dateAndTime.get(Calendar.DATE); + } //getDay + @Override + public int getMonth () { + int month = dateAndTime.get(Calendar.MONTH) + 1; + return month; + } //getMonth + @Override + public int getYear () { + return dateAndTime.get(Calendar.YEAR); + } //getYear +@Override + public int getHour () { + return dateAndTime.get(Calendar.HOUR); + } //getHour +@Override + public int getMin () { + return dateAndTime.get(Calendar.MINUTE); + } //getMin +@Override + public int getSec () { + return dateAndTime.get(Calendar.SECOND); + } //getSec +@Override + public float getDistance () { + return distance; + } //getYear + @Override + public String getWhere(){ + return location; + } + +@Override + public String getEntry () { + String result = getName()+" swam " + getDistance() + " km " + getWhere() + " in " + +getHour()+":"+getMin()+":"+ getSec() + " on " + +getDay()+"/"+getMonth()+"/"+getYear()+"\n"; + return result; + } //getEntry +} + + + diff --git a/TrainingRecord.java b/TrainingRecord.java new file mode 100644 index 00000000..131ee7d1 --- /dev/null +++ b/TrainingRecord.java @@ -0,0 +1,106 @@ +// An implementation of a Training Record as an ArrayList +package com.stir.cscu9t4practical1; + + + + +import java.util.*; + + +public class TrainingRecord { + private List tr; + + public TrainingRecord() { + tr = new ArrayList(); + } //constructor + + // add a record to the list + public void addEntry(Entry e){ + tr.add(e); + } // addClass + + // look up the entry of a given day and month + + public String lookupEntry (int d, int m, int y) { + ListIterator iter = tr.listIterator(); + String result = ""; + + int length = tr.size(); + for (int i = 0; i < length; i++ ){ + Entry current = iter.next(); + if (current.getDay()==d && current.getMonth()==m && current.getYear()==y){ + result = (result + " " + current.getEntry()); + } + } + if(result.isEmpty()){ + result = "No entries found"; + } + + + + return result; + } // lookupEntry + public String findAllByDate (int d, int m, int y) { + ListIterator iter = tr.listIterator(); + String result = ""; + + int length = tr.size(); + for (int i = 0; i < length; i++ ){ + Entry current = iter.next(); + if (current.getDay()==d && current.getMonth()==m && current.getYear()==y){ + String output = ("\n Name: " + current.getName() + " Hours: " + current.getHour() + " Mins: " + current.getMin() + " Secs: " + current.getSec()); + result = (result + " " + output); + } + } + if(result.isEmpty()){ + result = "No entries found"; + } + + + + return result; + } + public String removeEntry(int d, int m, int y, String n){ + ListIterator iter = tr.listIterator(); + + String result = "Not found"; + int length = tr.size(); + for (int i = 0; i < length; i++ ){ + Entry current = iter.next(); + if (current.getName().equals(n) && current.getDay()==d && current.getMonth()==m && current.getYear()==y);{ + iter.remove(); + result = "Removed"; + } + } + return result; + + + } + public boolean duplicate(int d, int m, int y, String n){ + ListIterator iter = tr.listIterator(); + + boolean a = false; + int length = tr.size(); + for (int i = 0; i < length; i++ ){ + Entry current = iter.next(); + if (current.getName().equals(n) && current.getDay()==d && current.getMonth()==m && current.getYear()==y);{ + + a = true; + } + } + return a; + + + } + + + // Count the number of entries + public int getNumberOfEntries(){ + return tr.size(); + } + // Clear all entries + public void clearAllEntries(){ + tr.clear(); + } + +} // TrainingRecord \ No newline at end of file diff --git a/TrainingRecordGUI.java b/TrainingRecordGUI.java new file mode 100644 index 00000000..d743fb82 --- /dev/null +++ b/TrainingRecordGUI.java @@ -0,0 +1,255 @@ +// GUI and main program for the Training Record +package com.stir.cscu9t4practical1; + +import java.awt.*; +import java.awt.event.*; +import java.util.*; +import javax.swing.*; +import java.lang.Number; +import java.awt.Dialog; +public class TrainingRecordGUI extends JFrame implements ActionListener { + + private JTextField name = new JTextField(30); + private JTextField day = new JTextField(2); + private JTextField month = new JTextField(2); + private JTextField year = new JTextField(4); + private JTextField hours = new JTextField(2); + private JTextField mins = new JTextField(2); + private JTextField secs = new JTextField(2); + private JTextField dist = new JTextField(4); + private JTextField type = new JTextField(10); + private JLabel labn = new JLabel(" Name:"); + private JLabel labd = new JLabel(" Day:"); + private JLabel labm = new JLabel(" Month:"); + private JLabel laby = new JLabel(" Year:"); + private JLabel labh = new JLabel(" Hours:"); + private JLabel labmm = new JLabel(" Mins:"); + private JLabel labs = new JLabel(" Secs:"); + private JLabel labtype = new JLabel(" Type:"); + private JLabel labdist = new JLabel(" Distance (km):"); + private JButton addR = new JButton("Add"); + private JButton lookUpByDate = new JButton("Look Up"); + private JButton findAllByDate = new JButton("Find All"); + private JButton removeEntry = new JButton("Remove Entry"); + + private TrainingRecord myAthletes = new TrainingRecord(); + + private JTextArea outputArea = new JTextArea(5, 50); + + public static void main(String[] args) { + TrainingRecordGUI applic = new TrainingRecordGUI(); + } // main + + // set up the GUI + public TrainingRecordGUI() { + + super("Training Record"); + + setLayout(new FlowLayout()); + add(labn); + add(name); + name.setEditable(true); + add(labd); + add(day); + day.setEditable(true); + add(labm); + add(month); + month.setEditable(true); + add(laby); + add(year); + year.setEditable(true); + add(labh); + add(hours); + hours.setEditable(true); + add(labmm); + add(mins); + mins.setEditable(true); + add(labs); + add(secs); + secs.setEditable(true); + add(labdist); + add(dist); + dist.setEditable(true); + add(labtype); + add(type); + type.setEditable(true); + add(addR); + addR.addActionListener(this); + + add(outputArea); + + outputArea.setEditable(false); + setSize(720, 200); + setVisible(true); + blankDisplay(); + + + // To save typing in new entries while testing, uncomment + // the following lines (or add your own test cases) + + } // constructor + + // listen for and respond to GUI events + public void actionPerformed(ActionEvent event) { + String message = ""; + + if (event.getSource() == addR) { + message = addEntry("generic"); + } + if (event.getSource() == lookUpByDate) { + message = lookupEntry(); + } + if (event.getSource() == findAllByDate) { + // to do + message = findAllByDate(); + } + if (event.getSource() == removeEntry) { + // to do + + + + message = removeEntry(); + int b = myAthletes.getNumberOfEntries(); + + if (b == 0){ + System.out.println("working"); + lookUpByDate.setEnabled(false); + findAllByDate.setEnabled(false); + removeEntry.setEnabled(false); + } + } + outputArea.setText(message); + blankDisplay(); + } // actionPerformed + + public String addEntry(String what) { + + String n = name.getText(); + int m = Integer.parseInt(month.getText()); + int d = Integer.parseInt(day.getText()); + int y = Integer.parseInt(year.getText()); + float km = java.lang.Float.parseFloat(dist.getText()); + int h = Integer.parseInt(hours.getText()); + int mm = Integer.parseInt(mins.getText()); + int s = Integer.parseInt(secs.getText()); + String input = type.getText(); + boolean a = myAthletes.duplicate(d, m, y, n); + if (a == true){ + String message = "Duplicate record"; + return message; + } + String message = "Record added\n"; + System.out.println("Adding "+what+" entry to the records"); + + + if ("Sprint".equals(input) || "sprint".equals(input)){ + int rep = Integer.parseInt(JOptionPane.showInputDialog("Enter the repetitions: ")); + int rec = Integer.parseInt(JOptionPane.showInputDialog("Enter the recovery: ")); + + Entry e = new SprintEntry(n, d, m, y, h, mm, s, km, rep, rec); + myAthletes.addEntry(e); + return message; + } + if ("Cycle".equals(input) || "cycle".equals(input)){ + String temp = JOptionPane.showInputDialog("Enter the tempo: "); + String ter = JOptionPane.showInputDialog("Enter the terrain: "); + Entry e = new CycleEntry(n, d, m, y, h, mm, s, km, ter, temp); + myAthletes.addEntry(e); + return message; + } + if ("Swim".equals(input) || "swim".equals(input)){ + String loc = JOptionPane.showInputDialog("Location: "); + + Entry e = new SwimEntry(n, d, m, y, h, mm, s, km, loc); + myAthletes.addEntry(e); + return message; + } + else { + message = "Error"; + return message; + } + + + + } + + public String lookupEntry() { + int m = Integer.parseInt(month.getText()); + int d = Integer.parseInt(day.getText()); + int y = Integer.parseInt(year.getText()); + outputArea.setText("looking up record ..."); + String message = myAthletes.lookupEntry(d, m, y); + + return message; + } + public String findAllByDate() { + int m = Integer.parseInt(month.getText()); + int d = Integer.parseInt(day.getText()); + int y = Integer.parseInt(year.getText()); + outputArea.setText("looking up record ..."); + String message = myAthletes.findAllByDate(d, m, y); + + return message; + } + public String removeEntry(){ + int m = Integer.parseInt(month.getText()); + int d = Integer.parseInt(day.getText()); + int y = Integer.parseInt(year.getText()); + String n = name.getText(); + outputArea.setText("Checking if record exists..."); + String message = myAthletes.removeEntry(d, m, y, n); + + return message; + } + + + + + + public void blankDisplay() { + name.setText(""); + day.setText(""); + month.setText(""); + year.setText(""); + hours.setText(""); + mins.setText(""); + secs.setText(""); + dist.setText(""); + type.setText(""); + int b = myAthletes.getNumberOfEntries(); + + if (b != 0){ + + + add(lookUpByDate); + + + + + + lookUpByDate.addActionListener(this); + add(findAllByDate); + findAllByDate.addActionListener(this); + removeEntry.addActionListener(this); + add(removeEntry); + + + } + + + + }// blankDisplay + // Fills the input fields on the display for testing purposes only + public void fillDisplay(Entry ent) { + name.setText(ent.getName()); + day.setText(String.valueOf(ent.getDay())); + month.setText(String.valueOf(ent.getMonth())); + year.setText(String.valueOf(ent.getYear())); + hours.setText(String.valueOf(ent.getHour())); + mins.setText(String.valueOf(ent.getMin())); + secs.setText(String.valueOf(ent.getSec())); + dist.setText(String.valueOf(ent.getDistance())); + } + +} // TrainingRecordGUI +