Skip to content

Instantly share code, notes, and snippets.

@neutronscott
Last active January 30, 2024 04:16
Show Gist options
  • Save neutronscott/a0e3c96cd317f56d0e9649e69a0a6075 to your computer and use it in GitHub Desktop.
Save neutronscott/a0e3c96cd317f56d0e9649e69a0a6075 to your computer and use it in GitHub Desktop.
/* menu.java
* learning the java OO way
* scott nicholas <m@san.aq> jan 29 2024
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Menu
{
/* from <https://yohanan.org/steve/projects/java-code-conventions>
* static variables: public, protected, package level, private
* instance variables: ditto
* constructors
* methods: should be grouped by functionality
* nested classes and interfaces
*/
/* instance variables */
private List<Item> items = new ArrayList<>();
private String name = "Menu";
private int maxNameLength = 0;
private int maxPriceLength = 0;
/* constructors */
public Menu(String name, Item ... items)
{
this.name = name;
for (Item m : items)
{
this.items.add(m);
if (m.name().length() > maxNameLength)
{
maxNameLength = m.name().length();
}
if (m.getPrice().length() > maxPriceLength)
{
maxPriceLength = m.getPrice().length();
}
}
}
/* methods */
public String getName()
{
return this.name;
}
public List<Item> getItems()
{
return this.items;
}
public Item findItem(char opt)
{
for (Item m : this.getItems())
{
if (m.opt() == opt)
{
return m;
}
}
return null;
}
public void displayMenu()
{
System.out.println(this.getName());
for (Item m : this.getItems())
{
System.out.printf("%s) %-" + this.maxNameLength + "s %"
+ this.maxPriceLength + "s\n",
m.opt(), m.name(), m.getPrice());
}
System.out.printf("Q) Quit\n");
}
public Item displayAndSelect(Scanner console, String prompt)
{
char input;
Item item;
while (true)
{
this.displayMenu();
System.out.printf(prompt);
try
{
input = console.nextLine().toUpperCase().charAt(0);
}
catch (Exception e)
{
continue; // probably hit enter without any character
}
// hmm
if (input == 'Q')
{
return null;
}
item = this.findItem(input);
if (item == null)
{
System.out.println("Invalid selection.");
}
else
{
return item;
}
}
}
/* nested classes */
record Item(char opt, String name, double price)
{
public String getPrice()
{
return String.format("$%.2f", this.price());
}
}
} // Menu
public class menu
{
static final Menu mainMenu = new Menu(
"Main Menu",
new Menu.Item('A', "item no. one", 5.25),
new Menu.Item('B', "item2", 16.99));
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
double total = 0.0;
while (true)
{
// find what was selected
Menu.Item selection = mainMenu.displayAndSelect(console,
"Selection: ");
if (selection == null) {
break; // XXX null means quit
}
System.out.printf("You selected: %s for $%.2f/ea\n",
selection.name(), selection.price());
total += selection.price();
}
System.out.printf("Total: %.2f\n", total);
console.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment