Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io . BufferedReader; import java.io . FileReader; import java.io . IOException; public class Main { public static void main ( String [ ] args

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args){
Market m1= new Market("Minnesota",10000);
Market m2= new Market("Michigan",20000);
Market m3= new Market("North Dakota", 20000);
System.out.println("m1.compareTo(m2): "+ m1.compareTo(m2));
System.out.println("m3.compareTo(m2): "+ m3.compareTo(m2)+"
");
MarketSortedSet sort = new MarketSortedSet(20);
String filePath = askUserForFilePath();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))){
String line;
while ((line = br.readLine())!= null){
String[] parts = line.split(",");
int type = Integer.parseInt(parts[0]);
String stringValue = parts[1];
int intValue = Integer.parseInt(parts[2]);
switch (type){
case 1:
int zip = Integer.parseInt(parts[3]);
Zip itemA = new Zip(stringValue, intValue, zip);
sort.insert(itemA);
break;
case 2:
boolean TF = Boolean.parseBoolean(parts[3]);
TF itemB = new TF(stringValue, intValue, TF);
sort.insert(itemB);
break;
case 3:
Market item = new Market(stringValue, intValue);
sort.insert(item);
break;
default:
System.out.println("Invalid item type encountered: "+ type);
}
}
System.out.println("Items in sorted set:");
System.out.println(sort);
testIndexOf(sort);
testRemove(sort);
testGrab(sort);
testCategorySet(sort);
} catch (IOException e){
e.printStackTrace();
}
}
private static String askUserForFilePath(){
// This method should ideally prompt the user for the file path
// or accept it from command-line arguments.
return "spot.txt"; // For now, returning a fixed file path for testing
}
private static void testIndexOf(MarketSortedSet set){
System.out.println("Testing indexOf method:");
Market item = new Market("Minnesota",10000);
int index = set.indexOf(item);
if (index >=0){
System.out.println("Item found at index: "+ index);
} else {
System.out.println("Item not found.");
}
}
private static void testRemove(MarketSortedSet set){
System.out.println("Testing remove method:");
Market item = new Market("Minnesota",10000);
if (set.remove(item)){
System.out.println("Item removed successfully.");
} else {
System.out.println("Item not found or could not be removed.");
}
System.out.println("Updated set:");
System.out.println(set);
}
private static void testGrab(MarketSortedSet set){
System.out.println("Testing grab method:");
Market item = set.grab(0);
if (item != null){
System.out.println("Item at index 0: "+ item);
} else {
System.out.println("Set is empty or index out of bounds.");
}
}
private static void testCategorySet(MarketSortedSet set){
System.out.println("
Testing categorySet method:");
System.out.println("Category 1(Zip):");
MarketSortedSet category1= set.categorySet(1);
System.out.println(category1);
System.out.println("Category 2(TF):");
MarketSortedSet category2= set.categorySet(2);
System.out.println(category2);
System.out.println("Category 3(Items):");
MarketSortedSet category3= set.categorySet(3);
System.out.println(category3);
}
}
Code can't read my txtfile. plz help
1.your driver should read the data from a text file where each line in
the input file represents one item and the line includes the following values separated by
tab: (1) a number (1,2, or 3) to specify whether the line represents an ItemA, ItemB, or
Item object respectively, (2) string value, (3) integer value, and (4) an integer value for
ItemA lines or boolean value for ItemB lines. Assume the input file is formatted correctly
and you do not have to check for lines that are not in the given format. This is an example
input file:
1 aaa 1020
2 bbb 20 true
3 ccc 39
2 aaa 10 false
2- Create an input file of at least 20 lines with at least 5 lines for each item type
(Item/ItemA/ItemB). Include some duplicate lines to test that the add method does not
add duplicate items.
3- Write code to ask the user for the path of the data file, read the file name, open the file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions