-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDealer.java
More file actions
125 lines (105 loc) · 4.08 KB
/
Copy pathDealer.java
File metadata and controls
125 lines (105 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.example.fxpartcw;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public class Dealer {
private String name;
private String contactNumber;
private String location;
private List<String> itemDetails;
public Dealer() {
itemDetails = new ArrayList<>();
}
public static void displayRandomDealerDetails() {
}
public String getName() {
return name;
}
public String getContactNumber() {
return contactNumber;
}
public String getLocation() {
return location;
}
public List<String> getItemDetails() {
return itemDetails;
}
public void setName(String name) {
this.name = name;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public void setLocation(String location) {
this.location = location;
}
// Method to read dealers and their item details from the text file.
public static List<Dealer> readDealersFromFile(String filePath) {
List<Dealer> dealersList = new ArrayList<>();
Dealer currentDealer = null;
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(", ");
if (data.length == 3) {
if (currentDealer != null) {
dealersList.add(currentDealer);
}
currentDealer = new Dealer();
currentDealer.setName(data[0]);
currentDealer.setContactNumber(data[1]);
currentDealer.setLocation(data[2]);
} else if (data.length == 5) {
currentDealer.getItemDetails().add(line);
}
}
if (currentDealer != null) {
dealersList.add(currentDealer);
}
} catch (IOException e) {
System.out.println("Error reading the file: " + e.getMessage());
}
return dealersList;
}
// Method to select a given number of random dealers from the list.
public static List<Dealer> selectRandomDealers(List<Dealer> dealersList, int numberOfDealers) {
if (dealersList.size() <= numberOfDealers) {
return dealersList;
}
List<Dealer> randomDealers = new ArrayList<>();
List<Integer> indexesUsed = new ArrayList<>();
Random random = new Random();
while (randomDealers.size() < numberOfDealers) {
int randomIndex = random.nextInt(dealersList.size());
if (!indexesUsed.contains(randomIndex)) {
randomDealers.add(dealersList.get(randomIndex));
indexesUsed.add(randomIndex);
}
}
return randomDealers;
}
// Method to display the details (name, contact number, location) of randomly selected dealers.
public static void displayDealersDetails(List<Dealer> selectedDealers) {
for (Dealer d : selectedDealers) {
System.out.println("Dealer: " + d.getName());
System.out.println("Contact Number: " + d.getContactNumber());
System.out.println("Location: " + d.getLocation());
}
}
// Method to display the names of randomly selected dealers and their item details.
public static ArrayList<HashMap<String, String>> displaySelectedDealersItemDetails(List<Dealer> selectedDealers) {
for (Dealer d : selectedDealers) {
System.out.println("Dealer Name: " + d.getName());
System.out.println("Item Details:");
for (String itemDetail : d.getItemDetails()) {
System.out.println(itemDetail);
}
System.out.println("====================");
}
return null;
}
}