Showing posts with label This program reads in three random vegetables from a predefined list and asks if the user would like to substitute any produce. The user can substitute produce as many times as they like.. Show all posts
Showing posts with label This program reads in three random vegetables from a predefined list and asks if the user would like to substitute any produce. The user can substitute produce as many times as they like.. Show all posts

Monday, July 2, 2012

box of produce JAVA CODE


Description:
* This program reads in three random vegetables from a predefined list and asks if the user would like
* to substitute any produce. The user can substitute produce as many times as they like.
* Once the user confirms the content of the box, the contents of the final box is output.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TestBoxOfProduce
{
public static void main(String[] args)
{

// reads in the ListOfProduce file
Scanner file = null;
try {
file = new Scanner(new FileInputStream("text"));
}
catch (FileNotFoundException e) {}

String item1 = file.next();
String item2 = file.next();
String item3 = file.next();
String item4 = file.next();
String item5 = file.next();
Scanner user = new Scanner(System.in);

BoxOfProduce1 CSA = new BoxOfProduce1();

System.out.println("Your box of produce contains: ");
System.out.println("[ "+CSA.getProduce1() + ", "
+ CSA.getProduce2() + ", " + CSA.getProduce3()+" ]");

String loop = "n";
System.out.println("\nAre you happy with your selection? (Y/N)\n>");
loop = user.next();

//begins loop if user is not satisfied with their selection
while (loop.equalsIgnoreCase("n"))
{
Scanner user2 = new Scanner(System.in);
Scanner user3 = new Scanner(System.in);

System.out.println("\nYou can choose from the following items: \n(1)" + item1 +" \n(2)" + item2+" \n(3)"+item3+" \n(4)"+item4+" \n(5)"+item5);
System.out.println("Which item do you prefer? Please select an item number.\n>");
int choice = user2.nextInt();
String item = null;

//given user input for their choice, sets the item to this new choice
if (choice==1)
item =item1;
if (choice==2)
item =item2;
if (choice==3)
item=item3;
if (choice==4)
item =item4;
if (choice==5)
item =item5;
System.out.println("You prefer "+item.toLowerCase());

System.out.println("\nWhich item would you like to replace?");
System.out.println("(1)"+CSA.getProduce1() + " "
+ "(2)" + CSA.getProduce2() + " " + "(3)"+CSA.getProduce3()+"\n>");
int replace = user3.nextInt();

if (replace == 1)
{
CSA.setProduce1(item);
}
if (replace == 2)
{
CSA.setProduce2(item);
}
if (replace == 3)
{
CSA.setProduce3(item);
}


System.out.println("\nYour new selection contains: ");
System.out.println(CSA.getProduce1() + ", "
+ CSA.getProduce2() + ", " + CSA.getProduce3());

System.out.println("\nAre you happy with your selection? (Y/N)");
loop = user.next();

}


System.out.println("Enjoy your selection.");

}
}

*************************************

file #2

Description:
* This program defines the class, accessor, and mutator variables.
* It takes values from the file, "ListOfProduce," puts them into an array with numbers corresponding to each item.
* It produces three random numbers and accesses the corresponding value of the array.
*


import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.io.*;


public class BoxOfProduce1
{
private String produce1, produce2, produce3 ;

public BoxOfProduce1()
{
this.produce1 = "";
this.produce2 = "";
this.produce3 = "";

//creates an array from the read in file (ListOfProduce)
ArrayList<String> produce = new ArrayList<String>();

Scanner file = null;
try {
file = new Scanner(new FileInputStream("text"));
}
catch (FileNotFoundException e)
{
}
String item1 = file.next();
String item2 = file.next();
String item3 = file.next();
String item4 = file.next();
String item5 = file.next();

produce.add(item1);
produce.add(item2);
produce.add(item3);
produce.add(item4);
produce.add(item5);

// creates 3 random variables from a possible 5 which correspond to the values in the array
Random r= new Random();
int x1 = r.nextInt(5);
int x2 = r.nextInt(5);
int x3 = r.nextInt(5);

this.produce1 = produce.get(x1);
this.produce2 = produce.get(x2);
this.produce3 = produce.get(x3);


}
public BoxOfProduce1 (String newProduce1, String newProduce2, String newProduce3)
{
this.produce1 = newProduce1;
this.produce2 = newProduce2;
this.produce3 = newProduce3;
}

//accessor1
public String getProduce1()
{
return this.produce1;
}

//mutator1
public void setProduce1 (String newProduce1)
{
this.produce1 = newProduce1;
}
//accessor2
public String getProduce2()
{
return this.produce2;
}
//mutator2
public void setProduce2(String newProduce2)
{
this.produce2 = newProduce2;
}
//accessor3
public String getProduce3()
{
return this.produce3;
}
//mutator3
public void setProduce3(String newProduce3)
{
this.produce3 = newProduce3;
}
public String toString()
{
return "Produce 1: " + this.produce1 + "Produce 2: " + this.produce2 +" Produce 3: " + this.produce3;
}
}