Develop a program to read in the market basket data from a text file with the fo
ID: 3724935 • Letter: D
Question
Develop a program to read in the market basket data from a text file with the following format and identify the unique items printed on the screen.Continue your program to count the frequency each unique item. Please note that comments are part of the file but not the data. You are NOT allowed to modify the file format. You are free to chose the language. There will be more phases. So, make sure that you come up with a nice, well designed modular approach. You have to submit the code, the content of the data file, and the screen shot of the output as ONE file together.
-- market.data ---
6 //# of transactions
1, 3, gum, bread, napkin //transaction id, # of items purchased, item1, item3,...
2, 1, gum
3, 2, fruit, bread
4, 5, milk, bread, napkin, fork, juice
5, 3, juice, napkin, milk
6, 2, bread, spoon
Output for your program would look like this:
There are 8 unique items: gum, bread, napkin, fruit, milk, fork, juice, spoon.
Item Name Frequency
gum ............................... 3
bread ............................11
napkin ............................7
Explanation / Answer
Hi... I have written the java program for the above.
Market.java
import java.util.ArrayList;
public class Market {
private int transactionId;
private int itemsPurchased;
private ArrayList<String> items;
public int getTransactionId() {
return transactionId;
}
public void setTransactionId(int transactionId) {
this.transactionId = transactionId;
}
public int getItemsPurchased() {
return itemsPurchased;
}
public void setItemsPurchased(int itemsPurchased) {
this.itemsPurchased = itemsPurchased;
}
public ArrayList<String> getItems() {
return items;
}
public void setItems(ArrayList<String> items) {
this.items = items;
}
public Market(int transactionId, int itemsPurchased, ArrayList<String> items) {
this.transactionId = transactionId;
this.itemsPurchased = itemsPurchased;
this.items = items;
}
}
MarketDriver.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class MarketDriver {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Market m[]=null;
BufferedReader br = new BufferedReader(new FileReader("c:/Santhosh/abc.txt"));
String line="";
int totalTransaction = 0;
int k=0;
while((line=br.readLine())!=null){
//System.out.println(line);
if(totalTransaction==0){
totalTransaction = Integer.parseInt(line.trim());
m = new Market[totalTransaction];
}else{
String vals[] = line.split(",");
int tid = Integer.parseInt(vals[0].trim());
int itemsPurchased = Integer.parseInt(vals[1].trim());
ArrayList<String> ab = new ArrayList<String>();
for(int i=2;i<vals.length;i++){
ab.add(vals[i].trim());
}
m[k] = new Market(tid, itemsPurchased, ab);
k++;
}
}
Set<String> set1 = new HashSet<String>();
ArrayList<String> total = new ArrayList<String>();
for(int i=0;i<m.length;i++){
ArrayList<String> ab1 = m[i].getItems();
total.addAll(ab1);
for(String s:ab1){
set1.add(s);
}
}
int ab[] = new int[set1.size()];
int j=0;
System.out.println("Unique Items are:");
for (String s : set1) {
System.out.print(s+" ");
//System.out.println(Collections.frequency(total, s));
ab[j] = Collections.frequency(total, s);
j++;
}
System.out.println();
System.out.println("Item Name Frequency");
j=0;
for(String s : set1){
System.out.println(s+" "+ab[j]);
j++;
}
}
}
abc.txt
6
1, 3, gum, bread, napkin
2, 1, gum
3, 2, fruit, bread
4, 5, milk, bread, napkin, fork, juice
5, 3, juice, napkin, milk
6, 2, bread, spoon
Output:
Unique Items are:
bread fork napkin fruit milk spoon juice gum
Item Name Frequency
bread 4
fork 1
napkin 3
fruit 1
milk 2
spoon 1
juice 2
gum 2
Please test the code and let me know any issues. Thank you. All the best.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.