😎
Introduction to Programming with Java
  • Introduction to Programming with Java
  • Session 1
    • Assignment1.java
  • Session 2
    • Source Code
    • Activities
    • Assignment
  • Session 3
    • Source Code
    • Activities
    • Assignment
  • Session 4
    • Source Code
    • Activities
  • Session 5
    • Source Code
    • Project
  • Session 6
    • Source Code
  • Session 7
    • Source Code
  • Session 8
    • Source Code
  • Session 9
    • Source Code
  • Session 10
    • Source Code
Powered by GitBook
On this page
  • Session7.java
  • ShoppingList.java
  • TravelPlanner.java
  1. Session 7

Source Code

Session7.java

import java.util.*;

public class Session7 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String myName = ""; // Add your name
        System.out.println("Session 7 - " + myName);

        // menu variables
        boolean done = false;
        String choice;

        do {
            // Menu
            System.out.println("Menu");
            System.out.println("E1 - Example 1");
            // optionally, add new menu items

            System.out.println("Q - Quit");
            System.out.print("Choice: ");
            choice = in.nextLine();

            switch (choice) {
                case "E1":
                    System.out.println("Example 1");
                    
                    break;
                case "E2":
                    System.out.println("Example 2");
                    
                    break;
                case "E3":
                    System.out.println("Example 3");
                    
                    break;
                case "E4":
                    System.out.println("Example 4");
                    
                    break;
                case "E5":
                    System.out.println("Example 5");
                    
                    break;
                // quit and default cases
                case "Q":
                    System.out.println("Quit");
                    done = true;
                    break;
                default:
                    System.out.println("Invalid Choice");
                    break;
            }
        } while (!done);
    }

    // example1 method definition
    public static void example1() {
        // ArrayList of friends
        

        // add some friends
        

        // print first element
        

        // add Devin
        

        // traditional for loop
        

        // enhanced for loop
        

    }

    // example2 method definition
    public static void example2() {
        // ArrayList of doubles
        

        // add elements
        

        // enhanced for loop
        

        // Using Collections class
        

        // max and min
        

        // sort
        

    }

    // example3 method definition
    public static void example3() {
        // ArrayList of Numbers
        

        // while loop
        

        // while loop and deleting
        

        // print the ArrayList
        
    }

    // example4 method definition
    public static void example4() {
        // Create and initialize the parallel ArrayLists
        

        // Print the parallel data
        System.out.println("Name and Age Pairs:");
        
    }

    // example5 method definition
    public static void example5() {
        // Create three parallel ArrayLists
        

        // Add elements to the ArrayLists
        

        // Print the parallel data
 

    }
}

ShoppingList.java

import java.util.*;

public class ShoppingList {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<String> shoppingList = new ArrayList<>();
        
        while (true) {
            System.out.println("Menu:");
            System.out.println("1. Add a new item");
            System.out.println("2. Remove an item");
            System.out.println("3. List all items");
            System.out.println("4. Exit");
            System.out.print("Choose an option: ");
            int choice = in.nextInt();
            in.nextLine(); // scanner bug
            
            switch (choice) {
                case 1:
                    System.out.print("Enter item name: ");

                    break;
                case 2:
                    System.out.print("Enter item name to remove: ");

                    break;
                case 3:
                    System.out.println("Shopping List:");

                    break;
                case 4:
                    System.out.println("Exiting...");
                    return;
                default:
                    System.out.println("Invalid option. Try again.");
            }
        }
    }
}

TravelPlanner.java

import java.util.*;

public class TravelPlanner {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<String> cities = new ArrayList<>();
        ArrayList<String> countries = new ArrayList<>();
        ArrayList<Integer> days = new ArrayList<>();
        
        while (true) {
            System.out.println("Menu:");
            System.out.println("1. Add a new destination");
            System.out.println("2. Remove a destination by city name");
            System.out.println("3. List all destinations");
            System.out.println("4. Find the total number of days for the trip");
            System.out.println("5. Exit");
            System.out.print("Choose an option: ");
            int choice = in.nextInt();
            in.nextLine(); // consume newline
            
            switch (choice) {
                case 1:
                    // Add a new destination
                    System.out.print("Enter city: ");

                    break;
                case 2:
                    // Remove a destination by city name
                    System.out.print("Enter city name to remove: ");

                    break;
                case 3:
                    // List all destinations
                    System.out.println("Travel Itinerary:");

                    break;
                case 4:
                    // Find the total number of days for the trip
                    
                    break;
                case 5:
                    // Exit the program
                    System.out.println("Exiting...");
                    return;
                default:
                    System.out.println("Invalid option. Try again.");
            }
        }
    }
}
PreviousSource CodeNextSource Code

Last updated 10 months ago