How to create this program and makes its run with the giving the class PA4Driver
ID: 3604601 • Letter: H
Question
How to create this program and makes its run with the giving the class PA4Driver with the main method and the unsortedItems.txt.
Write a program that will read a file (unsortedItems.txt)with a number of items. Your program will then store each item that was read form the file to an Arrayof items. Now that you have all items stored in the Array you need to sort them based on the uid (unique id) and write them to another file (sortedItems.txt).
You will need to design a class named Item that contains:
name: name of the item (String)
uid: unique id of the item (int)
description: description of the itme (String)
price: price of the item (double)
mutator and accessor of name, uid, description and price.
Overloaded Constructor that sets all the instance variables
toString() method returing details on an item in the following format:
uid + “|” + name + “|” + description + “|” price
your Item class need to implement the Comparable interface. When you implement the Comparable interface you will have to implement compareTo method. Your compareTo method should compare two items based on their uid.
You will need another class that you should name ShoppingCart that will do the following:
readItemsFromFile(): read all items from unsortedItems.txt file and store them to an array of Item Type.
sort(): sorts all items stored in the array of items using bubble sort algorithm.
writeSortedItemsToFile(): writes all sorted list back to a new file called sortedItems.txt
displayItems(): print all items in the file to the console
search(int): search for an item based on its uid. If the item is found display it, otherwise display a message saying (“item not found”).
constructor that accepts an int value to set the size of the items array.
package edu.century.pa4;
public class PA4Driver {
public static void main(String[] args) {
int numberOfItems = 100;
// create an instance of shopping cart that will store
// 200 items.
ShoppingCart cart = new ShoppingCart(numberOfItems);
// read all the items from unsortedItems.txt file and
// store them into an array of type Item called itemsList
cart.readItemsFromFile();
System.out.println("-------------------------");
// sort all the items stored in the itemsList
cart.sort();
System.out.println("-------------------------");
// write the sorted items from the itemsList array into sortedItems.txt
cart.writeSortedItemsToFile();
System.out.println("-------------------------");
// display the content of itemsList array to the console
cart.displayItems();
System.out.println("-------------------------");
// search for item with id 62
cart.search(62);
System.out.println("-------------------------");
// search for item with id 9999
cart.search(9999);
System.out.println("-------------------------");
}
}
The unsortedItems.txt
27 | item_1 | description_1 | 1295.85
51 | item_2 | description_2 | 459.85
17 | item_3 | description_3 | 1176.06
62 | item_4 | description_4 | 535.32
51 | item_5 | description_5 | 880.90
42 | item_6 | description_6 | 1387.96
44 | item_7 | description_7 | 1343.32
82 | item_8 | description_8 | 1393.50
51 | item_9 | description_9 | 1082.49
57 | item_10 | description_10 | 570.02
33 | item_11 | description_11 | 415.95
82 | item_12 | description_12 | 976.84
48 | item_13 | description_13 | 1103.65
8 | item_14 | description_14 | 1101.23
27 | item_15 | description_15 | 1032.90
37 | item_16 | description_16 | 480.83
61 | item_17 | description_17 | 432.77
24 | item_18 | description_18 | 517.75
34 | item_19 | description_19 | 892.85
74 | item_20 | description_20 | 554.52
70 | item_21 | description_21 | 1153.44
68 | item_22 | description_22 | 802.79
67 | item_23 | description_23 | 1233.16
27 | item_24 | description_24 | 642.06
80 | item_25 | description_25 | 964.52
70 | item_26 | description_26 | 1139.07
61 | item_27 | description_27 | 419.44
33 | item_28 | description_28 | 1377.53
39 | item_29 | description_29 | 1023.80
99 | item_30 | description_30 | 1333.49
45 | item_31 | description_31 | 1384.06
33 | item_32 | description_32 | 583.39
71 | item_33 | description_33 | 978.46
9 | item_34 | description_34 | 1089.71
22 | item_35 | description_35 | 901.91
16 | item_36 | description_36 | 673.65
14 | item_37 | description_37 | 795.64
47 | item_38 | description_38 | 554.52
76 | item_39 | description_39 | 941.62
5 | item_40 | description_40 | 512.40
69 | item_41 | description_41 | 1256.43
99 | item_42 | description_42 | 813.72
25 | item_43 | description_43 | 580.59
77 | item_44 | description_44 | 1028.44
27 | item_45 | description_45 | 1226.51
15 | item_46 | description_46 | 1263.29
73 | item_47 | description_47 | 1222.08
73 | item_48 | description_48 | 1136.99
89 | item_49 | description_49 | 1066.71
42 | item_50 | description_50 | 1031.69
Explanation / Answer
Please find the required solution: package edu.century.pa4; import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; /** * Create Item class with Comparable */ class Item implements Comparable { private String name; private int uid; private String description; private double price; public Item( String name, int uid, String description, double price ) { this.name = name; this.uid = uid; this.description = description; this.price = price; } public String getName() { return name; } public void setName( String name ) { this.name = name; } public int getUid() { return uid; } public void setUid( int uid ) { this.uid = uid; } public String getDescription() { return description; } public void setDescription( String description ) { this.description = description; } public double getPrice() { return price; } public void setPrice( double price ) { this.price = price; } @Override public String toString() { return uid + "|" + name + "|" + description + "|" + price; } @Override public int compareTo( Item o ) { return Integer.valueOf(uid).compareTo(o.getUid()); } } /** * Implementation of shoppingcart methods */ class ShoppingCart { int numberOfItems; List items; ShoppingCart( int numberOfItems ) { this.numberOfItems = numberOfItems; items = new ArrayList(); } void readItemsFromFile() throws FileNotFoundException { Scanner file = null; int i = 0; file = new Scanner(new File("unsortedItems.txt")); while( file.hasNext() && iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.