import java.util.HashMap;
import java.util.Scanner;
public class OrderingSystem {
public static void main(String[] args) {
HashMap<String, Double> menu = new HashMap<>();
menu.put("Item1", 10.99);
menu.put("Item2", 8.50);
menu.put("Item3", 6.25);
System.out.println("Welcome to the Ordering System!");
System.out.println("Here are the available items:");
for (String item : menu.keySet()) {
System.out.println(item + " - $" + menu.get(item));
}
}
}
menu
with three items and their prices. We then display the menu items along with their respective prices using a for-each loop.import java.util.HashMap;
import java.util.Scanner;
public class OrderingSystem {
public static void main(String[] args) {
HashMap<String, Double> menu = new HashMap<>();
menu.put("Item1", 10.99);
menu.put("Item2", 8.50);
menu.put("Item3", 6.25);
System.out.println("Welcome to the Ordering System!");
System.out.println("Here are the available items:");
for (String item : menu.keySet()) {
System.out.println(item + " - $" + menu.get(item));
}
Scanner scanner = new Scanner(System.in);
HashMap<String, Integer> order = new HashMap<>();
while (true) {
System.out.print("Enter an item to order (or 'done' to finish): ");
String item = scanner.nextLine();
if (item.equals("done")) {
break;
}
if (!menu.containsKey(item)) {
System.out.println("Invalid item!");
continue;
}
System.out.print("Enter the quantity to order: ");
int quantity = scanner.nextInt();
scanner.nextLine();
order.put(item, quantity);
System.out.println("Added " + quantity + " " + item + "(s) to the order.");
}
double total = 0.0;
for (String item : order.keySet()) {
double price = menu.get(item);
int quantity = order.get(item);
double itemTotal = price * quantity;
total += itemTotal;
System.out.println(quantity + " " + item + "(s) - $" + price + " each - Total: $" + itemTotal);
}
System.out.println("Total: $" + total);
}
}
order
to store the items that the user wants to order along with their quantity. We use a while loop to allow the user to add multiple items to the order. If the user enters "done", the loop breaks and we move on to calculating the total amount ordered.order
HashMap and calculate the total amount for each item. We add up all the item totals to get the total amount ordered. We also display the quantity, price, and item total for each item.import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;
public class OrderingSystem {
public static void main(String[] args) {
HashMap<String, Double> menu = new HashMap<>();
menu.put("Item1", 10.99);
menu.put("Item2", 8.50);
menu.put("Item3", 6.25);
System.out.println("Welcome to the Ordering System!");
System.out.println("Here are the available items:");
for (String item : menu.keySet()) {
System.out.println(item + " - $" + menu.get(item));
}
Scanner scanner = new Scanner(System.in);
HashMap<String, Integer> order = new HashMap<>();
while (true) {
System.out.print("Enter an item to order (or 'done' to finish): ");
String item = scanner.nextLine();
if (item.equals("done")) {
break;
}
if (!menu.containsKey(item)) {
System.out.println("Invalid item!");
continue;
}
System.out.print("Enter the quantity to order: ");
int quantity = scanner.nextInt();
scanner.nextLine();
order.put(item, quantity);
System.out.println("Added " + quantity + " " + item + "(s) to the order.");
}
double total = 0.0;
for (String item : order.keySet()) {
double price = menu.get(item);
int quantity = order.get(item);
double itemTotal = price * quantity;
total += itemTotal;
System.out.println(quantity + " " + item + "(s) - $" + price + " each - Total: $" + itemTotal);
}
System.out.println("Total: $" + total);
System.out.print("Enter payment amount: ");
double payment = scanner.nextDouble();
scanner.nextLine();
if (payment < total) {
System.out.println("Not enough payment!");
return;
}
double change = payment - total;
System.out.println("Change: $" + change);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datetime = format.format(new Date());
try {
FileWriter writer = new FileWriter("Receipt.txt");
writer.write("Order Date/Time: " + datetime + "\n\n");
writer.write("Item\tQuantity\tPrice\tTotal\n");
for (String item : order.keySet()) {
int quantity = order.get(item);
double price = menu.get(item);
double itemTotal = price * quantity;
writer.write(item + "\t" + quantity + "\t" + price + "\t" + itemTotal + "\n");
}
writer.write("\nTotal: $" + total + "\n");
writer.write("Payment: $" + payment + "\n");
writer.write("Change: $" + change + "\n");
writer.close();
System.out.println("Receipt saved to file!");
} catch (IOException e) {
System.out.println("Error writing to file!");
e.printStackTrace();
}
}
}
can u change hashmap?Sure, I'd be happy to help you out with that!
To start with, we can create a Java program that will display a menu with available items and their prices. We can use a HashMap to store the menu items and their prices. Here's some sample code to get you started:
Code:import java.util.HashMap; import java.util.Scanner; public class OrderingSystem { public static void main(String[] args) { HashMap<String, Double> menu = new HashMap<>(); menu.put("Item1", 10.99); menu.put("Item2", 8.50); menu.put("Item3", 6.25); System.out.println("Welcome to the Ordering System!"); System.out.println("Here are the available items:"); for (String item : menu.keySet()) { System.out.println(item + " - $" + menu.get(item)); } } }
This code creates a HashMapmenu
with three items and their prices. We then display the menu items along with their respective prices using a for-each loop.
Next, we can modify the code to allow the user to select items and enter the quantity they want to order. We can use a Scanner to get input from the user. Here's an updated version of the code:
Code:import java.util.HashMap; import java.util.Scanner; public class OrderingSystem { public static void main(String[] args) { HashMap<String, Double> menu = new HashMap<>(); menu.put("Item1", 10.99); menu.put("Item2", 8.50); menu.put("Item3", 6.25); System.out.println("Welcome to the Ordering System!"); System.out.println("Here are the available items:"); for (String item : menu.keySet()) { System.out.println(item + " - $" + menu.get(item)); } Scanner scanner = new Scanner(System.in); HashMap<String, Integer> order = new HashMap<>(); while (true) { System.out.print("Enter an item to order (or 'done' to finish): "); String item = scanner.nextLine(); if (item.equals("done")) { break; } if (!menu.containsKey(item)) { System.out.println("Invalid item!"); continue; } System.out.print("Enter the quantity to order: "); int quantity = scanner.nextInt(); scanner.nextLine(); order.put(item, quantity); System.out.println("Added " + quantity + " " + item + "(s) to the order."); } double total = 0.0; for (String item : order.keySet()) { double price = menu.get(item); int quantity = order.get(item); double itemTotal = price * quantity; total += itemTotal; System.out.println(quantity + " " + item + "(s) - $" + price + " each - Total: $" + itemTotal); } System.out.println("Total: $" + total); } }
This code adds a Scanner to get input from the user. We also create a HashMaporder
to store the items that the user wants to order along with their quantity. We use a while loop to allow the user to add multiple items to the order. If the user enters "done", the loop breaks and we move on to calculating the total amount ordered.
We then iterate over theorder
HashMap and calculate the total amount for each item. We add up all the item totals to get the total amount ordered. We also display the quantity, price, and item total for each item.
Finally, we can add a payment function and write the transaction details to a file. Here's the complete code:
Code:import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Scanner; public class OrderingSystem { public static void main(String[] args) { HashMap<String, Double> menu = new HashMap<>(); menu.put("Item1", 10.99); menu.put("Item2", 8.50); menu.put("Item3", 6.25); System.out.println("Welcome to the Ordering System!"); System.out.println("Here are the available items:"); for (String item : menu.keySet()) { System.out.println(item + " - $" + menu.get(item)); } Scanner scanner = new Scanner(System.in); HashMap<String, Integer> order = new HashMap<>(); while (true) { System.out.print("Enter an item to order (or 'done' to finish): "); String item = scanner.nextLine(); if (item.equals("done")) { break; } if (!menu.containsKey(item)) { System.out.println("Invalid item!"); continue; } System.out.print("Enter the quantity to order: "); int quantity = scanner.nextInt(); scanner.nextLine(); order.put(item, quantity); System.out.println("Added " + quantity + " " + item + "(s) to the order."); } double total = 0.0; for (String item : order.keySet()) { double price = menu.get(item); int quantity = order.get(item); double itemTotal = price * quantity; total += itemTotal; System.out.println(quantity + " " + item + "(s) - $" + price + " each - Total: $" + itemTotal); } System.out.println("Total: $" + total); System.out.print("Enter payment amount: "); double payment = scanner.nextDouble(); scanner.nextLine(); if (payment < total) { System.out.println("Not enough payment!"); return; } double change = payment - total; System.out.println("Change: $" + change); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String datetime = format.format(new Date()); try { FileWriter writer = new FileWriter("Receipt.txt"); writer.write("Order Date/Time: " + datetime + "\n\n"); writer.write("Item\tQuantity\tPrice\tTotal\n"); for (String item : order.keySet()) { int quantity = order.get(item); double price = menu.get(item); double itemTotal = price * quantity; writer.write(item + "\t" + quantity + "\t" + price + "\t" + itemTotal + "\n"); } writer.write("\nTotal: $" + total + "\n"); writer.write("Payment: $" + payment + "\n"); writer.write("Change: $" + change + "\n"); writer.close(); System.out.println("Receipt saved to file!"); } catch (IOException e) { System.out.println("Error writing to file!"); e.printStackTrace(); } } }
This code prompts the user to enter the payment amount and calculates the change. We then format the current date and time using a SimpleDateFormat and write the transaction details to a file named "Receipt.txt". We use a FileWriter to write to the file, and catch any IOExceptions that may occur.
I hope this helps! Let me know if you have any questions or if there's anything else I can do for you.