im learning Java and encounter some problem. Tried many hours but still cant solve. Pls bear with me. Thank you all.
How can i do a search result like shown in
and when user press enter, will go next search result? Sori for pasting all my code here. Thanks all.
import javax.swing.JOptionPane;
public class Makan {
static int[] count;
static String[] foodName;
static String[] country;
static String[] type;
static String[] description;
static String[] price;
public static void main(String[] args) {
// TODO Auto-generated method stub
count = new int[7];
count[0] = 1;
count[1] = 2;
count[2] = 3;
count[3] = 4;
count[4] = 5;
count[5] = 6;
count[6] = 7;
foodName = new String[7];
foodName[0] = "Fish and Chips";
foodName[1] = "Chicken Cutlet";
foodName[2] = "Oyako Don";
foodName[3] = "Nasi Goreng";
foodName[4] = "Seafood Hor Fun";
foodName[5] = "Ice Kachang";
foodName[6] = "Ice cream Waffles";
country = new String[7];
country[0] = "Western";
country[1] = "Western";
country[2] = "Asian";
country[3] = "Asian";
country[4] = "Asian";
country[5] = "Asian";
country[6] = "Asian";
type = new String[7];
type[0] = "Main";
type[1] = "Main";
type[2] = "Main";
type[3] = "Main";
type[4] = "Main";
type[5] = "Dessert";
type[6] = "Dessert";
description = new String[7];
description[0] = "Fish fillets in tartar sauce, served with fried chips";
description[1] = "Breaded chicken cutlet with melted cheese, served with potato and fries";
description[2] = "Chicken in Japanese rice, served with a bowl of Miso Soup";
description[3] = "Fried Rice with Satay";
description[4] = "Flat rice noodles in a tasty gravy with prawns, fish cakes and sliced fish.";
description[5] = "Shaved ice dessert with jelly, red beans, sweet corn and palm seeds, topped with sweet syrup";
description[6] = "Freshly baked waffles topped with 2 scoops icecream of your choice";
price = new String[7];
price[0] = "16.80";
price[1] = "15.90";
price[2] = "13.90";
price[3] = "10.20";
price[4] = "10.50";
price[5] = "5.60";
price[6] = "8.20";
String menu = "Main Menu\n";
menu += "==================\n";
menu += "1. Display all Menu Items\n";
menu += "2. Display Menu Item names for selection\n";
menu += "3. Search Food Type\n";
menu += "0. Quit\n";
menu += "==================";
String input;
int optionSelected;
do {
input = JOptionPane.showInputDialog(menu);
optionSelected = Integer.parseInt(input);
System.out.println(optionSelected);
if (optionSelected == 1) {
displayAllMenuItems();
} else if (optionSelected == 2) {
showAllSelections();
}
else if (optionSelected == 3) {
searchBox();
}
else if (optionSelected == 0) {
System.exit(0);
}
} while (optionSelected != 0);
}
// handle option 1...
public static void displayAllMenuItems() {
String hotkey = "";
hotkey += "==================\n";
hotkey += "Enter N for next menu item\n";
hotkey += "Enter P for previous menu item\n";
hotkey += "Enter M to return main menu\n";
int index = 0;
String input = "";
do {
String menu = "Menu Item " + count[index] + " of " + count.length;
menu += "\n================\n";
menu += "Name: " + foodName[index] + "\nCountry: " + country[index]
+ "\nType: " + type[index] + "\nDescription: "
+ description[index] + "\nPrice: " + price[index] + "\n";
menu += hotkey;
input = JOptionPane.showInputDialog(menu);
if (input.equalsIgnoreCase("n")) {
index++;
}
else if (input.equalsIgnoreCase("p")) {
index--;
}
} while (!input.equalsIgnoreCase("M"));
}
// handle option 2
public static void showAllSelections() {
int index;
String input = "";
String menu = "";
menu += "Menu Item Selection Menu\n";
menu += "==================\n";
for (index = 0; index < foodName.length; index++) {
menu += index + 1 + ". " + (foodName[index]) + "\n";
}
menu += "Please enter your selection\n";
input = JOptionPane.showInputDialog(menu);
int optionSelected;
optionSelected = Integer.parseInt(input);
listSpecificItems(optionSelected);
}
// list specific items
public static String listSpecificItems(int index) {
String menu = "";
String input = "";
menu = "Menu Item " + count[index - 1] + " of " + count.length;
menu += "\n================\n";
menu += "Name: " + (foodName[index - 1]) + "\nCountry: "
+ (country[index - 1]) + "\nType: " + (type[index - 1])
+ "\nDescription: " + (description[index - 1]) + "\nPrice: "
+ (price[index - 1]) + "\n";
menu += "================\n";
menu += "Enter M to return to main menu\n";
if (input.equalsIgnoreCase("m")) {
displayAllMenuItems();
}
input = JOptionPane.showInputDialog(menu);
return input;
}
// Handle Search Box
public static void searchBox() {
String menu = "";
String input = "";
menu = "Search By\n";
menu += "================\n";
input = JOptionPane.showInputDialog(menu);
showSearchResult(input);
}
/** stuck here how define search n show results **/
public static String showSearchResult(String input) {
}
}
Aucun commentaire:
Enregistrer un commentaire