Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Part I: Your Community Supported Agriculture (CSA) farm delivers a box of fresh fruits and vegetables to your house once a week. For this

JAVA Part I:

Your Community Supported Agriculture (CSA) farm delivers a box of fresh fruits and vegetables to your house once a week. For this Programming Project, define the class BoxOfProduce that contains exactly three bundles of fruits or vegetables. You can represent the fruits or vegetables as three instance variables of type String. Add appropriate constructor, accessor, and mutator methods. Also write a toString() method that returns as a String the complete contents of the box. Next, write a main method that creates a BoxOfProduce with three items randomly selected from this list: Broccoli Tomato Kiwi Kale Tomatillo This list should be stored in a text file that is read in by your program. For now you can assume that the list contains exactly five types of fruits or vegetables. Do not worry if your program randomly selects duplicate produce for the three items. Next, the main method should display the contents of the box and allow the user to substitute any one of the five possible fruits or vegetables for any of the fruits or vegetables selected for the box. After the user is done with substitutions, output the final contents of the box to be delivered. If you create additional methods to select the random items and to select valid substitutions, then your main method will be simpler to write.

JAVA

Part I: Your Community Supported Agriculture (CSA) farm delivers a box of fresh fruits and vegetables to your house once a week. For this Programming Project, define the class BoxOfProduce that contains exactly three bundles of fruits or vegetables. You can represent the fruits or vegetables as three instance variables of type String. Add appropriate constructor, accessor, and mutator methods. Also write a toString() method that returns as a String the complete contents of the box. Next, write a main method that creates a BoxOfProduce with three items randomly selected from this list: Broccoli Tomato Kiwi Kale Tomatillo This list should be stored in a text file that is read in by your program. For now you can assume that the list contains exactly five types of fruits or vegetables. Do not worry if your program randomly selects duplicate produce for the three items. Next, the main method should display the contents of the box and allow the user to substitute any one of the five possible fruits or vegetables for any of the fruits or vegetables selected for the box. After the user is done with substitutions, output the final contents of the box to be delivered. If you create additional methods to select the random items and to select valid substitutions, then your main method will be simpler to write.

Here is what I have for Part 1:

import java.util.Random;

import java.util.Scanner;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

public class CSA

{

public static void main(String[] args)

{

int SIZE = 5;

//scanner class to scan input file

Scanner scanner = new Scanner(System.in);

Scanner scannerobj = null;

String fruit = null;

//initializing items with size 5

String[] items = new String[SIZE];

//this try catch for to throw exceptions

try

{

scannerobj = new Scanner(new FileInputStream("items.txt"));

} catch (FileNotFoundException e)

{

System.out.println("File not found");

System.exit(0);

}

int index = 0;

//storing input file contens to array

while (scannerobj.hasNext())

{

fruit = scannerobj.nextLine();

items[index] = fruit;

index = index + 1;

}

//this all for getting randome fruit

Random random = new Random();

String firstFruitType = items[random.nextInt(SIZE)];

String secondFruitType = items[random.nextInt(SIZE)];

String thirdFruitType = items[random.nextInt(SIZE)];

BoxOfProduce boxOfProduce = new BoxOfProduce(firstFruitType, secondFruitType, thirdFruitType);

//printing the results according to constraints

System.out.print("****Random of 3 items***** ");

System.out.println(boxOfProduce.toString());

System.out.println("******Total items:*******");

for (int position = 0; position < items.length; position++)

System.out.println(position + 1 + ": " + items[position]);

System.out.println("----------------------");

System.out.print("Enter your choice:");

int listChoice = scanner.nextInt();

System.out.print("List of replace fruit items: ");

System.out.println(boxOfProduce.toString());

System.out.print("Choice of replace fruit item number(1-3): ");

int boxChoice = scanner.nextInt();

//this is for boxChoice

switch (boxChoice)

{

case 1:

boxOfProduce = new BoxOfProduce(items[listChoice - 1], secondFruitType, thirdFruitType);

break;

case 2:

boxOfProduce = new BoxOfProduce(firstFruitType, items[listChoice - 1], thirdFruitType);

break;

case 3:

boxOfProduce = new BoxOfProduce(firstFruitType, secondFruitType, items[listChoice - 1]);

break;

default:

System.out.println("Invalid choice");

}

System.out.println("Items:");

System.out.println(boxOfProduce.toString());

}

}

//this class is for boxofproduce with appopriate getters and setters method according to quests

class BoxOfProduce

{

private String f1;

private String f2;

private String f3;

BoxOfProduce(String f1, String f2, String f3) {

setFruit1(f1);

setFruit2(f2);

setFruit3(f3);

}

public void setFruit1(String f1)

{

this.f1 = f1;

}

public void setFruit2(String f2)

{

this.f2 = f2;

}

public void setFruit3(String f3)

{

this.f3 = f3;

}

public String getFruit1()

{

return f1;

}

public String getFruit2()

{

return f2;

}

public String getFruit3() {

return f3;

}

public String toString()

{

return "1 : " + f1 + " " + "2 : " + f2 + " " + "3 : " + f3;

}

}

items.txt file data:

Broccoli

Tomato

Kiwi

Kale

Tomatillo

Here is what I need help in.

Part II Modify the main method with a loop so that an arbitrary number of BoxOfProduce objects are created and substitutions are allowed for each box. Add a menu so the user can decide when to stop creating boxes. You would like to throw in a free recipe flyer for salsa verde if the box contains tomatillos. However, there are only five recipe flyers. Add a static variable to the BoxOfProduce class that counts the number of recipe flyers remaining and initialize it to 5. Also add an instance variable that indicates whether or not the box contains a recipe flyer and modify the toString() method to also output salsa verde recipe if the box contains a recipe flyer. Finally, add logic inside the class so that if the box contains at least one order of tomatillos then it automatically gets a recipe flyer until all of the recipe flyers are gone. Note that a box should only get one recipe flyer even if there are multiple orders of tomatillos. Test your class by creating boxes with tomatillos from your menu until all of the flyers are gone

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions