1.Change the representation for the ProduceItems in the database from an array t
ID: 3765532 • 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
ProduceItem class:
public abstract class ProduceItem
{
private String code;
private String name;
private float price;
ProduceItem() {}
ProduceItem(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;
}
}
Fruit class:
public class Fruit extends ProduceItem
{
public Fruit(String code,String name,float price)
{
super(code,name,price);
}
}
Vegetable class:
public class Vegetable extends ProduceItem
{
public Vegetable(String code,String name,float price)
{
super(code,name,price);
}
}
MyJFrame class:
import java.awt.*;
import java.text.DecimalFormat;
import javax.swing.*;
public class MyJFrame
{
public MyJFrame(String myLine, float myTot)
{
DecimalFormat df = new DecimalFormat("0.00");
/* 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: $" + df.format(myTot));
myFram.getContentPane().add(jL);
tA.setText(myLine);
/* Displaying window */
myFram.pack();
myFram.setVisible(true);
}
}
Database class:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Database {
ProduceNode start,end;
int length;
void addNode(ProduceItem data)
{
if(length == 0)
{
start = new ProduceNode(data);
end = start;
}
else
{
ProduceNode newNode = new ProduceNode(data);
newNode.next = start;
start = newNode;
}
length++;
}
public static void main(String args[]) throws IOException {
Database db = new Database();
String line;
String word[], code,output = "";
ProduceItem newItem;
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(",");
switch(word[0])
{
case "V":
newItem = new Vegetable(word[1], word[2],Float.parseFloat(word[3]));
db.addNode(newItem);
break;
case "F":
newItem = new Vegetable(word[1], word[2],Float.parseFloat(word[3]));
db.addNode(newItem);
break;
}
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(db,code);
output += getName(db,code) + " " + getPrice(db,code) + " " + weight
+ " " + weight * getPrice(db,code) + " ";
line = reader.readLine();
}
reader.close();
output += " Total Cost: " + totalCost;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
new MyJFrame(output, totalCost);
}
static String getName(Database db,String code) {
ProduceNode temp = db.start;
while(temp != null)
{
if (temp.data.getCode().equals(code)) {
return temp.data.getName();
}
}
return "";
}
static float getPrice(Database db,String code) {
ProduceNode temp = db.start;
while(temp != null)
{
if (temp.data.getCode().equals(code)) {
return temp.data.getPrice();
}
}
return 0;
}
}
class ProduceNode
{
ProduceItem data;
ProduceNode next;
ProduceNode(ProduceItem data)
{
this.data = data;
next = null;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.