In: Computer Science
I am trying to get this code to work but I am having difficulties, would like to see if some one can solve it. I tried to start it but im not sure what im doing wrong. please explain if possible
package edu.hfcc;
/*
* Create Java application that will create Fruit class and Bread
class
*
* Fruit class will have 3 data fields name and quantity which you
can change.
* The third data field price should always be 2.0
* Bread class will have 3 data fields name, quantity and price. All
three data fields can change.
*
* Create method that creates the string used write to console and
return from execute()
*
* In GroceryApp
* Create method to calculate totalPrice for fruit
* Create method to calculate totalPrice for bread
* Create method to print to console use EXACLTY like example
below
*
* Data to use
* Bread: Name=French,
Quantity=2, Price=3.0
* Fruit: Name=Apple, Quantity=5,
*
* EXAMPLE TO Console
* French
2 @ 3.0
6.0
Apple
5 @ 2.0
10.0
---------------------
16.0
*/
public class Grocery {
private static final char NEW_LINE = '\n';
private static final String TAB = "\t";
public String execute(){
Fruit fruitOne = new Fruit();
fruitOne.setName("Apple");
fruitOne.setQuantity( 5);
String fruits =
constructOutputFruit(apple);
System.out.println(fruits);
return fruits;
}
private String constructOutputFruit(Fruit fruit1)
{
String apple = fruit1.getName() +
NEW_LINE + fruit1.getQuantity() + TAB + fruit1.getPrice();
return apple + NEW_LINE;
}
}
here are my classes:
Fruit:
package edu.hfcc;
public class Fruit {
private String name;
private int quantity;
private float price;
public Fruit(){
this.price = (float)2.0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public float getPrice() {
return price;
}
}
Bread:
package edu.hfcc;
public class Bread {
private String name;
private int quantity;
private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
class Fruit {
private String name;
private int quantity;
private float price;
public Fruit(){
this.price = (float)2.0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public float getPrice() {
return price;
}
}
class Bread {
private String name;
private int quantity;
private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
class GroceryApp
{
public static void main (String[] args)
{
Bread bread1 = new Bread();
bread1.setName("French");
bread1.setQuantity(2);
bread1.setPrice(3.0f);
System.out.println(bread1.getName() + "\n" + bread1.getQuantity() +
"@"+ bread1.getPrice() +"\t"+totalPriceBread(bread1));
Fruit fruit1 = new Fruit();
fruit1.setName("Apple");
fruit1.setQuantity( 5);
System.out.println(fruit1.getName() + "\n" + fruit1.getQuantity() +
"@"+ fruit1.getPrice() +"\t"+totalPriceFruit(fruit1));
System.out.println("------------------------------");
System.out.println("\t"+(totalPriceFruit(fruit1)+totalPriceBread(bread1)));
}
public static float totalPriceFruit(Fruit f)
{
return f.getQuantity() * f.getPrice();
}
public static float totalPriceBread(Bread b)
{
return b.getQuantity() * b.getPrice();
}
}
Output:
French [email protected] 6.0 Apple [email protected] 10.0 ------------------------------ 16.0
Do ask if any doubt. Please upvote.