Fix the problem in the Golf project (the problem is described at the end of “Gol
ID: 3628587 • Letter: F
Question
Fix the problem in the Golf project (the problem is described at the end of “GolfApp.java” shown below). In your modified project, the user should be able to add golfers and also check those golfers arbitrarily to see whether they are in the list. Provide a menu of operations such that the user can perform add, remove, and search.
//---------------------------------------------------------------------
// GolfApp.java by Dale/Joyce/Weems Chapter 6
//
// Allows user to enter golfer name and score information.
// Displays information ordered by score.
//----------------------------------------------------------------------
import java.util.Scanner;
//import ch06.lists.*;
//import support.*; // Golfer
public class GolfApp
{
public static void main(String[] args)
{
Scanner conIn = new Scanner(System.in);
String name; // golfer's name
int score; // golfer's score
SortedListInterface golfers = new ArraySortedList(20);
Golfer golfer;
String skip; // Used to skip rest of input line after reading integer
System.out.print("Golfer name (press Enter to end): ");
name = conIn.nextLine();
while (!name.equals(""))
{
System.out.print("Score: ");
score = conIn.nextInt();
skip = conIn.nextLine();
golfer = new Golfer(name, score);
golfers.add(golfer);
System.out.print("Golfer name (press Enter to end): ");
name = conIn.nextLine();
}
System.out.println();
System.out.println("The final results are");
System.out.println(golfers);
//--------------------------------------------------------------------------
// Problem 1 of assignment one, ACSC220, fall 2011
//
// Add Jeffrey with score 111 to the list. The object can not be founded.
// You are asked to fix the problem in problem 1 of assignment one.
//--------------------------------------------------------------------------
Golfer aGolfer = new Golfer("Jeffrey", 70);
if ( golfers.contains( aGolfer ) )
System.out.print( aGolfer + " is in the list." );
else
System.out.print( aGolfer + " is NOT in the list." );
//--------------------------------------------------------------------------
}
}
Explanation / Answer
You need to have a menu function, like the following: public void showMenu( void ) { println( "1. Add Golfer" ); println( "2. Remove Golfer" ); println( "3. Search for Golfer" ); println( "4. Exit" ); } And a function to test the input and call the appropriate function: public void GetInput( void ) { boolean done = false; String resp = ""; InputStreamReader converter = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(converter); while( done == false ) { showMenu(); resp = in.readLine(); if( resp == "1" ) addGolfer(); else if( resp == "2" ) removeGolfer(); else if( resp == "3" ) searchGolfer(); else if( resp == "4" ) done = true; } } Once you have this, you just need to implement these functions: addGolfer, removeGolfer, and searchGolfer. public void addGolfer( void ) { String name; int score; Golfer golfer; InputStreamReader converter = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(converter); println( "Enter the name of the new golfer:" ); name = in.readLine(); println( "Enter the golfer's score:" ); score = in.readInt(); golfer = new Golfer( name, score ); } Following this design, it should be easy to write the last two functions to implement search (you already have the code pretty much) and remove. Separating the behavior into discrete functions makes it easier to work with, but you can rearrange things however you wish. From here, the rest should be easy.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.