Write a java program 1-Change the representation for the ProduceItems in the dat
ID: 3762276 • Letter: W
Question
Write a java program 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 interacts 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. Program
Explanation / Answer
package productitem; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Iterator; import java.util.LinkedList; public abstract class ProduceItem { private String code; public static void main(String[] args) throws IOException { LinkedList<Fruits> fruitlink = new LinkedList<Fruits>(); LinkedList<Vegetables> veglink = new LinkedList<Vegetables>(); File fin=new File("########### FILE PATH #################"); FileInputStream fis = new FileInputStream(fin); //Construct BufferedReader from InputStreamReader BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String line = null; while ((line = br.readLine()) != null) { String data=""; data=line; if("F".equalsIgnoreCase(data.substring(0))) { String[] a=line.split(","); Fruits fruits=new Fruits(a[1], Integer.valueOf(a[2]), Float.valueOf(a[3])); fruitlink.add(fruits); } else { String[] b=line.split(","); Vegetables vegetables=new Vegetables(b[1], Integer.valueOf(b[2]), Float.valueOf(b[3])); veglink.add(vegetables); } System.out.println(line); } br.close(); printProducts(fruitlink,veglink); } private static void printProducts(LinkedList<Fruits> fruitlink, LinkedList<Vegetables> veglink) { for (Iterator iterator = veglink.iterator(); iterator.hasNext();) { Vegetables vegetables = (Vegetables) iterator.next(); System.out.println("vegetables:"+vegetables.toString()); } for (Iterator iterator = fruitlink.iterator(); iterator.hasNext();) { Fruits fruits = (Fruits) iterator.next(); System.out.println("fruits:"+fruits.toString()); } } }
package productitem; public class Fruits extends ProduceItem { private String name; private int barcode; private float price; public Fruits(String name, int barcode, float price) { super(); this.name = name; this.barcode = barcode; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getBarcode() { return barcode; } public void setBarcode(int barcode) { this.barcode = barcode; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } @Override public String toString() { return "Fruits [name=" + name + ", barcode=" + barcode + ", price=" + price + "]"; } }
package productitem; public class Vegetables extends ProduceItem { private String name; private int barcode; private float price; public Vegetables(String name, int barcode, float price) { super(); this.name = name; this.barcode = barcode; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getBarcode() { return barcode; } public void setBarcode(int barcode) { this.barcode = barcode; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } @Override public String toString() { return "Vegetables [name=" + name + ", barcode=" + barcode + ", price=" + price + "]"; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.