1 year ago

#380313

test-img

Onek

Java CSV printing relevant information in relation to User input

my current code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Menu {

private static final String DEFAULT_DATE_FORMAT = "MM/dd/yyyy";







public static void main(String[] args) {
    
    
    boolean exit = false;
    int userInputt;
    do {
        userInputt = printMenu();
        switch(userInputt) {
        case 1:
            inputLocation();
            break;
        case 2:
            inputType();
            break;
        case 3:
            inputRating();
            break;
        case 4: 
            System.out.println("Goodbye!");
            exit = true;
            break;
        default:
            System.out.println("Invalid Option.");
            break;
        }
        
            
        
    }
    /* an int varible that allows the storing of an integer */
    while(userInputt > 4);
    
}




/* an int varible that allows the storing of an integer */
public static int printMenu () {
    
    int selection;
    Scanner sc = new Scanner (System.in);
    System.out.println("Welcome to Melbnb!");
    System.out.println("------------------------------------------------");
    System.out.println("> Select from main menu");
    System.out.println("------------------------------------------------");
    System.out.println("    1) Search by location");
    System.out.println("    2) Browse by type of place");
    System.out.println("    3) Filter by rating");
    System.out.println("    4) Exit");
    
    System.out.print("Please Select: ");
    selection = sc.nextInt();
    return selection;

}

private static char inputLocation() {

        Scanner userInput = new Scanner(System.in);
        File file = new File("C:/Users/andys/OneDrive/Desktop/Melbnb (2).csv"); 

        String fileData = "";
        try (Scanner reader = new Scanner(file)) {
            // Read the header line so we don't deal with it again
            fileData = reader.nextLine();
            
    System.out.print("Please provide a location: ");
    String location1 = userInput.nextLine().trim();
    
    List<String> foundRecords = new ArrayList<>(); 
    boolean found = false;
    while (reader.hasNextLine()) {
        fileData = reader.nextLine().trim();
        // Skip blank lines (if any).
        if (fileData.isEmpty()) {
            continue;
        } String regex = ",";
        String[] lineParts = fileData.split(regex);
       
        found = (location1.isEmpty()  ||
                (lineParts[0].contains(location1) ||
                (location1.isEmpty() || 
                (lineParts[1].contains(location1)))));
        
        
        
        
        if (found) {
            foundRecords.add(fileData);
            found = false;
        }
    }
        
    // Display found records (if any)
    System.out.println(); 
    System.out.println("Found Records:");
    System.out.println("====================================");
    if (foundRecords.isEmpty()) {
        System.out.println("         No Records Found!"); 
    }
    else {
        for (String str : foundRecords) {
            System.out.println(str);
            
            
        }
    }
    System.out.println("====================================");
}
catch (FileNotFoundException ex) {

    
}

    
    
        
        
        
        char userInputt;
        do {
            userInputt = location();
            switch(userInputt) {
            case 1:
                System.out.println("------------------------------------------------");
                break;
            case 2:
                inputType();
                break;
            case 3:
                inputRating();
                break;
            case 4: 
                printMenu();
                break;
            default:
                break;
            }
        }
        while(userInputt > 4);
        return userInputt;
        
        
    }{
    

    
    
    
}

private static char location() {
    // TODO Auto-generated method stub
    return 0;
}


private static void inputType() {
    System.out.println("------------------------------------------------");
}
    
private static void inputRating() { 
    
}
    

/* ---------------------------------- Locations ---------------------------------- */

private static void Southbank() {   

int southbankPrice = 42;
int southbankClean = 11;
int southbankService = 10;
double southbankDiscount = 0.05; 

Scanner checkIn = new Scanner (System.in);

System.out.println("------------------------------------------------");
System.out.println("> Provide dates");
System.out.println("------------------------------------------------");
System.out.print("Please provide check-in date (dd/mm/yyyy): ");
Date inDate = getInputDate(checkIn);
System.out.print("Please provide checkout date (dd/mm/yyyy): ");
Date outDate = getInputDate(checkIn);
System.out.println("------------------------------------------------");

Scanner pInfo = new Scanner (System.in);

System.out.println("> Provide personal information");
System.out.println("------------------------------------------------");

System.out.print("Please provide your given name: ");
String southName = pInfo.nextLine();

System.out.print("Please provide your surname: ");
String southLast = pInfo.nextLine();

System.out.print("Please provide your email address: ");
String southEmail = pInfo.nextLine();

System.out.print("Please provide number of guests: ");
String southGuest = pInfo.nextLine();

System.out.print("Confirm and pay (Y/N): ");
String southConfirm = pInfo.nextLine();

System.out.println("------------------------------------------------");
System.out.println("> Show property details");
System.out.println("------------------------------------------------");

System.out.printf("%-25s%-20s\n", "Property:","Private room in the heart of Southbank hosted by " + southName);
System.out.printf("%-25s%-20s\n", "Type of place:","Private room");
System.out.printf("%-25s%-20s\n", "Location:","Southbank");
System.out.printf("%-25s%-20s\n", "Rating:","4.5");
System.out.printf("%-25s%-20s\n", "Description:","The apartment is situated in the heart of Southbank with an easy access to shops and cafes. It has a warm and spacious living room with an amazing view of the gardens.");
System.out.printf("%-25s%-20s\n", "Number of guests: ", southGuest);
System.out.printf("%-25s%-20s\n", "Price:", "asd");
System.out.printf("%-25s%-20s\n", "Discounted price:","asd");
System.out.printf("%-25s%-20s\n", "Service fee:","asd");
System.out.printf("%-25s%-20d\n", "Cleaning fee: ", southbankClean );
System.out.printf("%-25s%-20s\n", "Total:","asd");

}

/* ---------------------------------- Date Validation ---------------------------------- */

private static Date getInputDate(final Scanner checkIn) {
try {
    return new SimpleDateFormat(DEFAULT_DATE_FORMAT).parse(checkIn.nextLine());
} catch (ParseException ex) {
    System.out.println("Invalid Date. Please Try again!");
    Southbank();
}

return null;
}



}

The first image is my Desired output The second is my current output and the third image is my CSV

My program has a feature which will allow the user to a hotel apartment by inputting a specific location for example 'South', as shown in the screenshots of my current output once the term 'South' gets inputted to prints out all the columns for any fields relating to the search term in the CSV file, However i only want the first column to be printed out. How can i achieve this output? What can i do to achieve my desired output?

This is my desired output

This is my current output

This is my CSV

I have tried removing the 'for' statement in the following code and replacing the print element with foundrecords.get(0) 

java

csv

java.util.scanner

0 Answers

Your Answer

Accepted video resources