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
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<Item> { 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<Item> 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() && i < numberOfItems ) { String[] values = file.nextLine().split("\\|"); Item item = new Item(values[1].trim(), Integer.valueOf(values[0].trim()), values[2].trim(), Double.valueOf(values[3].trim())); items.add(item); } file.close(); } void sort() { Collections.sort(items); } void writeSortedItemsToFile() throws IOException { BufferedWriter writer = new BufferedWriter(new FileWriter("sortedItems.txt")); for( Item item : items ) { writer.write(item.toString()); writer.newLine(); } writer.close(); } void displayItems() { for( Item item : items ) System.out.println(item.toString()); } void search( int uid ) { for( Item item : items ) if( uid == item.getUid() ) { System.out.println(uid + " found"); return; } System.out.println(uid + " not found"); } } public class PA4Driver { public static void main( String[] args ) throws IOException { 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("-------------------------"); } } Sample output:(written to file as well)
------------------------- ------------------------- ------------------------- 5|item_40|description_40|512.4 8|item_14|description_14|1101.23 9|item_34|description_34|1089.71 14|item_37|description_37|795.64 15|item_46|description_46|1263.29 16|item_36|description_36|673.65 17|item_3|description_3|1176.06 22|item_35|description_35|901.91 24|item_18|description_18|517.75 25|item_43|description_43|580.59 27|item_1|description_1|1295.85 27|item_15|description_15|1032.9 27|item_24|description_24|642.06 27|item_45|description_45|1226.51 33|item_11|description_11|415.95 33|item_28|description_28|1377.53 33|item_32|description_32|583.39 34|item_19|description_19|892.85 37|item_16|description_16|480.83 39|item_29|description_29|1023.8 42|item_6|description_6|1387.96 42|item_50|description_50|1031.69 44|item_7|description_7|1343.32 45|item_31|description_31|1384.06 47|item_38|description_38|554.52 48|item_13|description_13|1103.65 51|item_2|description_2|459.85 51|item_5|description_5|880.9 51|item_9|description_9|1082.49 57|item_10|description_10|570.02 61|item_17|description_17|432.77 61|item_27|description_27|419.44 62|item_4|description_4|535.32 67|item_23|description_23|1233.16 68|item_22|description_22|802.79 69|item_41|description_41|1256.43 70|item_21|description_21|1153.44 70|item_26|description_26|1139.07 71|item_33|description_33|978.46 73|item_47|description_47|1222.08 73|item_48|description_48|1136.99 74|item_20|description_20|554.52 76|item_39|description_39|941.62 77|item_44|description_44|1028.44 80|item_25|description_25|964.52 82|item_8|description_8|1393.5 82|item_12|description_12|976.84 89|item_49|description_49|1066.71 99|item_30|description_30|1333.49 99|item_42|description_42|813.72 ------------------------- 62 found ------------------------- 9999 not found -------------------------
How to create this program and makes its run with the giving the class PA4Driver with...
Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...
Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...
Ch 7 Program: Online shopping cart (continued) (Java)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...
Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to easily plug-in new sort algorithms and compare them. Assume that input data is generated randomly and stored in a text file (have no less than 2000 items to sort). Do not restrict your program to only one data type, or to one ordering relationship. The data type, ordering relationship, and the sorting method must be input parameters for your program....
In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...
Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods. Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement Element to be found */ int findAll(int...
Java using data structures The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in. Employees.txt: (No WhiteSpace...
After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...
FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...
In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...