Project 1
import javax.swing.*;
import java.util.*;
public class Team {
Random r = new Random();
private String name;
private String location = "";
private int nWins = 0;
private int nLosses = 0;
private double offense = Math.random();
private double defense = Math.random();
public Team(String name, String location) {
this.name = name;
this.location = location;
}
public String getName() {
return name;
}
public String getLocation() {
return location;
}
public int getN_Wins() {
return nWins;
}
public int getN_Losses() {
return nLosses;
}
public double getOffense() {
return offense;
}
public double getDefense() {
return defense;
}
public static double luck() {
return Math.random();
}
public int calcTotalGames() {
return getN_Wins() + getN_Losses();
}
public double calcWinRate() {
return (double) getN_Wins()/ (double) calcTotalGames() * 100;
}
public double calcLossRate() {
return (double) getN_Losses()/ (double) calcTotalGames() * 100;
}
public int calcDifference() {
return (int) Math.abs(getN_Wins() - getN_Losses());
}
public String generateStats() {
String statname = "\n" + getName() + "(" + getLocation() + ")";
String stattotalgames = "Total games: " + calcTotalGames();
String calcWinRateTwo = String.format("%.2f", calcWinRate());
String calcLossRateTwo = String.format("%.2f", calcLossRate());
String statswins = "No. wins: " + getN_Wins() + " (" + (calcWinRateTwo) + "%)";
String statslosses = "No. losses: " + getN_Losses() + " (" + (calcLossRateTwo + "%)");
String statsdifference = "Difference: " + calcDifference();
return statname + "\n" + stattotalgames + "\n" + statswins + "\n" + statslosses + "\n" + statsdifference;
}
public double homeScore(Team homeTeam) {
double homeScore = (homeTeam.getOffense() + homeTeam.getDefense() + 0.2) * luck();
return homeScore;
}
public double awayScore(Team awayTeam) {
double awayScore = (awayTeam.getOffense() + awayTeam.getDefense()) * luck();
return awayScore;
}
public void incrementWins() {
nWins += 1;
}
public void incrementLosses() {
nLosses += 1;
}
public static Team play(Team team1, boolean isHome, Team team2) {
if (isHome == true) {
if (team1.homeScore(team1) > team2.awayScore(team2)) {
team1.incrementWins();
team2.incrementLosses();
return team1;
}
else {
team2.incrementWins();
team1.incrementLosses();
return team2;
}
}
else {
if (team1.awayScore(team1) < team2.homeScore(team2)) {
team2.incrementWins();
team1.incrementLosses();
return team2;
}
else {
team1.incrementWins();
team2.incrementLosses();
return team2;
}
}
}
public static void main(String[] args) {
//------------Team 1--------------//
String unformatted_one = JOptionPane.showInputDialog("Enter the name and location for home team separated by a comma(,):");
Scanner s = new Scanner(System.in);
int where_dat_comma_at = unformatted_one.indexOf(',');
String name_uncased_one = unformatted_one.substring(0, where_dat_comma_at);
String name_low_one = name_uncased_one.substring(1, name_uncased_one.length());
String name_lower_one = name_low_one.toLowerCase();
String first_letter_one = name_uncased_one.substring(0, 1);
String first_letter_cased_one = first_letter_one.toUpperCase();
String name_one = first_letter_cased_one + name_lower_one;
String location_uncased_one = unformatted_one.substring(where_dat_comma_at + 1, unformatted_one.length());
String location_low_one = location_uncased_one.substring(1, location_uncased_one.length());
String location_lower_one =location_low_one.toLowerCase();
String location_first_letter_one = location_uncased_one.substring(0,1);
String location_one = location_first_letter_one + location_lower_one;
//------------Team 2--------------//
String unformatted_two = JOptionPane.showInputDialog("Enter the name and location for away team separated by a comma(,):");
int where_dat_comma_at_two = unformatted_two.indexOf(',');
String name_uncased_two = unformatted_two.substring(0, where_dat_comma_at_two);
String name_low_two = name_uncased_two.substring(1, name_uncased_two.length());
String name_lower_two = name_low_two.toLowerCase();
String first_letter_two = name_uncased_two.substring(0, 1);
String first_letter_cased_two = first_letter_two.toUpperCase();
String name_two = first_letter_cased_two + name_lower_two;
String location_uncased_two = unformatted_two.substring(where_dat_comma_at_two + 1, unformatted_two.length());
String location_low_two = location_uncased_two.substring(1, location_uncased_two.length());
String location_lower_two = location_low_two.toLowerCase();
String location_first_letter_two = location_uncased_two.substring(0,1);
String location_two = location_first_letter_two + location_lower_two;
Team t1 = new Team(name_one, location_one);
Team t2 = new Team(name_two, location_two);
String gOffense = String.format("%.2f", t1.getOffense());
String gDefense = String.format("%.2f", t1.getDefense());
String gOffenseTwo = String.format("%.2f", t2.getOffense());
String gDefenseTwo = String.format("%.2f", t2.getDefense());
String home_details = name_one + " from " + location_one + " rated " + gOffense + " (offense) " + gDefense + "(defense)";
String away_details = name_two + " from " + location_two + " rated " + gOffenseTwo + " (offense) " + gDefense + " (defense) ";
JOptionPane.showMessageDialog(null, "Home team is: " + home_details);
JOptionPane.showMessageDialog(null, "Away team is: " + away_details);
//Round 1
if (t1.play(t1, true, t2) == t1) {
JOptionPane.showMessageDialog(null, "Round 1\n Winner is: " + home_details);
}
else {
JOptionPane.showMessageDialog(null, "Round 1\n Winner is: " + away_details);
}
//Round 2
if (t1.play(t1, true, t2) == t1) {
JOptionPane.showMessageDialog(null, "Round 2\n Winner is: " + home_details);
}
else {
JOptionPane.showMessageDialog(null, "Round 2\n Winner is: " + away_details);
}
//Round 3
if (t1.play(t1, true, t2) == t1) {
JOptionPane.showMessageDialog(null, "Round 3\n Winner is: " + home_details);
}
else {
JOptionPane.showMessageDialog(null, "Round 3\n Winner is: " + away_details);
}
JOptionPane.showMessageDialog(null, t1.generateStats());
JOptionPane.showMessageDialog(null, t2.generateStats());
}
}