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

1.Change the representation for the ProduceItems in the database from an array t

ID: 3765453 • Letter: 1

Question

1.Change the representation for the ProduceItems in the database from an array to a linked list of ProduceItems. The linked list should have a first and last pointer and a length as shown in lecture. There shouldn’t be a need to modify how your program inter acts with the database. That is, the method signatures remain the same; only the internal representation for storing the ProduceItems changes.

2. Create a class hierarchy for ProduceItem where Fruit and Vegetable are subclasses of ProduceItem. Make ProduceItem abstract. The input file will now indicate if the code number is for a Fruit or a Vegetable by having a ‘F’ or ‘V’ as the first field: F,4088,Apple,1.69 V,3023,Carrot,0.99

3. Include appropriate Javadoc in the classes for the database, ProduceItem, Fruit and Vegetable. 4. Use a DecimalFormat object, if you haven ’t already done so, to print the total bill for the customer using only two decimal places

I need to edit this program

Produces.java

public class Produces {

   private String code;

   private String name;

   private float price;

   Produces() {

   }

   Produces(String code, String name, float price) {

       this.code = code;

       this.name = name;

       this.price = price;

   }

   public String getCode() {

       return code;

   }

   public void setCode(String code) {

       this.code = code;

   }

   public String getName() {

       return name;

   }

   public void setName(String name) {

       this.name = name;

   }

   public float getPrice() {

       return price;

   }

   public void setPrice(float price) {

       this.price = price;

   }

}

Database.java

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class Database {

   static Produces items[]=new Produces[60];

   public static void main(String args[]) throws IOException {

       String line;

       String word[], code, name, output = "";

       float weight, price, totalCost = 0;

       int totalItems = 0;

       /*

       * Create a database from "database.txt" file. Read all items make

       * object of items and store in items[] array

       */

       try {

           BufferedReader reader = new BufferedReader(new FileReader(

                   "database.txt"));

           line = reader.readLine();

           while (line != null) {

               word = line.split(",");

               code = word[0];

               name = word[1];

               price = Float.parseFloat(word[2]);

            

               items[totalItems] = new Produces(code, name, price);

               totalItems++;

               line = reader.readLine();

           }
reader.close();
       } catch (FileNotFoundException e) {

           e.printStackTrace();

       }

       /*

       * Read items purchased by the customer from file "purchase.txt"

       * Calculate total cost of all purchases

       */

       output += "Name Price Weight Cost ";

       try {

           BufferedReader reader = new BufferedReader(new FileReader(

                   "transaction.txt"));

           line = reader.readLine();

           while (line != null) {

               word = line.split(",");

               code = word[0];

               weight = Float.parseFloat(word[1]);

               totalCost = totalCost + weight*getPrice(code);

               output += getName(code) + " " + getPrice(code) + " " + weight+" "+weight

                       * getPrice(code)+" ";

               line = reader.readLine();

           }
reader.close();
           output += " Total Cost:   "+totalCost;

        

       } catch (FileNotFoundException e) {

           e.printStackTrace();

       }

       new myJFrame(output, totalCost);

   }

   static String getName(String code) {

       for (int i = 0; i < items.length; i++) {

           if (items[i] != null && (items[i].getCode()).equals(code)) {

               return items[i].getName();

           }

       }

       return "";

   }

   static float getPrice(String code) {

       for (int i = 0; i < items.length; i++) {

           if (items[i] != null && (items[i].getCode()).equals(code)) {

               return items[i].getPrice();

           }

       }

       return 0;

   }

}

myJFrame.java

import java.awt.*;

import javax.swing.*;

public class myJFrame

{

     public myJFrame(String myLine, float myTot)

     {

          /* Creating and settingup window */

          JFrame myFram = new JFrame("ShoppingReceipt");

          myFram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          myFram.setSize(310,310);

          /* width and height */

          myFram.setLocation(210,110);

          /* x and y */

          myFram.setLayout(new GridLayout(2,1));

          JTextArea tA = new JTextArea(30, 30);

          tA.setEditable(false);

          JScrollPane sP = new JScrollPane(tA);

          myFram.getContentPane().add(sP);

          JLabel jL = new JLabel("Shopping Total: $" + myTot);

          myFram.getContentPane().add(jL);

          tA.setText(myLine);

          /* Displaying window */

          myFram.pack();

          myFram.setVisible(true);

     }

}

Explanation / Answer

public abstract class ProduceItem{
private String field;
private String code;
private String name;
private float price;
Produces() {
}
public Produces(String field,String code, String name, float price) {
this.field=field;
this.code = code;
this.name = name;
this.price = price;
}
public String getField()
{
return field;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
  
public class Fruit extends ProduceItem
{
public Fruit(String code, String name, float price)
{
super("F",code,name,price);
}
}

public class Vegetable extends ProduceItem
{
public Vegetable (String code, String name, float price)
{
super("V",code,name,price);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote