Search code examples
javatostringgetter

Java creating objects for every record with unique ID


I'm working on this project where I am supposed to parse the file which contains data of fictional companies and other relevant details, create an object for each record and store objects into a suitable collection. I have made a start but now I'm stuck and reaching out for help.

I've managed to read the file from local storage and output it to the console but oddly it outputs the details six times, matching the number of variables I have. Is there an easy fix for it?

Also, what technology would I use for generating ID for each of the records that would rise incrementally with each record? e.g. 1, 2 ... 11, 12, 13 and so on

I've read about getter method to map to the data members and toString() methods but can't seem to comprehend it enough to implement it into my program. Are there any similar projects that would help me with understanding how those technologies work and would help with mine?

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;

public class ApplicationRunner {
static void menu() {  
    try {
        char quit = 'n';
        String menuInput;
        int menuChoice = 0;

        Scanner scan = new Scanner(System.in);

        while (quit != 'y') {
            System.out.println(
              "\n List traders.......1"
            + "\n Select trader......2"
            + "\n Search locations...3"
            + "\n Search services....4"
            + "\n Exit...............0"
            + "\n\n Enter choice:>");

            menuChoice = scan.nextInt();

        switch (menuChoice) {
            case 1:
                listTraders();
                break;
            case 2:
                selectTrader();
                break;
            case 3:
                searchLocation();
                break;
            case 4:
                searchServices();
                break;
            case 0:
                System.out.println("Exiting...");
                System.exit(0);
                break;
            default:
                System.out.println("Invalid menu selection.");
        }

        System.out.println("Quit? y/n");
        menuInput = scan.next().toLowerCase();
        quit = menuInput.charAt(0);
        }   
    }

    catch (Exception inputError)
    {
        System.out.println("\nInvalid input value. Valid values 0-4.");
        menu();
    }  
}

static void listTraders() {
    String dataFile = System.getProperty("user.dir") + File.separator + "traders.txt";

    try {
        BufferedReader br = new BufferedReader(new FileReader(dataFile));

        String line = null;        

        while ((line = br.readLine()) != null) {
            String[] details = line.split(":");

            String companyName = details[0];
            String location = details[1];
            String services = details[2];
            String description = details[5];
            int numEmployees = Integer.parseInt(details[3]);
            double dailyRate = Double.parseDouble(details[4]);

            for (String printDetails : details) {

                System.out.println(companyName + "\t" + location + "\t" + services +
                        "\t" + description + "\t" + numEmployees + "\t" + dailyRate);
            }
        }

        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

static void selectTrader() {

}

static void searchLocation() {

}

static void searchServices() {

}

public static void main(String[] args) {
    menu();
}

}

The traders file that's being used in this program - https://gist.github.com/senotajs/0011c93460f0e9603a53858de15f8639


Solution

  • In the piece of code below, you are running the for loop for the length of details array. details array contains 6 variables which is why you see it printed 6 times. To fix it, remove the loop and just print the variables.

        for (String printDetails : details) {
    
            System.out.println(companyName + "\t" + location + "\t" + services +
                    "\t" + description + "\t" + numEmployees + "\t" + dailyRate);
        }