Activities
S10Problem1.java
import java.util.*;
public class S10Problem1 {
public static void main(String[] args) {
System.out.println("Problem 1");
Scanner in = new Scanner(System.in);
}
}
S10Problem2.java
import java.util.*;
public class S10Problem2 {
public static void main(String[] args) {
System.out.println("Problem 2");
Scanner in = new Scanner(System.in);
}
}
GroceryStoreManager.java
import java.util.*;
public class GroceryStoreManager {
// Parallel ArrayLists
private static ArrayList<String> itemNames = new ArrayList<>();
private static ArrayList<Double> prices = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// Add some sample data
addSampleData();
int choice;
do {
displayMenu();
choice = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (choice) {
case 1:
addNewItem();
break;
case 2:
removeItem();
break;
case 3:
updatePrice();
break;
case 4:
displayAllItems();
break;
case 5:
findMostExpensive();
break;
case 6:
findLeastExpensive();
break;
case 7:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 7);
}
public static void displayMenu() {
System.out.println("\n=== Grocery Store Inventory ===");
System.out.println("1. Add new item");
System.out.println("2. Remove item");
System.out.println("3. Update price");
System.out.println("4. Display all items");
System.out.println("5. Find most expensive item");
System.out.println("6. Find least expensive item");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
}
public static void addSampleData() {
itemNames.add("Apples");
prices.add(2.99);
itemNames.add("Bread");
prices.add(2.50);
itemNames.add("Milk");
prices.add(4.99);
itemNames.add("Bananas");
prices.add(2.99);
}
public static void addNewItem() {
System.out.print("Enter item name: ");
String name = scanner.nextLine();
System.out.print("Enter price: $");
double price = scanner.nextDouble();
// TODO: Add the item name and price to their respective ArrayLists
System.out.println("Item added successfully!");
}
public static void removeItem() {
displayAllItems();
if (itemNames.size() == 0) {
System.out.println("No items to remove!");
return;
}
System.out.print("Enter the number of the item to remove: ");
int index = scanner.nextInt() - 1;
// TODO: Check if index is valid (between 0 and size-1)
// TODO: Remove item from both ArrayLists at the same index
System.out.println("Item removed successfully!");
}
public static void updatePrice() {
displayAllItems();
if (itemNames.size() == 0) {
System.out.println("No items to update!");
return;
}
System.out.print("Enter the number of the item to update: ");
int index = scanner.nextInt() - 1;
// TODO: Check if index is valid
// TODO: Ask for new price and update the prices ArrayList using .set()
System.out.println("Price updated successfully!");
}
public static void displayAllItems() {
System.out.println("\n=== Current Inventory ===");
if (itemNames.size() == 0) {
System.out.println("No items in inventory!");
return;
}
System.out.println("Item\tPrice");
for (int i = 0; i < itemNames.size(); i++) {
System.out.println(itemNames.get(i) + "\t$" + prices.get(i));
}
}
public static void findMostExpensive() {
if (prices.size() == 0) {
System.out.println("No items in inventory!");
return;
}
// TODO: Find the index of the item with the highest price
// Display the item name and price
// Hint: Use a loop to compare prices and track the highest
}
public static void findLeastExpensive() {
if (prices.size() == 0) {
System.out.println("No items in inventory!");
return;
}
// TODO: Find the index of the item with the lowest price
// Display the item name and price
// Hint: Use a loop to compare prices and track the lowest
}
}
GroceryStoreManagerV2.java
import java.util.*;
public class GroceryStoreManagerV2 {
// HashMap instead of parallel ArrayLists
private static HashMap<String, Double> inventory = new HashMap<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// Add some sample data
addSampleData();
int choice;
do {
displayMenu();
choice = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (choice) {
case 1:
addNewItem();
break;
case 2:
removeItem();
break;
case 3:
updatePrice();
break;
case 4:
displayAllItems();
break;
case 5:
findMostExpensive();
break;
case 6:
findLeastExpensive();
break;
case 7:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 7);
}
public static void displayMenu() {
System.out.println("\n=== Grocery Store Inventory ===");
System.out.println("1. Add new item");
System.out.println("2. Remove item");
System.out.println("3. Update price");
System.out.println("4. Display all items");
System.out.println("5. Find most expensive item");
System.out.println("6. Find least expensive item");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
}
public static void addSampleData() {
inventory.put("Apples", 2.99);
inventory.put("Bread", 2.50);
inventory.put("Milk", 4.99);
inventory.put("Bananas", 2.99);
}
public static void addNewItem() {
System.out.print("Enter item name: ");
String name = scanner.nextLine();
System.out.print("Enter price: $");
double price = scanner.nextDouble();
// TODO: Add the item name and price to the HashMap
// Example: inventory.put(name, price);
System.out.println("Item added successfully!");
}
public static void removeItem() {
displayAllItems();
if (inventory.isEmpty()) {
System.out.println("No items to remove!");
return;
}
System.out.print("Enter the item name to remove: ");
String name = scanner.nextLine();
// TODO: Check if the item exists and remove it using inventory.remove(name)
System.out.println("Item removed successfully!");
}
public static void updatePrice() {
displayAllItems();
if (inventory.isEmpty()) {
System.out.println("No items to update!");
return;
}
System.out.print("Enter the item name to update: ");
String name = scanner.nextLine();
// TODO: Check if item exists
// TODO: Ask for new price and update using inventory.put(name, newPrice)
System.out.println("Price updated successfully!");
}
public static void displayAllItems() {
System.out.println("\n=== Current Inventory ===");
if (inventory.isEmpty()) {
System.out.println("No items in inventory!");
return;
}
System.out.println("Item\tPrice");
for (String name : inventory.keySet()) {
System.out.println(name + "\t$" + inventory.get(name));
}
}
public static void findMostExpensive() {
if (inventory.isEmpty()) {
System.out.println("No items in inventory!");
return;
}
// TODO: Find the item with the highest price
// Hint: Use a loop through inventory.entrySet() to compare values
// Example: if (entry.getValue() > max), then track max and key
// TODO: Display the item name and price
}
public static void findLeastExpensive() {
if (inventory.isEmpty()) {
System.out.println("No items in inventory!");
return;
}
// TODO: Find the item with the lowest price
// Hint: Use a loop through inventory.entrySet() to compare values
// TODO: Display the item name and price
}
}
PlaylistManager.java
import java.io.*;
import java.util.*;
public class PlaylistManager {
private static HashMap<String, String> playlist = new HashMap<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
loadFromCSV("playlist.csv");
int choice;
do {
displayMenu();
choice = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (choice) {
case 1:
addSong();
break;
case 2:
removeSong();
break;
case 3:
randomSong();
break;
case 4:
displayAllSongs();
break;
case 5:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice.");
}
} while (choice != 5);
}
public static void loadFromCSV(String filename) {
try {
File file = new File(filename);
Scanner fileScanner = new Scanner(file);
// Skip header
if (fileScanner.hasNextLine()) {
fileScanner.nextLine();
}
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
String[] parts = line.split(",");
if (parts.length == 2) {
String artist = parts[0].trim();
String title = parts[1].trim();
playlist.put(title, artist);
}
}
fileScanner.close();
System.out.println("Playlist loaded.");
} catch (FileNotFoundException e) {
System.out.println("CSV file not found.");
}
}
public static void displayMenu() {
System.out.println("\nš¶ Playlist Manager š¶");
System.out.println("1. Add a new song");
System.out.println("2. Remove a song");
System.out.println("3. Search for a song");
System.out.println("4. Display entire playlist");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
}
public static void addSong() {
System.out.print("Enter song title: ");
String title = scanner.nextLine();
System.out.print("Enter artist name: ");
String artist = scanner.nextLine();
// TODO
}
public static void removeSong() {
System.out.print("Enter song title to remove: ");
String title = scanner.nextLine();
// TODO
}
public static void randomSong() {
System.out.print("Here's a random song!");
// TODO
}
public static void displayAllSongs() {
if (playlist.isEmpty()) {
System.out.println("Your playlist is empty.");
return;
}
System.out.println("\nš§ Your Playlist:");
for (String title : playlist.keySet()) {
System.out.println(title + " - " + playlist.get(title));
}
}
}
Last updated