package database_comm;
import java.sql.ResultSet;
import java.sql.SQLException;

import retail.RetailHelper;

public class ItemDatabase
{

	//Debug item class for SQL connection
	//TODO remove
	public static class Item
	{
		public String barcode;
		public String description;
		public int price;
		//public int cost;

		public String toString()
		{
			return barcode + ", " + description + ", " + price;
		}
	}

	//Returns an item with the given barcode from the database. null if not found
	public static Item getItemFromBarcode(String barcode)
	{
		//Create and execute query to find item with matching barcode
		DatabaseConnection connection = new DatabaseConnection();
		String query = "SELECT * FROM ITEMS WHERE BARCODE = '" + barcode + "'";
		ResultSet rs = connection.executeQuery(query);

		//Set up our return item with null by default
		Item returnItem = null;
		
		try
		{
			//Check if our query got any results
			if (rs.next())
			{
				//If so, create item instance from result data
				returnItem = new Item();
				returnItem.barcode = barcode;
				returnItem.description = rs.getString("DESCR");
				returnItem.price = (int)(rs.getFloat("PRICE") * 100);
			}
			else
			{
				//If not, print that the item was not found
				System.out.println("Item with barcode " + barcode + " not found");
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		//Close the connection and return our results
		connection.close();		
		return returnItem;

	}
	
	
	//Adds the given item to the database, returns true if successful
	public static boolean addItemToDatabase(Item item)
	{
		//Create database connection
		DatabaseConnection connection = new DatabaseConnection();
		
		//Set up query to insert into ITEMS table and execute it
		//TODO deal with placeholder ITEM_NO and COST values
		String query = "INSERT INTO ITEMS (ITEM_NO, BARCODE, DESCR, PRICE, COST) "
				+ "VALUES ('" + item.barcode + "', '" + item.barcode + "', '" + item.description + "', " + getCashString(item.price) + ", '1.00')";
		//System.out.println(query);
		connection.execute(query);
		
		
		return false;
	}
	
	//Placeholder get cash string
	//TODO remove
	public static String getCashString(int amount)
	{
		int cents = amount % 100;
		int dollars = amount - cents;
		dollars /= 100;
		return dollars + "." + cents;
	}

}

@@ -101,16 +101,36 @@
 lHelper;
+%0Aimport retail.Item;
 %0A%0Apublic
@@ -156,281 +156,8 @@
 %0A%7B%0A%0A
-%09//Debug item class for SQL connection%0A%09//TODO remove%0A%09public static class Item%0A%09%7B%0A%09%09public String barcode;%0A%09%09public String description;%0A%09%09public int price;%0A%09%09//public int cost;%0A%0A%09%09public String toString()%0A%09%09%7B%0A%09%09%09return barcode + %22, %22 + description + %22, %22 + price;%0A%09%09%7D%0A%09%7D%0A%0A
 %09//R
@@ -768,43 +768,16 @@
 tem(
-);%0A%09%09%09%09returnItem.barcode = 
 barcode
+)
 ;%0A%09%09
@@ -785,25 +785,28 @@
 %09returnItem.
-d
+setD
 escription =
@@ -803,19 +803,17 @@
 cription
- = 
+(
 rs.getSt
@@ -825,16 +825,17 @@
 %22DESCR%22)
+)
 ;%0A%09%09%09%09re
@@ -847,16 +847,17 @@
 tem.
-p
+setP
 rice
- = 
+(
 (int
@@ -885,16 +885,73 @@
 ) * 100)
+);%0A%09%09%09%09returnItem.setTaxable(rs.getBoolean(%22IS_TAXABLE%22))
 ;%0A%09%09%09%7D%0A%09
@@ -1654,16 +1654,28 @@
 CE, COST
+, IS_TAXABLE
 ) %22%0A%09%09%09%09
@@ -1687,39 +1687,44 @@
 LUES ('%22 + item.
-b
+getB
 arcode
+()
  + %22', '%22 + item
@@ -1724,23 +1724,28 @@
  + item.
-b
+getB
 arcode
+()
  + %22', '
@@ -1753,17 +1753,20 @@
  + item.
-d
+getD
 escripti
@@ -1767,17 +1767,23 @@
 cription
- 
+()%0A%09%09%09%09
 + %22', %22 
@@ -1788,45 +1788,79 @@
 %22 + 
-getCashString(item.price) + %22, '1.00'
+item.getPriceString() + %22, '1.00', %22+ (item.isTaxable() ? %221%22 : %220%22) +%22
 )%22;%0A
@@ -1892,16 +1892,37 @@
 ery);%0A%09%09
+boolean successful = 
 connecti
@@ -1949,235 +1949,47 @@
 %09%0A%09%09
-%0A%09%09
-return false;%0A%09%7D%0A%09%0A%09//Placeholder get cash string%0A%09//TODO remove%0A%09public static String getCashString(int amount)%0A%09%7B%0A%09%09int cents = amount %25 100;%0A%09%09int dollars = amount - cents;%0A%09%09dollars /= 100;%0A%09%09return dollars + %22.%22 + cents
+connection.close();%0A%09%09return successful
 ;%0A%09%7D

Implement Item class and taxable in ItemDatabase