Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When i sort my data it doesn\'t save to the external file. Also when i sort my d

ID: 3673947 • Letter: W

Question

When i sort my data it doesn't save to the external file. Also when i sort my data it outputs

0 0 0 before the sort. Can someone point into the right direction.

import java.io.*;
import java.util.Scanner;

public class Driver
{
   public static void main(String[] args)
   {
   // local constants

   // local variables
   String fileName = "items.txt";
   Scanner scanner = null;
   ItemsList itemsList = new ItemsList(5);
   int i = 0;
   int choice = 0;
   String itemName;
   String name;
   double price;
   int qty;
   String cho = "y";

   Scanner scan = new Scanner(System.in);

   //************************************************************************************//

   do {
// Ask the user the following
   System.out.println(" Menu ");
   System.out.println(" 1. Add item");
   System.out.println(" 2. Display items");
   System.out.println(" 3. Sort");
   System.out.println(" 4. Exit ");
   System.out.print(" Enter your choice : ");
   choice = scan.nextInt();

   switch (choice) {
   // Add an item to the itemsList
   case 1:
   // Ask user for to enter information
   System.out.print(" Enter item name : ");
   name = scan.next();

   System.out.print(" Enter price : ");
   price = scan.nextDouble();

   System.out.print(" Enter quantity : ");
   qty = scan.nextInt();

   // Add the OneItem to the itemsList
   itemsList.addItem(new OneItem(name, price, qty));
   break;

   case 2:
   // print the list
   System.out.printf("%-10s%-10s%-10s ", "Item", "Price", "Quantity");
   System.out.println(itemsList.toString());
   break;

   case 3:
   itemsList.sort();
   break;
  
   case 4:
   // Terminate the program
   System.out.println(" Terminate the program.");
   break;

   default:
   System.out.println(" Incorrect option is selected.");
   break;
   }
   // Ask user if they would like to continue
   System.out.print(" Would to like to continue <(Y or N)> : ");
   cho = scan.next();
   System.out.println();

   writeToFile(itemsList);
  
   } while (cho.equalsIgnoreCase("Y"));

   //open the file and catch the exception if file not found
   try
   {
scan = new Scanner(new File(fileName));

   // read file items until end of file
   while (scan.hasNext())
   {
   itemName = scan.next();
   price = scan.nextDouble();
   qty = scan.nextInt();

   // Add the OneItem object to the itemList
   itemsList.addItem(new OneItem(itemName, price, qty));
   i++;
   }

   // close the file object
   scan.close();
  
   } catch(FileNotFoundException e)
   {
   System.out.println(e.getMessage());
   }

}

   private static void writeToFile(ItemsList itemsList)
   {

   // Create a file name called items.txt
   String filename = "items.txt";
   // Create a variable of Class PrintWriter
   PrintWriter filewriter = null;

   try {
   // create an instance of PrintWriter
   File file = new File(filename);

   // if file doesnt exists, then create it
   if (!file.exists())
   {
   file.createNewFile();
   }

   FileWriter fw = new FileWriter(file.getAbsoluteFile());
   BufferedWriter bw = new BufferedWriter(fw);

   OneItem items[] = itemsList.getItems();

   for (int i = 0; i < itemsList.getCount(); i++)
   {
bw.write(items[i].toString());
   }

   bw.close();

   }
   catch (IOException e)
   {
   System.out.println(e.getMessage());
   }
   }
}// end Driver class

====================================

//ItemsList.java
public class ItemsList
{
// declare variables
private OneItem items[];
private String arr[];
private int size;
private int count;
String temp;

// constructor to set items, size and count to zero
public ItemsList() {
items = null;
size = 0;
count = 0;
}

public OneItem[] getItems() {
return items;

}

public int getCount() {
return count;

}

// Parameter constructor
public ItemsList(int size) {
items = new OneItem[size];

for (int i = 0; i < items.length; i++) {
items[i] = new OneItem();
}

this.size = size;
count = 0;
arr = new String[size];
}

// Add OneItem to the itemlist
public void addItem(OneItem item)
{
if (items.length == count)
{
resize();
items[count] = item;
count++;
} else {
items[count] = item;
count++;
}
}

// Resize
private void resize()
{
int oldsize = size;
count = oldsize;
int newsize = 2 * this.size;
size = newsize;

OneItem[] tempList = new OneItem[size];

for (int i = 0; i < oldsize; i++)
tempList[i] = items[i];

items = new OneItem[size];
items = tempList;
}

   public void sort()
   {
for(int i = 0; i < size; i++)
{
   arr[i] = items[i].toString();
}
   for(int i =0; i< size; i++)
   {
for(int j =i +1; j< size; j++)
{
if(arr[i].compareTo(arr[j])>0)
{
   temp = arr[i];
   arr[i] =arr[j];
   arr[j] = temp;
}
}
   }
  
for(int i = 0; i < size; i++)
{
   System.out.println(arr[i]);
   }
}

// getSize
public int getSize()
{
return count;
}

// toString
public String toString()
{
String description = "";

for (int i = 0; i < count; i++)
{
description += items[i].toString();
}

return description;
}
   }

================================

public class OneItem
{
   //declare a variables
   private String name;
   private double price;
   private int quantity;

   //default constructor
   public OneItem()
   {
   name = "";
   price = 0;
   quantity = 0;
   }
   //parameter constructor
   public OneItem(String name, double price, int quantity)
   {
   this.name = name;
   this.price = price;
   this.quantity = quantity;
   }

   //toString
   public String toString() {
   return String.format("%-10s%-10.2f%-10d ", name,price,quantity);
   }

1. Add a method that will Sort the data in your Array of Objects. Pick one default field to sort the data by. Display a menu that will allow the User to select at least one other field to sort the data by. C. Be sure to also include the following: 1. Create a Driver Class with a Menu with the option to add an item, to print out a list of all items, to sort, and exit. Include a sub-menu for Sort option that allows the User to select which field they wish to sort by. 2. When your program is first executed, read data from an external file and place it in your array. Include a minimum of 5 sets of data in the file. Submit the external file with your program. 3. Save the data back to the external file when the Exit option is chosen from the menu in the Driver. NOTE: You MUST read your data from an external file! You MUST save your data back to that external file when done.

Explanation / Answer

Please find the corrected code below :

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote