Hi I am confused on what I need to do next on this project I
Hi I am confused on what I need to do next on this project I have
Here is what I have to do
Problem overview:
You are to write programs to help manage and provide reports for a small bowling league.
Input specifications:
Input will consist of a text file that is comma delimited. Each record will consist of bowler number, bowler first score, bowler second score, bowler third score. You are not allowed to modify the input file in any way.
Processing requirement:
Each record’s information will be used to instantiate a “Bowler” type object. The Bowler object will have the following attributes: bowlerNumber, score 1, score 2 and score 3. All attributes are to be integers. You will need to provide the ‘set’ and ‘get’ methods for each attribute. You will also need to provide at least 1 constructor method for the Bowler class.
Output specifications:
Your program needs to allow the user to choose between multiple reports.
- Display file information
- Display file information plus the average score for each bowler
- Display file information plus the team totals for each game and a grand total for the team’s series total.
and this is the code that I have done so far
public class Bolwer
{
int bowlerNumber;
int score_1;
int score_2;
int score_3;
//creates a constructor method
public Bowler (int number, int s1, int s2, int s3)
{
bowlerNumber = number;
score_1 = s1;
score_2 = s2;
score_3 = s3;
}
//sets the bowler\'s number
public void bowlerNumber(int number)
{
bowlerNumber = number;
}
//sets the first score
public void score_1 (int s1)
{
score_1 = s1;
}
//sets the second score
public void score_2 (int s2)
{
score_2 = s2;
}
//sets the third score
public void score_3 (int s3)
{
score_3 = s3;
}
//gets the bowler number
public int bowlerNumber()
{
return number;
}
//gets the first score
public int score_1()
{
return s1;
}
//gets the second score
public int score_2()
{
return s2;
}
//gets the third score
public int score_3()
{
return s3;
}
public double average()
{
return (s1+s2+s3) / 3;
}
}
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class BowlerDemo
{
public static void main(String [] agrs) throws IOException
{
int bowlerNumber;
int score_1;
int score_2;
int score_3;
ArrayList info = new ArrayList();
//identifies the file and opens it
File file = new File (\"Scores.txt\");
//creates a new scanner type object
Scanner inputfile = new Scanner(file);
while (inputfile.hasNext())
{
String stats = inputfile.nextLine();
info.add(stats);
}
try
{
//opens the file
PrintWriter outputFile = new PrintWriter(\"Mid-Term.txt\");
int sz = info.size();
for (int i = 0; i < sz; i++)
{
outputfile.println(info.get(i).toString());
}
outputfile.close();
}
catch (Exception e)
{
System.out.pintln(\"The file cant be created\");
}
//see if the values were assigned correctly
System.out.println(bowlerNumber + score_1 + score_2 + score_3);
//creates the student variable
Bowler b = new Bowler(bowlerNumber, score_1, score_2, score_3);
//closes the files
outputFile.close();
inputfile.close();
//tells the user a file was created with the info requested
System.out.println(\"Data was written to a file called Mid-Term.txt.\");
}
}
and the data that is in the file that I am trying to read is
101, 123, 133, 165, 102, 202, 175, 220, 103, 155, 170, 140, 104, 123, 111, 99, 105, 75, 127, 133, 201, 195, 202, 187, 202, 188, 167, 175, 203, 145, 155, 165, 204, 165, 180, 133, 205, 140, 130, 125, 301, 100, 175, 90, 302, 130, 77, 120, 303, 200, 230, 193, 304, 178, 188, 193, 305, 155, 156, 157, 401, 160, 140, 155, 402, 170, 190, 185, 403, 210, 202, 233, 404, 105, 121, 133, 405, 145, 165, 185
I know I need to put in a \"split\" and proabbly use an \"ArrayList\", but I am not sure where would be a good spot to put the \"split\" in and whenever I try and put the \"ArrayList\" in my program it keeps giving me an error.
Solution
Program is Done :
Please try to Comment few lines when you want to see each Outputs
i.e Output-1, Output-2, Output-3 and Output-4
Bowler.java
public class Bowler {
String bowlerNumber;
int score1,score2,score3;
public String getBowlerNumber() {
return bowlerNumber;
}
public void setBowlerNumber(String bowlerNumber) {
this.bowlerNumber = bowlerNumber;
}
public int getScore1() {
return score1;
}
public void setScore1(int score1) {
this.score1 = score1;
}
public int getScore2() {
return score2;
}
public void setScore2(int score2) {
this.score2 = score2;
}
public int getScore3() {
return score3;
}
public void setScore3(int score3) {
this.score3 = score3;
}
}
BowlerReports.java
package Feburary;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class BowlerReports {
public static void main(String args[]){
ArrayList<Bowler> scores;
// HashMap with key as Team No and Value as ArrayList of Bowlers
HashMap<Character, ArrayList<Bowler>> map = new HashMap<Character, ArrayList<Bowler>>();
try{
//Reading each value from file using FileReader
// and storing in StrinngBuffer
FileReader fr = new FileReader(\"D:\\\\scores.txt\");
BufferedReader br= new BufferedReader(fr);
String line;
StringBuffer sb = new StringBuffer();
while((line=br.readLine())!=null){
sb.append(line);
}
//Converting into string and splitting using comma
String data[] = sb.toString().split(\",\");
int i = 0;
Bowler b;
//Iterating array by dividing each player and storing into ArrayList
// depends upon first character
//Storing into Map needs Key as First Character of ID i.e 1,2,3,4
//If key repates then appending to ArrayList
while(data.length > i){
scores = new ArrayList<Bowler>();
b = new Bowler();
b.setBowlerNumber(data[i]);
b.setScore1(Integer.parseInt(data[i+1]));
b.setScore2(Integer.parseInt(data[i+2]));
b.setScore3(Integer.parseInt(data[i+3]));
i=i+4;
scores.add(b);
//If Key repeats adding to list
if(map.containsKey(b.getBowlerNumber().charAt(0))){
ArrayList<Bowler> sl = map.get(b.getBowlerNumber().charAt(0));
sl.add(b);
map.put(b.getBowlerNumber().charAt(0), sl);
}else { //Otherwise add to List normally
map.put(b.getBowlerNumber().charAt(0),scores);
}
}
// Getting Key and Value from HashMap using entrySet
//Iterating each Bowler using Entry in Map
Set<Entry<Character, ArrayList<Bowler>>> set = map.entrySet();
Iterator<Entry<Character, ArrayList<Bowler>>> itr = set.iterator();
while(itr.hasNext()){
Entry<Character, ArrayList<Bowler>> entry = itr.next();
ArrayList<Bowler> als = entry.getValue();
Iterator<Bowler> bow = als.iterator();
int sum1,sum2,sum3;
sum1=sum2=sum3=0;
int gt=0;
while(bow.hasNext()){
Bowler bb = bow.next();
//Each Bowler Individual Score
//Below Line is for Output-1
System.out.println(bb.getBowlerNumber()+\" \"+bb.getScore1()+\" \"+bb.getScore2()+\" \"+bb.getScore3());
float avg = (bb.getScore1()+bb.getScore2()+bb.getScore3())/3;
//Average Score of Each Bowler
//Below Line is for Output-2
System.out.println(\"Average Score of \"+bb.getBowlerNumber()+\" is:\"+avg);
sum1 = sum1 + bb.getScore1();
sum2 = sum2 + bb.getScore2();
sum3 = sum3 + bb.getScore3();
gt = gt + bb.getScore1()+bb.getScore2()+bb.getScore3();
}
//Team Each Game Score
//Below Line is for Output-3
System.out.println(\"Team-\"+entry.getKey()+\" games score are:\"+sum1+\" \"+sum2+\" \"+sum3);
// Grand Total of Each Teams
//Below Line is for Output-4
System.out.println(\"Grand total for Team-\"+entry.getKey()+\" is:\"+gt);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Output 1:
101 123 133 165
102 202 175 220
103 155 170 140
104 123 111 99
105 75 127 133
201 195 202 187
202 188 167 175
203 145 155 165
204 165 180 133
205 140 130 125
301 100 175 90
302 130 77 120
303 200 230 193
304 178 188 193
305 155 156 157
401 160 140 155
402 170 190 185
403 210 202 233
404 105 121 133
405 145 165 185
Output-2:
Average Score of 101 is:140.0
Average Score of 102 is:199.0
Average Score of 103 is:155.0
Average Score of 104 is:111.0
Average Score of 105 is:111.0
Average Score of 201 is:194.0
Average Score of 202 is:176.0
Average Score of 203 is:155.0
Average Score of 204 is:159.0
Average Score of 205 is:131.0
Average Score of 301 is:121.0
Average Score of 302 is:109.0
Average Score of 303 is:207.0
Average Score of 304 is:186.0
Average Score of 305 is:156.0
Average Score of 401 is:151.0
Average Score of 402 is:181.0
Average Score of 403 is:215.0
Average Score of 404 is:119.0
Average Score of 405 is:165.0
Output-3:
Team-1 games score are:678 716 757
Team-2 games score are:833 834 785
Team-3 games score are:763 826 753
Team-4 games score are:790 818 891
Output-4:
Grand total for Team-1 is:2151
Grand total for Team-2 is:2452
Grand total for Team-3 is:2342
Grand total for Team-4 is:2499
Total Output:
101 123 133 165
Average Score of 101 is:140.0
102 202 175 220
Average Score of 102 is:199.0
103 155 170 140
Average Score of 103 is:155.0
104 123 111 99
Average Score of 104 is:111.0
105 75 127 133
Average Score of 105 is:111.0
Team-1 games score are:678 716 757
Grand total for Team-1 is:2151
201 195 202 187
Average Score of 201 is:194.0
202 188 167 175
Average Score of 202 is:176.0
203 145 155 165
Average Score of 203 is:155.0
204 165 180 133
Average Score of 204 is:159.0
205 140 130 125
Average Score of 205 is:131.0
Team-2 games score are:833 834 785
Grand total for Team-2 is:2452
301 100 175 90
Average Score of 301 is:121.0
302 130 77 120
Average Score of 302 is:109.0
303 200 230 193
Average Score of 303 is:207.0
304 178 188 193
Average Score of 304 is:186.0
305 155 156 157
Average Score of 305 is:156.0
Team-3 games score are:763 826 753
Grand total for Team-3 is:2342
401 160 140 155
Average Score of 401 is:151.0
402 170 190 185
Average Score of 402 is:181.0
403 210 202 233
Average Score of 403 is:215.0
404 105 121 133
Average Score of 404 is:119.0
405 145 165 185
Average Score of 405 is:165.0
Team-4 games score are:790 818 891
Grand total for Team-4 is:2499






