Create A Menu Using Methods From The Baseball Class And Team Info Class Asking U
ID: 660179 • Letter: C
Question
Create A Menu Using Methods From The Baseball Class And Team Info Class Asking User to Choose From
1.Add Player
2.Update Player
3.Show Player Stats
4.Show All Player Stats
5.Quit
When i type 1 it should make me create a player type in player first and last name, at bats , hits. When i type 2 it should let me edit player stats . When i type 3 it should display player. When i type 4 display all player stats. When i type 5 it should quit the program.
***CANT USE DO WHILE***
PLAYER CLASS
/**********************************************************************
*Program Name : CSC 111 Array Input With Random Numbers
*Author :
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: Write a program to create an array based on a
*
*
*Constructor: initializes the instance data
*updateStats: adds hits and at bats to the totals in the instance data
*toString : formats the instance data for display
*********************************************************************/
import java.text.*;
public class BaseballPlayer
{
//class constants
//class variables
private String name; //players first and last name
private int hits; //players hit total
private int atBats; //number of attempts
private float avg; //players batting average
/**********************************************************************
*Method Name : BaseballPlayer(constructor)
*Author :
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This constructor will intialize the instance data
* to values passed in via the parameter list.
*
*BEGIN BaseballPlayer
* initialize all instace data
*END Baseball Player
***********************************************************************/
public BaseballPlayer(String inName, int inHits, int inAtBats)
{
//local constants
//local variables
/******************************************************/
//initialize the instance data
name = inName;
atBats = inAtBats;
hits = inHits;
avg = (float)hits / atBats;
}//end constructor
/**********************************************************************
*Method Name : Update Stats
*Author : Professor Scheemaker
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This method will format the instance data in a
* manner fit for display
*
*BEGIN update stats(hits, at bats)
* add hits to total hits in the class data
* add at bats to total at bats in the class data
* recalculate the batting average
*END Baseball Player
***********************************************************************/
public void updateStats(int inAtBats, int inHits)
{
//local constants
//local variables
/*******************************************************************/
//update player at bats
atBats += inAtBats;
//update player hits
hits += inHits;
//recalculate the batting average
avg = (float)hits / atBats;
}//end updateStats
/**********************************************************************
*Method Name : getName
*Author :
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This method will return name
*
*
*BEGIN getName
* return name
*END getName
***********************************************************************/
public String getName()
{
//local constants
//local variables
/*******************************************************************/
//return name
return name;
}//end getName
/**********************************************************************
*Method Name : getAvgerage
*Author :
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This method will ruturn average
*
*
*BEGIN getAverage
* return avg
*END getAverage
***********************************************************************/
public float getAverage()
{
//local constants
//local variables
/*******************************************************************/
//return name
return avg;
}//end getAverage
/**********************************************************************
*Method Name : getHits
*Author :
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This method will return hits
*
*
*BEGIN getHits
* return hits
*END getHits
***********************************************************************/
public int getHits()
{
//local constants
//local variables
/*******************************************************************/
//return name
return hits;
}//end getHits
/**********************************************************************
*Method Name : toString
*Author :
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This method will format the instance data in a
* manner fit for display
*
*BEGIN BaseballPlayer
* IF(player name is too long)
* make it 10 characters long
* ELSE
* padd the name to be 10 characters long
* END IF
* Create output string
* return output string
*
*END Baseball Player
***********************************************************************/
public String toString()
{
//local constants
final int NAME_LENGTH = 10;
//local variables
String fmtName; //name from instance data formatted to NAME_LENGTH
String output; //formatted instance data
DecimalFormat fmt = new DecimalFormat("0.000");
String avgFmt = fmt.format(avg);
/*******************************************************************/
//IF(player name is too long)
if (name.length() > NAME_LENGTH)
//make it 10 characters long
fmtName = name.substring(0,10);
//ELSE name is not too long
else
//padd the name to be 10 characters long
fmtName = Util.setRight(NAME_LENGTH, name);
//END IF
//Create output string
output = Util.setLeft(29, "Name : ") + fmtName + " " +
Util.setLeft(29, "At Bats:: ") + Util.setRight(NAME_LENGTH + 5, "" + hits) + " " +
Util.setLeft(29, "Hits : ") + Util.setRight(NAME_LENGTH + 5, "" + hits) + " " +
Util.setLeft(29, "Avg : ") + Util.setRight(NAME_LENGTH + 5, "" + avgFmt);
//return output string
return output;
}//end toString
}//end BaseballPlayer class
-----------------------------------------------------
TEAM INFO CLASS
/**********************************************************************
*Program Name : TeamInfo
*Author :
*Due Date : May 11, 2015
*Course/Section : CSC 111 - 001
*Program Description: Write a program to create an array based on a
*
*
*Constructor: initializes the instance data
*addPlayer :
*updateStats:
*getStats :
*getTeamAverage:
*getTotalHits:
*toString : formats the instance data for display
*
*********************************************************************/
public class TeamInfo
{
//class constants
//class variables
private String teamName; //players team
private int totalHits; //players hit total
private int totalAtBats; //players at bats total
private float teamAverage; //batting average
private int playersCount; //player count
private int maxPlayers; //max player
private BaseballPlayer[] players;
/**********************************************************************
*Method Name : TeamInfo(constructor)
*Author : Daniel Williams
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This constructor will intialize the instance data
* to values passed in via the parameter list.
*
*BEGIN TeamInfo
* initialize all instace data
*END TeamInfo
***********************************************************************/
public TeamInfo(String tName, int max)
{
//local constants
//local variables
/******************************************************/
//initialize the instance data
teamName =tName;
maxPlayers = max;
players = new BaseballPlayer[maxPlayers];
playersCount = 0;
totalHits = getTotalHits();
teamAverage = getTeamAverage();
}//end constructor
/**********************************************************************
*Method Name : Add Player
*Author :
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This method will format the instance data in a
* manner fit for display
*
*BEGIN addPlayer(BaseballPlayer player)
* add hits to total hits in the class data
* add at bats to total at bats in the class data
* recalculate the batting average
*END addPlayer(BaseballPlayer player)
***********************************************************************/
public void addPlayer(BaseballPlayer player)
{
//local constants
//local variables
/*******************************************************************/
//
if(playersCount < maxPlayers)
{
players[playersCount] = player;
playersCount ++;
totalHits = getTotalHits();
teamAverage = getTeamAverage();
}
}//end addPlayer
/**********************************************************************
*Method Name : Update Stats
*Author :
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This method will format the instance data in a
* manner fit for display
*
*BEGIN updateStats
*
*
*
*END updateStats
***********************************************************************/
public void updateStats(String inName, int InBats, int inHits)
{
//local constants
//local variables
/*******************************************************************/
//
for(int index=0; index < playersCount; index++)
{
if(players[index].getName().equalsIgnoreCase(inName));
{
totalHits = getTotalHits();
teamAverage = getTeamAverage();
return;
}
}
System.out.println("The player" + inName + "is not a member of the team");
}//end updateStats
/**********************************************************************
*Method Name : Get Stats
*Author :
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This method will format the instance data in a
* manner fit for display
*
*BEGIN getStats
*
*
*
*END getStats
***********************************************************************/
public BaseballPlayer getStats(String inName)
{
//local constants
//local variables
/*******************************************************************/
//
for(int index =0; index < playersCount; index++)
{
if(players[index].getName().equalsIgnoreCase(inName));
{
return players[index];
}
}
return null;
}//end getStats
/**********************************************************************
*Method Name : Get Team Average
*Author :
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This method will format the instance data in a
* manner fit for display
*
*BEGIN getTeamAverage)
*
*
*
*END getTeamAverage
***********************************************************************/
public float getTeamAverage()
{
//local constants
//local variables
float sum = 0;
/*******************************************************************/
//
for(int index = 0; index < playersCount; index++)
{
sum += players[index].getAverage();
}
return sum/playersCount;
}//end getTeamAverage
/**********************************************************************
*Method Name : Get Total Hits
*Author :
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This method will format the instance data in a
* manner fit for display
*
*BEGIN getTotalHits
* add hits to total hits in the class data
* add at bats to total at bats in the class data
* recalculate the batting average
*END getTotalHits
***********************************************************************/
public int getTotalHits()
{
//local constants
//local variables
int total = 0;
/*******************************************************************/
//
for(int index = 0; index < playersCount; index++)
{
total += players[index].getHits();
}
return total;
}//end getTotalHits
/**********************************************************************
*Method Name : toString
*Author :
*Due Date : December 2, 2013
*Course/Section : CSC 111 - 001
*Program Description: This method will format the instance data in a
* manner fit for display
*
*BEGIN toString
* add hits to total hits in the class data
* add at bats to total at bats in the class data
* recalculate the batting average
*END toString
***********************************************************************/
public String toString()
{
//local constants
//local variables
/*******************************************************************/
//
String result = String.format("%-12s%12s", "Team Name: ", teamName) + " "
+ String.format("%-12s%12d", "Total Hits: ", totalHits) + " "
+ String.format("%-12s%12d", "Team Average: ", teamAverage) + " ";
//
for(int index = 0; index < playersCount; index++)
{
result += players[index];
}
//return result
return result;
}//end toString
}//end TeamInfo class
Explanation / Answer
//BaseballPlayer.java
import java.text.*;
public class BaseballPlayer
{
//class constants
//class variables
private String name; //players name
private int hits; //players hit total
private int atBats; //number of attempts
private float avg; //players batting average
public BaseballPlayer(String inName, int inHits, int inAtBats)
{
//initialize the instance data
this.name=inName;
atBats = inAtBats;
hits = inHits;
avg = (float)hits / atBats;
}//end constructor
public void updateStats(int inAtBats, int inHits)
{
//local constants
//local variables
/*******************************************************************/
//update player at bats
atBats += inAtBats;
//update player hits
hits += inHits;
//recalculate the batting average
avg = (float)hits / atBats;
}//end updateStats
public String getName()
{
//return name
return name;
}//end getName
public float getAverage()
{
//return name
return avg;
}//end getAverage
public int getHits()
{
//return name
return hits;
}//end getHits
public String toString()
{
//local constants
final int NAME_LENGTH = 10;
//local variables
String fmtName; //name from instance data formatted to NAME_LENGTH
String output; //formatted instance data
DecimalFormat fmt = new DecimalFormat("0.000");
String avgFmt = fmt.format(avg);
/*******************************************************************/
//IF(player name is too long)
if (name.length() > NAME_LENGTH)
//make it 10 characters long
fmtName = name.substring(0,10);
//ELSE name is not too long
else
//padd the name to be 10 characters long
fmtName = String.format("%s",name);
//END IF
//Create output string
output =
String.format("Name : %-29s AtBats : %-29d Hits :%-29d Avg : %-29s ",
fmtName,atBats,hits,avgFmt);
//Util.setLeft(29, "Name : ") + fmtName + " " +
//Util.setLeft(29, "At Bats:: ") + Util.setRight(NAME_LENGTH + 5, "" + hits) + " " +
//Util.setLeft(29, "Hits : ") + Util.setRight(NAME_LENGTH + 5, "" + hits) + " " +
//Util.setLeft(29, "Avg : ") + Util.setRight(NAME_LENGTH + 5, "" + avgFmt);
//return output string
return output;
}//end toString
}//end BaseballPlayer class
------------------------------------------------------------------------------------------
//TeamInfo.java
public class TeamInfo
{
//class constants
//class variables
private String teamName; //players team
private int totalHits; //players hit total
private int totalAtBats; //players at bats total
private float teamAverage; //batting average
private int playersCount; //player count
private int maxPlayers; //max player
private BaseballPlayer[] players;
/************************************
*Method Name : TeamInfo(constructor)
************************************/
public TeamInfo(String tName, int max)
{
//initialize the instance data
teamName =tName;
maxPlayers = max;
players = new BaseballPlayer[maxPlayers];
playersCount = 0;
totalHits = getTotalHits();
teamAverage = getTeamAverage();
}//end constructor
/**********************************************************************
*Method Name : Add Player
**********************************************************************/
public void addPlayer(BaseballPlayer player)
{
if(playersCount < maxPlayers)
{
players[playersCount] = player;
playersCount ++;
totalHits = getTotalHits();
teamAverage = getTeamAverage();
}
}//end addPlayer
/**********************************************************************
*Method Name : Update Stats
***********************************************************************/
public void updateStats(String inName, int InBats, int inHits)
{
for(int index=0; index < playersCount; index++)
{
if(players[index].getName().equalsIgnoreCase(inName));
{
totalHits = getTotalHits();
teamAverage = getTeamAverage();
return;
}
}
System.out.println("The player" + inName + "is not a member of the team");
}//end updateStats
/************************
*Method Name : Get Stats
************************/
public BaseballPlayer getStats(String inName)
{
for(int index =0; index < playersCount; index++)
{
if(players[index].getName().equalsIgnoreCase(inName));
{
return players[index];
}
}
return null;
}//end getStats
/*******************************
*Method Name : Get Team Average
*******************************/
public float getTeamAverage()
{
float sum = 0;
for(int index = 0; index < playersCount; index++)
{
sum += players[index].getAverage();
}
return sum/playersCount;
}//end getTeamAverage
/*******************************
*Method Name : Get Total Hits
*******************************/
public int getTotalHits()
{
int total = 0;
for(int index = 0; index < playersCount; index++)
{
total += players[index].getHits();
}
return total;
}//end getTotalHits
/***********************
*Method Name : toString
***********************/
public String toString()
{
String result = String.format("%-12s%12s", "Team Name: ", teamName) + " "
+ String.format("%-12s%12d", "Total Hits: ", totalHits) + " "
+ String.format("%-12s%12f", "Team Average: ", teamAverage) + " ";
for(int index = 0; index < playersCount; index++)
{
result += players[index];
}
//return result
return result;
}//end toString
}//end TeamInfo class
------------------------------------------------------------------------------------------
//Driver program :menu choice
//MenuProgram.java
/**Menu program that imlements the BaseballPlayer and TeamInfo classes
* and allows to user to select a menu choice*/
//MenuProgram.java
import java.util.Scanner;
public class MenuProgram
{
public static void main(String[] args)
{
//create variables to read first name
//and last name
String firstName;
String lastName;
//To read hits and bats
int inHits;
int inAtBats;
int ch;
//scanner to read input from keyboard
Scanner input=new Scanner(System.in);
//create a variable of type BaseballPlayer
BaseballPlayer baseBallPlayer=null;
//set number of palyers 9
final int NUMBER_OF_PLAYERS=9;
//create an instance of TeamInfor class and set team name and number of players in the team
TeamInfo teamInfo=new TeamInfo("BASE-Ball TEAM", NUMBER_OF_PLAYERS);
//The while loop calls the printMenu method that gets the user choice
//and calls the appropriate methods of TeamInfo class and repeats the
//calling the printMenu method until user exit from program
while(true)
{
//calling printMenu method
ch=printMenu();
//switch case to select the appropriate method
switch(ch)
{
case 1:
//read player information to add to team
System.out.println("Enter first name");
firstName=input.nextLine();
System.out.println("Enter last name");
lastName=input.nextLine();
System.out.println("Enter at bats");
inHits=input.nextInt();
input.nextLine();
System.out.println("Enter hits");
inAtBats=input.nextInt();
input.nextLine();
//create an instance of BaseballPlayer with first nama and last name as concatenated
//and hits and bats as input argumets
baseBallPlayer=new BaseballPlayer(firstName.concat(lastName), inHits, inAtBats);
//call the method addPlayer to teamInfor object
teamInfo.addPlayer(baseBallPlayer);
break;
case 2:
//read first name and last name ,bats and hits to update the player stats
System.out.println("Enter first name");
firstName=input.nextLine();
System.out.println("Enter last name");
lastName=input.nextLine();
System.out.println("Enter at bats");
inHits=input.nextInt();
input.nextLine();
System.out.println("Enter hits");
inAtBats=input.nextInt();
input.nextLine();
//call the method updateStas
teamInfo.updateStats(firstName.concat(lastName), inAtBats, inHits);
break;
case 3:
//read first name ,last name
System.out.println("Enter first name");
firstName=input.nextLine();
System.out.println("Enter last name");
lastName=input.nextLine();
//call the method getStats that takes the concatenate string of name
//and returns the palyer object as output
BaseballPlayer player=teamInfo.getStats(firstName.concat(lastName));
//check if the player object is not null
if(player!=null)
System.out.println(player.toString());
else
//otherwiser print player is not found
System.out.println("Player not found");
break;
case 4:
//print all players in the teamInfo object
System.out.println("Palyers in the Base Ball Team");
System.out.println(teamInfo.toString());
break;
case 5:
System.out.println("Exiting from program");
//exit the program
System.exit(0);
}
}
}
/*The static method printMenu that prompts user to enter a choice from
* a list of menu choices and repeat until user enters a valid menu choice*/
private static int printMenu()
{
//To read input from user
Scanner scanner=new Scanner(System.in);
int userChoice;
do
{
System.out.println("1. Add Player");
System.out.println("2. Update Player");
System.out.println("3. Show Player Stats");
System.out.println("4. Show All Player Stats");
System.out.println("5. Exit");
System.out.println("Enter your choice");
userChoice=scanner.nextInt();
if(userChoice<0 ||userChoice>5)
System.out.println("Invalid Choice.");
}while(userChoice<0 ||userChoice>5);
//return a userChoice value
return userChoice;
}
}
------------------------------------------------------------------------------------------
sample output:
1. Add Player
2. Update Player
3. Show Player Stats
4. Show All Player Stats
5. Exit
Enter your choice
1
Enter first name
r
Enter last name
r
Enter at bats
10
Enter hits
10
1. Add Player
2. Update Player
3. Show Player Stats
4. Show All Player Stats
5. Exit
Enter your choice
1
Enter first name
s
Enter last name
s
Enter at bats
10
Enter hits
10
1. Add Player
2. Update Player
3. Show Player Stats
4. Show All Player Stats
5. Exit
Enter your choice
4
Palyers in the Base Ball Team
Team Name: BASE-Ball TEAM
Total Hits: 20
Team Average: 1.000000
Name : rr AtBats : 10 Hits :10 Avg : 1.000
Name : ss AtBats : 10 Hits :10 Avg : 1.000
1. Add Player
2. Update Player
3. Show Player Stats
4. Show All Player Stats
5. Exit
Enter your choice
5
Note :Util class is not availbal in java package. There was a Util package.
Please make sure of it. Changes are made instead of Util .
Hope this helps you