Hi there,
I am supposed to make a Java programme called prize collection
My programme is a follow:
import javax.swing.JOptionPane;
import java.util.*;
import java.lang.*;
public class PrizeCollection {
public static void main (String[]args) {
// Declare variables and arrays which will be used in the programme
String[] Name = new String [5];
String[] Description = new String [5];
String[] Color = new String [5];
float[] Price = new float [5];
int option = 0;
while (option!=4) //to exit the program when 4 is pressed
{
// create the main menu
option = Integer.parseInt(JOptionPane.showInputDialog("Please choose an option: \n"
+ "1 - Enter the details of a prize \n"
+ "2 - Print the details stored for all prizes \n"
+ "3 - Search for a prize with a particular value or by description \n"
+ "4 - Quit"));
// calling sub method to execute program when user choose an option
if (option==1)
enterDetails(Name,Color,Description,Price);
if (option==2)
printDetails(Name,Color,Description,Price);
if (option==3)
searchDetails(Name,Color,Description,Price);
}
System.exit(0);
} // end the main method
// start sub method
public static void enterDetails(String Name[], String Color[], String Description[], float Price[]){
for (int i=0; i<5; i++)
Name[i] = JOptionPane.showInputDialog(null, "Please enter the name of the prize: ");
Color[i] = JOptionPane.showInputDialog(null, "Please enter the color of the prize: ");
Description[i] = JOptionPane.showInputDialog(null, "Please enter the description of the prize: ");
Price[i] = JOptionPane.showInputDialog(null, "Please enter the value of the prize: ");
}
// method to search
public static void searchDetails(String Name[], String Color[], String Description[], float Price[])
{
String search_a_prize = JOptionPane.showInputDialog("Please enter the name of the prize you want to search for");
boolean if_a_prize_is_found = false; // the boolean is initialize from false
//loop start here
for (int i=0; i<Type.length; i++)
{
if (Name[i].equals (search_a_prize))
{
System.out.println("Successful search! A prize is found");
System.out.println("The prize is: " + Name[i] + "\t\t\t Descption: " + Description[i] + "\t\t\t Color: " + Color[i] + "\t\t\t Price: " + Price[i]);
if_a_prize_is_found = true; // then is this case the program will go to the next step
}
} // loop end here
if (if_a_prize_is_found == false)
System.out.println("No prize has been found!");
}//end of search method
// method to show the list
public static void printDetails(String Name[], String Color[], String Description[], float Price[]) // those refer to 1 in the daclaring variables
{
for (int i=0; i<Type.length; i++)
{
System.out.println(Name[i] + " " + Description[i] + " " + Color[i] + " " + Price[i]); // will print the list
} // end of loop
} // end of method
//method to exit the program
public static void Exit()
{
System.exit(0);
}
} //end the program