public class BookStore {
/**************************************************
Kim works in the school bookstore, selling pens, notebooks, etc.
They have prices written in a notebook. When prices change
they must cross out old prices - that's messy. They want
a computer program that stores and displays prices.
Item names and prices are stored in PARALLEL ARRAYS.
***************************************************/
String[] items = {
"pencil", "pen", "sharpie", "notebook A", "notebook B",
"eraser", "tippex", "usb stick", "glue", "tape"
};
float[] prices = {
0.75f, 1.50f, 1.20f, 1.90f, 2.50f,
0.50f, 1.75f, 5.00f, 1.25f, 2.00f
};
public static void main(String[] args) {
String name = "";
do {
name = input("Type the name of an item");
float price;
price = getPrice(name);
if (price > 0) {
output(name + " = " + price);
} else {
output(name + " was not found");
}
} while (name.length() > 0); // type nothing to quit
}
float getPrice(String name) // search for name, return price
{ // the price is not case-sensitive
for (int x = 0; x < prices.length; x = x + 1) {
if (items[x].equalsIgnoreCase(name)) // not cases-sensitive
{
return prices[x];
}
}
return 0.00f;
}
static public String input(String prompt) {
return javax.swing.JOptionPane.showInputDialog(null, prompt);
}
static public void output(String message) {
javax.swing.JOptionPane.showMessageDialog(null, message);
}
}
So I run this code and I get the following error:
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
- non-static method getPrice(java.lang.String) cannot be referenced from a static context at BookStore.main(BookStore.java:26)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
Which is confusing because I tried to just tack on the 'static' modifier in front of getPrice() but that creates a whole host of other errors. Maybe one of you guys can assist me..? Any help is appreciated.