Home > Software design >  How do I print an element from an array by asking for user input? (matching input with elements in t
How do I print an element from an array by asking for user input? (matching input with elements in t

Time:07-13

Case 3 and Case 4 - How do I print an element from an array by asking for user input? (matching input with elements in the array)

Also how do I grab user input as an integer range (e.g 1 - 4) and display it in order? (e.g element 1, element 2, element 3, element 4)


import java.util.Scanner;
import java.util.ArrayList;

// ----- OBJECT TYPE 1 ----- //
public class project {
    
    // ----- OBJECT 1 [ main method ] ----- //
    public static void main(String[]args) {
        
        Scanner input = new Scanner(System.in);
        
        ArrayList<CustomerData> companyLog = new ArrayList<>();
        
        
        boolean cycleMenu = true;
        while (cycleMenu) {
            System.out.println("THE MENU");
            System.out.println("------------- \n");
            System.out.println("1: Add New Customers");
            System.out.println("2: Display All Customers");
            System.out.println("3: Retrieve Specific Customer Data");
            System.out.println("4: Retrieve Customer Total Sales Range");
            System.out.println("9: Exit");
            
            //Add new customer - prompts the user for customer data: customer name, customer id, and total sales
            int responses = input.nextInt();
            switch (responses) {
                case 1: {
                    //Load customers’ data - Prompt user for the number of customers to be loaded
                    System.out.println("What is the number of customers?: ");
                    int cycle = input.nextInt();
                    for (int i = 0; i < cycle; i  ) {
                        companyLog.add(new CustomerData());
                    }
                } break;
                case 2: {
                    //re = results | Display ALL customers
                    for (CustomerData re : companyLog) {
                        re.printData();
                    }
                } break;
                //Retrieve specific customer's data:
                case 3: {
                    //prompts the user for the customer id
                    System.out.println("Enter the customer's ID: ");
                    int identify = input.nextInt();
                    
                    //match specific customer id with an id in the companyLog array
                    for (int i = 0; i < identify; i  ) {
                        //print the specific customer's data: customer id, customer name, and total sales
                         companyLog.get(identify);
                    }
                } break;
                //Retrieve customers with total sales based on the range:
                case 4: {
                    //prompts the user for the lowest and highest total sales
                    System.out.println("Enter a sales range?: ");
                    float range = input.nextFloat();
                    
                    //match lowest and highest sales to companyLog array
                    
                    
                    //print all customers with total sales in that range from companyLog array
                    
                    
                } break;
                case 9: {
                    //Exit
                    System.out.println("\n"   "You have exited the Java program. Goodbye! :)");
                    cycleMenu = false;
                } break;
                }
            }   
        }
    }


// ----- OBJECT TYPE 2 ----- //
class CustomerData {
    
    String names;
    float sales;
    int id;
    
    // ----- OBJECT 1 ----- //
    public CustomerData() {
        
        Scanner input = new Scanner(System.in);
        
        //Prompts for each customer's name
        System.out.println("\n What is this customers name?: \n");
        names = input.next();
        
        //Customer id (5 digit number)
        System.out.println("\n Please enter this customer's 5-digit id number.: \n");
        id = input.nextInt();
        
        //Total sales
        System.out.println("\n What is this customer's sales total?: \n");
        sales = input.nextFloat();
        
        this.names = names;
        this.id = id;
        this.sales = sales;
    }
    
    // ----- OBJECT 2 ----- //
    public void printData() {
        
        //Display all customers - displays each customer's data to the console, one customer per line
        System.out.println("Customer: "   this.names   "\n");
        System.out.println("ID: "   this.id   "\n");
        System.out.println("Sales Total: "   this.sales   "\n");
    }
}

CodePudding user response:

Case 3) You need an if statement against the ID

System.out.println("Enter the customer's ID: ");
int identify = input.nextInt();

for (int i = 0; i < companyLog.size(); i  ) {
     CompanyData cd = companyLog.get(i);
     // match specific customer id with an id in the companyLog array
     if (cd.id == identify) {
         // print the specific customer's data: customer id, customer name, and total sales
         cd.printData();
     }
}

Case 4) You can input a range as a string rather than a float 0-100, then split("-") the string, then cast each part to a float... Or you can input two floats for a low then a high.

The comments in the code say nothing about ordering the output.

//prompts the user for the lowest and highest total sales
System.out.println("Enter a sales range? (low): ");
float low = input.nextFloat();
System.out.println("Enter a sales range? (high): ");
float high = input.nextFloat();

for (int i = 0; i < companyLog.size(); i  ) {
     CompanyData cd = companyLog.get(i);
     if (cd.sales >= low && cd.sales < high) {
         // print the specific customer's data: customer id, customer name, and total sales
         cd.printData();
     }
}

CodePudding user response:

Maybe you can do some like that in the case 3:

case 3: {

    System.out.println("Enter the customer's ID: ");
    int identify = input.nextInt();
    companyLog.get(identify).printData();

    break;
}

I give you some advice

  1. The 'break' sentence must be into the case
  2. You don't need 'for' in this case because you know the client id
  •  Tags:  
  • java
  • Related