In: Computer Science
Write a program (C++) which allows a League of Legends player to both store and output statistics on their games played. When the program starts it allows the user to enter new entries. The entries contain the following information: Champion Name Kills Assists Deaths Win? Each entry is appended to a file called games.txt. When the user is done entering data the statistics from the games in the file are to be displayed. The average kills, assists, and deaths are to be displayed with the percentage of wins to be displayed.
Here is the solution of the above problem in java. Save the file with name Main.java and run javac Main.java . To get output run java Main.
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
class Player {
//Champion Name Kills Assists Deaths Win
String championName;
Integer kills;
Integer assists;
Integer deaths;
Integer wins;
Player(String championName, Integer kills, Integer assists,
Integer deaths, Integer wins) {
this.championName = championName;
this.kills = kills;
this.assists = assists;
this.deaths = deaths;
this.wins = wins;
}
}
class Application {
private ArrayList<Player> players;
private Scanner sc;
private BufferedWriter out;
// file name
private final String FILE_NAME = "games.txt";
Application() throws IOException {
players = new ArrayList<>(); // initialize players list
sc = new Scanner(System.in); // for taking system input
out = new BufferedWriter(new FileWriter(FILE_NAME, true));//
initialization bufferedWriter
}
// Enter details of players
void enterPlayerData() throws IOException {
//Champion Name Kills Assists Deaths Win
String choice; // for storing choice (yes/no)
do { // run until choice is other than yes
System.out.println("Enter Champion Name?");
String name = sc.next();
System.out.println("Enter number of kills?");
Integer kills = sc.nextInt();
System.out.println("Enter number of assists?");
Integer assists = sc.nextInt();
System.out.println("Enter number of deaths?");
Integer deaths = sc.nextInt();
System.out.println("Enter number of wins?");
Integer win = sc.nextInt();
//create instance of player and store it into player list
players.add(new Player(name, kills, assists, deaths, win));
String str = name + "\t" + kills + "\t" + assists + "\t" + deaths +
"\t" + win + "\n";
out.write(str);
System.out.println("Do you want to enter more(yes/no)?");
choice = sc.next();
} while (choice.equalsIgnoreCase("yes"));
// close file
out.close();
// for printing statics of entered data... it does not consisting
old data which file already have
System.out.println("\n\nPrinting statics of players....");
printStatics();
}
private void printStatics() {
Integer totalKills = 0;
Integer totalAssists = 0;
Integer totalDeaths = 0;
Integer totalWins = 0;
// loop through players and store that into appropriate
variables
for (Player p : players) {
totalAssists += p.assists;
totalKills += p.kills;
totalDeaths += p.deaths;
totalWins += p.wins;
}
// change formula according to your need ... here i'm doing
is
// calculating total and dividing with total number of players...
you can change according to your need
// average kills = (total number of kills / total players)
System.out.println("Average kills: " +
Float.valueOf(totalKills)/players.size());
// average assists = (total number of assists / total
players)
System.out.println("Average Assists: "+
Float.valueOf(totalAssists)/players.size());
// average deaths = (total number of deaths / total
players)
System.out.println("Average Deaths: "+
Float.valueOf(totalDeaths)/players.size());
// percentage wins = (total number of win / (total number of
wins + total number of deaths))
System.out.println("Percentage of wins: "+
(Float.valueOf(totalWins)/(totalWins + totalDeaths))*100);
}
}
// Driver class
public class Main {
public static void main(String[] args) throws IOException {
// create instance of Application
Application app = new Application();
app.enterPlayerData(); // call function enterPlayerData()
}
}
//Output