Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/H24.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import classes.Card;
import utils.HelpCommands;
import utils.Node;

public class H24 extends HelpCommands {

public static void fixCardsByNum(Node<Card> node) {
while (node != null) {
Node<Card> p = node;
while (hasNum(p.getNext(), p.getValue().num())) {
Node<Card> before = findBeforeNum(p, p.getValue().num());
Card card = before.getNext().getValue();
before.setNext(before.getNext().getNext());
p.setNext(new Node<>(card, p.getNext()));
p = p.getNext();
}
node = node.getNext();
}
}

public static boolean hasNum(Node<Card> node, int num){
while (node != null) {
if (node.getValue().num() == num) {
return true;
}
node = node.getNext();
}
return false;
}

public static Node<Card> findBeforeNum(Node<Card> node, int num){
while (node.getNext() != null) {
if (node.getNext().getValue().num() == num) {
return node;
}
node = node.getNext();
}
return null;
}

public static void main(String[] args) {
Card card = new Card(3, "a");
Card card1 = new Card(4, "b");
Card card2 = new Card(3, "c");
Card card3 = new Card(2, "d");

Node<Card> node = buildNodes(card, card1, card2, card3);
fixCardsByNum(node);
System.out.println(node);
}

}
4 changes: 4 additions & 0 deletions src/classes/Card.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package classes;

public record Card(int num, String color) {
}
48 changes: 48 additions & 0 deletions src/classes/Competitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package classes;

public class Competitor {

private int minutes;
private int seconds;
private String name;

/**
* @param seconds NOT greater than 59!
*/
public Competitor(int minutes, int seconds, String name) {
this.minutes = minutes;
this.seconds = seconds;
this.name = name;
}

public boolean isBetter(Competitor other){
if (this.minutes < other.minutes) {
return true;
}
return this.seconds < other.seconds;
}

public int getMinutes() {
return minutes;
}

public void setMinutes(int minutes) {
this.minutes = minutes;
}

public int getSeconds() {
return seconds;
}

public void setSeconds(int seconds) {
this.seconds = seconds;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
39 changes: 39 additions & 0 deletions src/classes/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package classes;

import utils.HelpCommands;
import utils.Node;

public class Race {

private Node<Competitor> competitors;

public Race(){}

public void add(Competitor competitor){
if (competitors == null) {
competitors = new Node<>(competitor);
} else {
if (competitor.isBetter(competitors.getValue())) {
competitors = new Node<>(competitor, competitors);
}
Node<Competitor> temp = competitors;
boolean hasBeenPlaced = false;
while (temp.getNext() != null) {
if (competitor.isBetter(temp.getNext().getValue())) {
temp.setNext(new Node<>(competitor, temp.getNext()));
hasBeenPlaced = true;
}
temp = temp.getNext();
}
if (!hasBeenPlaced) {
temp.setNext(new Node<>(competitor));
}
}
}

public Competitor rank(int rank){
rank -= 1;
return HelpCommands.valueAt(competitors, rank);
}

}