After a huge success selling our flower packs to local shops we’ve acquired enou
ID: 3835005 • Letter: A
Question
After a huge success selling our flower packs to local shops we’ve acquired enough money to replace all of our items and had plenty left over to leave Alexander and Elizabeth so they can hire a doctor to travel in and hopefully help Elizabeth recover. Things have been happening very quickly and we haven’t had much time to process what we’ve accomplished, so here’s a little recap of everything we’ve done since we started our journey.
Created a basic flower pack that can add, display and remove (using arrays) – Assignment 1
Searched through the flower pack. – Assignment 1
Sorted the flower pack. – Assignment 1
Created a flower object to hold attributes of each flower. – Assignment 2
Upgraded our flower pack (to now use an ArrayList) – Assignment 3
Filter the flower pack based on a partial search. – Assignment 3
Upgraded our flower pack (to now hold multiple different types of plants) – Midterm
Saved and Loaded information to/from a file. – Assignment 4
*This list does not include information or tasks specific to labs
Several days after we’ve left and continued the journey we began so long ago we come to another small town that looks like a great place to stay for the night. The first thing we do, and have always done, is look for an inn. After we find what looks like the best inn we’ve ever seen and reserve our room we head to the tavern to socialize and hopefully find someone who catches our eye. After sitting at the bar for a bit a nice older lady comes up to us and starts a simple conversation. During this, we discover she is the owner of the small flower shop on the corner (what are the chances!). We politely tell her about the system we’ve developed, but sadly she isn’t interested. The bartender overhears our conversation and when the lady leaves he comes over to ask us more about our system.
The next morning we wake up in our room and look to our night stand where there is a candle that apparently burned the entire night and is hanging on to the last bit of light it can produce. Below the candle we notice a contract with what could only be construed as our signature on the bottom. What did we agree to last night??
After reading the contract it appears we’ve agreed to create a flower pack for the bartender to use. The bartender hopes to take our system, make copies and sell as many as he can to rebuild the home he lost in a fire a long time ago. There is a drawing on the first page with what appears to be some sort of interface, but it can’t be interpreted in any meaningful way.
Here’s our task for the week, since violating a contract results in death, we have no option but to finish.
Create an interface for the user to display, add, remove, sort, and filter our flower pack.You should use all of the following at least once in your interface
Stage
Button
Label
TextField
CheckBox
Layout (you can use any layout found in the lecture or book)
Menu
MenuItem
Load and Save file is not required for this assignment
The flower pack should hold flowers, weeds, fungus, and now herbs. All should be a subclass of a plant class.
Each subclass shares certain qualities (ID, Name, and Color)
Flower traits include (Thorns and Smell)
Fungus traits include (Poisonous)
Weed traits include (Poisonous, Edible and Medicinal)
Herb traits include (Flavor, Medicinal, Seasonal)
Submit 6 files: Assignment5.java, flower.java, fungus.java, plant.java, herb.java and weed.java
Explanation / Answer
ANSWER::
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.text.SimpleDateFormat;
public class FlowerShop extends MouseAdapter implements ActionListener{
Flower inventory;
JButton [] flowerButton;
JButton transactionOver;
JFrame fProduct, fReceipt;
JPanel buttonPanel;
JPanel textPanel;
JPanel receiptPanel;
JPanel backgroundPanel;
JTextField flowerPurchased, unitPrice, quantity;
JTextField purchasePrice, netCostField;
JTextArea receipt;
JScrollPane scrollPane;
int flowerCount=0;
int maxFlowers=100;
double totalCost=0;
Flower [] flower=new Flower[maxFlowers];
String inventoryLocation="FlowerPictures";
public FlowerShop(){
public void getFlowerdata(String fileName) throws Exception{
File file = new File(fileName);
Scanner in = new Scanner(file);
flowerButton=new JButton[maxFlowers] ;
while (in.hasNext()){
String name=in.next();
double unitPrice=in.nextDouble();
String location=inventoryLocation+"/"+name+".jpg";
Flower f=new Flower(name, location, unitPrice);
flower[flowerCount]=f;
flowerCount++;
System.out.println(flowerCount+" "+name +" "+unitPrice);
}
}
public void createButtons(){
for(int i=0; i<flowerCount; i++){
flowerButton[i]=new JButton(flower[i].getIcon());
flowerButton[i].addActionListener(this);
flowerButton[i].addMouseListener(this);
}
transactionOver=new JButton("Checkout");
transactionOver.addActionListener(this);
}
public void createTextBoxes(){
final int TEXTFIELD_WIDTH=10;
flowerPurchased=new JTextField("None", TEXTFIELD_WIDTH);
unitPrice=new JTextField("$0.0", TEXTFIELD_WIDTH);
quantity=new JTextField("0", TEXTFIELD_WIDTH);
quantity.addActionListener(this);
purchasePrice=new JTextField("$0.0", TEXTFIELD_WIDTH);
netCostField=new JTextField("$0.0", TEXTFIELD_WIDTH);
receipt=new JTextArea("Your receipt ", 20, 40);
scrollPane = new JScrollPane(receipt);
}
public void createPanels(){
textPanel=new JPanel(new GridLayout(2,5));
textPanel.add(new JLabel("Flower"));
textPanel.add(new JLabel("Quantity"));
textPanel.add(new JLabel("Unit Price"));
textPanel.add(new JLabel("You pay"));
textPanel.add(new JLabel("Total Price"));
textPanel.add(flowerPurchased);
textPanel.add(quantity);
textPanel.add(unitPrice);
textPanel.add(purchasePrice);
textPanel.add(netCostField);
buttonPanel=new JPanel(new GridLayout(5,2));
for(int i=0; i<flowerCount; i++){
buttonPanel.add(flowerButton[i]);
}
buttonPanel.add(transactionOver);
receiptPanel=new JPanel();
receiptPanel.setBackground(Color.BLUE);
receiptPanel.add(scrollPane);
backgroundPanel=new JPanel();
backgroundPanel.setBackground(Color.YELLOW);
backgroundPanel.add(textPanel);
backgroundPanel.add(buttonPanel);
backgroundPanel.add(receiptPanel);
}
public void createFrame(){
JFrame.setDefaultLookAndFeelDecorated(true);
fProduct=new JFrame("CS 180 Flower Shop");
fProduct.setSize(700, 500);
fProduct.add(backgroundPanel);
fProduct.setVisible(true);
Point p=fProduct.getLocationOnScreen();
Dimension d=fProduct.getSize();
fReceipt=new JFrame("CS 180 Flower Shop: Receipt");
fReceipt.setSize(600, 400);
fReceipt.add(receiptPanel);
fReceipt.setLocation(p.x, p.y+d.height+5);
fReceipt.setVisible(true);
}
public void newTransaction(){
}
public String getDate(){
Date today;
String output;
SimpleDateFormat formatter;
String pattern="yyyy.MMMMM.dd GGG hh:mm aaa";
formatter = new SimpleDateFormat(pattern);
today = new Date();
output = formatter.format(today);
return(output);
}
public void actionPerformed(ActionEvent e){
Object ob=e.getSource();
if(ob==transactionOver){
if(totalCost>0){
Date d;
receipt.append(" Your Total: "+String.format("$%.2f ", totalCost));
receipt.append(" Thank you. Please come again. ");
receipt.append(getDate());
}
return;
}
boolean objectLocated=false;
int nextObject=0;
while(nextObject<flowerCount &&!objectLocated){
if(ob==flowerButton[nextObject]){
objectLocated=true;
}else{
nextObject++;
}
}
if(objectLocated){
String fPurchased=flower[nextObject].getKind();
flowerPurchased.setText(fPurchased);
double uPrice=flower[nextObject].getUnitPrice();
String formattedUPrice="$"+Double.toString(uPrice);
unitPrice.setText(formattedUPrice);
String q=quantity.getText();
double quantDouble=Double.parseDouble(q);
if(quantDouble>0){
double price=quantDouble*uPrice;
String formattedPrice="$"+String.format("%.2f",price);
purchasePrice.setText(formattedPrice);
totalCost=totalCost+price;
String fTotalCost=String.format("$%.2f ", totalCost);
netCostField.setText("$"+fTotalCost);
String r=fPurchased+" "+q+" "+formattedUPrice+
" "+formattedPrice+" "+fTotalCost+" ";
receipt.append(r);
}
}
}
public void mouseEntered(MouseEvent e){
Object ob=e.getSource();
boolean buttonLocated=false;
int nextObject=0;
while(nextObject<flowerCount &&!buttonLocated){
if(ob==flowerButton[nextObject]){
buttonLocated=true;
}else{
nextObject++;
}
}
if(buttonLocated){
flowerPurchased.setText(flower[nextObject].getKind());
unitPrice.setText("$"+Double.toString(flower[nextObject].getUnitPrice()));
}
}
public static void main(String [] args)throws Exception{
String fileName="flowerInventory.txt";
FlowerShop flowerShop=new FlowerShop();
flowerShop.getFlowerdata(fileName);
flowerShop.createButtons();
flowerShop.createTextBoxes();
flowerShop.createPanels();
flowerShop.createFrame();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.