We will write a simulation of someone shopping in a grocery store for produce (f
ID: 3770055 • Letter: W
Question
We will write a simulation of someone shopping in a grocery store for produce (fruits and vegetables). An input file will contain the code number and the weight of the item being purchased. A database will contain the code number, the name of the item (such as “Pineapple”) and the price per pound.
1- Create a class called ProduceItem with three instance variables for the code (String), name (String) and price (float). Include appropriate constructors, get/set methods and overrides of class Object.
2- Create a class for the database. It should contain an array which will store all the ProcudeItems (that will be read in from a file). This class should contain, among others, methods that will return the name and the price of an item with a given code:
String getName(String code);
float getPrice(String code);
The output of the program should be a JFrame (a separate GUI class) that serves as a receipt for the customer. It should contain, one per line, the name, price, weight and total cost of each item, and at the end the total cost of all purchases.
3- 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.
4- 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
5- Include a File menu with the options Open and Quit. The open option is used to open a transaction file and run the transactions.
6- Include a Database menu with three menu items: Open (let the user choose a file to initialize the database), Display Fruits (use a JFrame to display all the fruits in the database), and Display Vegetables. Write a DatabaseMenuHandler for this menu item.
The transaction file for this project will include PLU codes that are not in the database. Have the database throw an ItemNotFoundException in this case, and include a try/catch block in your program that will catch this exception and pop up a JOptionPane input dialog to get the price for the missing item.
NB: 13 files need to be submited!!!!!!
Explanation / Answer
public class ProduceItem
{
private float price;
private String name;
private String code;
construct(String nam,String cod,float z){
price = z;
name = nam;
code = cod;
}
void display(){System.out.println("code"+code+"name"+name+"price"+price);}
public static void main(String args[]){
ProduceItem myProduceItem = new ProduceItem( "ora1","orange",01.11 );
construct s1 = new construct("pin1","Pineapple",11.11);
construct s2 = new construct("app1","apple",21.11,);
s1.display();
s2.display();
}
public int getCode(){
return code;
}
public String getName(){
return name;
}
public String getPrice(){
return price;
}
public void setCode( float newCode){
code = newCode;
}
public void setName(String newName){
name = newName;
}
public void setPrice( String newPrice){
price = newPrice;
}
}
Veg.java
Fruitshop.java
public class Veg extlasts ProduceItem
{
public Veg(String code,String name,float price)
{
super(code,name,price);
}
}
public class Fruitshop extlasts ProduceItem
{
public Fruitshop(String code,String name,float price)
{
super(code,name,price);
}
}
public class Database {
ProduceNode first,last;
int length;
void addItem(ProduceItem data)
{
if(length == 0)
{
first = new ProduceNode(data);
last = first;
}
else
{
ProduceNode Node1 = new ProduceNode(data);
Node1.next = first;
first = Node1;
}
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;
BufferedReader reader= null;
try {
reader = new BufferedReader(new FileReader(
"database.txt"));
line = reader.readLine();
while (line != null) {
word = line.split(",");
switch(word[0])
{
case "V":
newItem = new Veg(word[0], word[1],Float.parseFloat(word[2]));
db.addItem(newItem);
break;
case "F":
newItem = new Fruitshop(word[0], word[1],Float.parseFloat(word[2]));
db.addItem(newItem);
break;
}
totalItems++;
line = reader.readLine();
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
output += "Name Price Weight Cost ";
try {
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 myJF(output, totalCost);
}
static String getName(Database db,String code) {
ProduceNode temp = db.first;
while(temp != null)
{
if (temp.data.getCode().equals(code)) {
return temp.data.getName();
}
else
temp=temp.next;
}
return "";
}
static float getPrice(Database db,String code) {
ProduceNode temp = db.first;
while(temp != null)
{
if (temp.data.getCode().equals(code)) {
return temp.data.getPrice();
}
else
temp=temp.next;
}
return 0;
}
}
class ProduceNode
{
ProduceItem data;
ProduceNode next;
ProduceNode(ProduceItem data)
{
this.data = data;
next = null;
}
}
myJF.java
public class myJF
{
public myJF(String myLine, float myTot)
{
DecimalFormat DecimalFor = new DecimalFormat("0.00");
JFrame myFram = new JFrame("slip");
myFram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFram.setSize(310,310);
myFram.setLocation(210,110);
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: $" + DecimalFor.format(myTot));
myFram.getContentPane().add(jL);
tA.setText(myLine);
myFram.pack();
myFram.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.