/*
 * AssignmentOne: This class manages retailers and suppliers for
 *   sand.  It creates a retailer and a supplier for this retailer.
 *   It calls some methods on the retailer and supplier to cause
 *   various actions to take place.  The user is asked to enter
 *   input as the program runs.
 *
 *   The process is repeated for another retailer and supplier for
 *   this retailer that are created in the second half of the main
 *   method.  Again, methods are called to cause this supplier and
 *   retailer to perform various actions.  User input is accepted.

 *   This class currently reads input from the keyboard and displays
 *   output on the screen.  The only change that you'll need to make to
 *   this class is at the very end once you are sure your program is
 *   working, and you are ready to create your output file.  Unlike
 *   the last assignment, we will not be copying and pasting the output
 *   into a file.  You must modify this class to make the input come
 *   from a file called "input1.txt" and the output go to a file called
 *   "output1.txt".  Then print and submit the output file.
 *
 * -- A. Hunter, May 2001 */

import java.io.*;

public class AssignmentOne {

	// Declare variables for reading input and writing output.
	private static BufferedReader in;
	private static PrintStream out;

	// main: The main method is where the program starts executing.
	// It creates references to supplier and retailer objects and
	// calls various methods on them.
	public static void main (String[] args) throws IOException {

		/* --- You will need to modify this section only. --- */

		/* --- START OF SECTION TO MODIFY --- */

		// Input is from the keyboard.
		in = new BufferedReader (new InputStreamReader (System.in));

		// Uncomment this statement and comment out the statement
		// above it, to have your input read from a file named
		// "input1.txt" instead.
		//in = new BufferedReader (new FileReader ("input1.txt"));

		// Uncomment these two statements to send the output
		// of the program to a file called "output1.txt" instead.
		//out = new PrintStream (new FileOutputStream
		//	(new File ("output1.txt")));
		//System.setOut (out);

		/* --- END OF SECTION TO MODIFY --- */

		// --- Display Information about the program. --- */
		System.out.println ("SAND SALES PROGRAM\n");

		System.out.println ("Note: all sand amounts are in kilograms " +
			"and all money amounts ");
		System.out.println ("  are in dollars.  Do not use any amounts " +
			"that have decimal points.\n");

		/* --- Create retailer "The Sand Man" whose supplier is a
		   non-profit company called "The Pile". --- */

		// Create 'sandMan', which is a reference to a new Retailer
		// named "The Sand Man", who has no sand, but has $500
		// and charges a retail sand cost of $20/kg.
		System.out.println ("---");
		System.out.println ("Creating new retailer 'The Sand Man' " +
			"with $500 and a sand cost of");
		System.out.println ("  $20/kg.  This retailer currently has no sand.");
		Retailer sandMan = new Retailer ("The Sand Man", 500, 20);

		// Create a new non-profit supplier, called "The Pile", that
		// has a sand cost of $10/kg, and that has no restocking fee.
		System.out.println ("Creating new non-profit supplier " +
			"called 'The Pile' that has a sand");
		System.out.println ("  cost of $10/kg.  This supplier " +
			"currently has no money or sand.");
		Supplier thePile = new Supplier ("The Pile", "Non-Profit", 10);

		// Read in the starting amounts of money and sand for supplier.
		System.out.println();
		System.out.print ("Enter starting money for 'The Pile': $");
		int money = Integer.parseInt (in.readLine());
		thePile.addMoney (money);

		System.out.print ("Enter starting amount of sand for 'The Pile': ");
		int sand = Integer.parseInt (in.readLine());
		thePile.addSand (sand);

		// Set "The Sand Man" retailer so that its supplier is "The Pile".
		sandMan.setSupplier (thePile);

		// Read in the amount of sand that "The Sand Man" orders
		// from its supplier, "The Pile".
		System.out.println();
		System.out.print ("Enter amount of sand 'The Sand Man'" +
			" orders from supplier 'The Pile': ");
		sand = Integer.parseInt (in.readLine());

		// As long as 'The Sand Man' has enough money to order
		// this amount of sand, proceed to order the sand.
		// First print true/false to indicate if there is enough.
		boolean enoughAvailable = sandMan.canRestock (sand);
		System.out.println ("Can 'The Sand Man' order " + sand +
					" kg of sand? " + enoughAvailable);

		// If there is enough available, purchase 'sand' kg of sand
		// from "The Pile", otherwise print an error message.
		if (enoughAvailable) {
			System.out.println ("'The Sand Man' buys " + sand +
				" kg of sand from 'The Pile'.");
			sandMan.restock (sand);
		} else {
			System.out.println ("'The Sand Man' does not have enough " +
				"money to order " + sand + " kg. Order rejected.");
		}

		// Read in the amount of sand that consumer 1 would like to
		// purchase from retailer, "The Sand Man".
		System.out.println();
		System.out.print ("Enter amount of sand consumer 1 " +
			"purchases from retailer 'The Sand Man': ");
		sand = Integer.parseInt (in.readLine());

		// As long as 'The Sand Man' has enough sand available,
		// proceed to allow consumer 1 to buy the sand.
		// First print true/false to indicate if there is enough.
		enoughAvailable = sandMan.canSell (sand);
		System.out.println ("Can 'The Sand Man' sell " + sand +
					" kg of sand? " + enoughAvailable);

		// If there is enough available, "The Sand Man" can sell
		// 'sand' kg of sand, otherwise print an error message.
		if (enoughAvailable) {
			System.out.println ("'The Sand Man' sold " + sand +
				" kg of sand to consumer 1.");
			sandMan.sale (sand);
		} else {
			System.out.println ("'The Sand Man' does not have " +
				sand + " kg of sand available. Sale rejected.");
		}

		// Print out summary information for "The Sand Man".
		System.out.println ("\nSummary Information:");
		System.out.println (sandMan.summary());
		System.out.println ();

		/* --- Create retailer "Dirt Is Us" whose supplier is a
		   for-profit company called "We Do Dirt". --- */

		// Create 'isDirt', which is a reference to a new Retailer
		// named "Dirt Is Us", who has 500 kg sand, $1000,
		// and charges a retail sand cost of $25/kg.
		System.out.println ("---");
		System.out.println ("Creating new retailer 'Dirt Is Us' " +
			"with 500 kg sand, $1000,");
		System.out.println ("  and a sand cost of $25/kg.");
		Retailer isDirt = new Retailer ("Dirt Is Us", 1000, 500, 25);

		// Create a new for-profit supplier, called "Sand Inc", that
		// has a sand cost of $15/kg, and that has a $10 restocking fee.
		System.out.println ("Creating new for-profit supplier " +
			"called 'Sand Inc' with sand cost of ");
		System.out.println ("  $15/kg and $10 fee.  This supplier " +
			"currently has no money or sand.");
		Supplier doDirt = new Supplier ("Sand Inc", "For-Profit", 15, 10);

		// Read in the starting amounts of money and sand for supplier.
		System.out.println();
		System.out.print ("Enter starting money for 'Sand Inc': $");
		money = Integer.parseInt (in.readLine());
		doDirt.addMoney (money);

		System.out.print ("Enter starting amount of sand for 'Sand Inc': ");
		sand = Integer.parseInt (in.readLine());
		doDirt.addSand (sand);

		// Set "Dirt Is Us" retailer so that its supplier is "Sand Inc".
		isDirt.setSupplier (doDirt);

		// Read in the amount of sand that "Dirt Is Us" orders
		// from its supplier, "Sand Inc".
		System.out.println();
		System.out.print ("Enter amount of sand 'Dirt Is Us'" +
			" orders from supplier 'Sand Inc': ");
		sand = Integer.parseInt (in.readLine());

		// As long as 'Dirt Is Us' has enough money to order
		// this amount of sand, proceed to order the sand.
		// First print true/false to indicate if there is enough.
		enoughAvailable = isDirt.canRestock (sand);
		System.out.println ("Can 'Dirt Is Us' order " + sand +
					" kg of sand? " + enoughAvailable);

		// If there is enough available, purchase 'sand' kg of
		// sand from "Sand Inc", otherwise print an error message.
		if (enoughAvailable) {
			System.out.println ("'Dirt Is Us' buys " + sand +
				" kg of sand from 'Sand Inc'.");
			isDirt.restock (sand);
		} else {
			System.out.println ("'Dirt Is Us' does not have enough " +
				"money to order " + sand + " kg. Order rejected.");
		}

		// Read in the amount of sand that consumer 2 would like to
		// purchase from retailer, "Dirt Is Us".
		System.out.println();
		System.out.print ("Enter amount of sand consumer 2 " +
			"purchases from retailer 'Dirt Is Us': ");
		sand = Integer.parseInt (in.readLine());

		// As long as 'Dirt Is Us' has enough sand available,
		// proceed to allow consumer 2 to buy the sand.
		// First print true/false to indicate if there is enough.
		enoughAvailable = isDirt.canSell (sand);
		System.out.println ("Can 'Dirt Is Us' sell " + sand + 
			" kg of sand? " + enoughAvailable);

		// If there is enough available, "Dirt Is Us" can sell
		// 'sand' kg of sand, otherwise print an error message.
		if (enoughAvailable) {
			System.out.println ("'Dirt Is Us' sold " + sand +
				" kg of sand to consumer 2.");
			isDirt.sale (sand);
		} else {
			System.out.println ("'Dirt Is Us' does not have " +
				sand + " kg of sand available. Sale rejected.");
		}

		// Print out summary information for "Dirt Is Us".
		System.out.println ("\nSummary Information:");
		System.out.println (isDirt.summary());
		System.out.println ();
	}
}
