Jump to content

  • Log In with Google      Sign In   
  • Create Account


Manhattanisgr8

Member Since 13 Nov 2007
Offline Last Active Aug 16 2014 03:12 PM

Topics I've Started

Help with ArrayList Java

15 August 2014 - 05:52 PM

I am currently working on a text based game using swing. In it, I have the user create a game world (essentially just a new folder name and then some serialized files in it). I a trying to make a way to import what data they want from one world to another. My problems is, instead of making a new popup for each object, I want to create just one and pass a string to it in order to get what serialized object for the popup to get. I have tried to turn the string into a class name via Class className = Class.forName("com.package.class") method, however, when I try to input that into ArrayList<className> arrayList, it tells me className cannot be resolved to a type. Is there anyway i can achieve this? Or am I going to have to make a popup for each object I want to have imported?


How to use mkdir in java without knowing full path

05 August 2014 - 01:43 AM

I am currently making a text based game using swing. I plan on using a wrapper to distribute the program. The question I have is; How do I make a new directory without knowing where the user installed the game? Basically I want to have something like this:

 

GameFolder\Data\(This is where I would want to make the new directory with the name the end user specifies)

 

Any help or direction would greatly be appreciated.


Open a new panel on top of another

22 July 2014 - 08:07 PM

I am currently making a text based game using swing and I am running into a problem. I currently have a JFrame and 2 different panels(much more will be added). However, I can't figure out how to switch between the two panels. The first panel has a JList for a list of characters with some buttons at the bottom. One button in particular is an Add button. When the user clicks on the button, I want the first panel not to be visible, and for the second panel to become visible. Once they click save on the second panel, I want the second panel not to be visible, and the first panel to become visible. I am using a method to pass the character information back to the first panel to add the new character to the list. Just having trouble displaying the two panels. I am trying not to make the classes static. With a bunch on other panels to add and display, I want to be able to dispose of the panels not being used to free up memory. Any advice would greatly be appreciated.


Need help with installation of program

21 April 2014 - 03:09 PM

I have created a simple console application that allows a user to manage a product inventory. After the user quits, it saves the data into a txt file. When the user opens the application again, it reads the information from the text file. I am using Inno to "distribute" the application. My question is, the program works, however, after installing it, it doesn't create the txt file, thus it can't read from it. Could this be a simple path finding problem? If so, how can I fix this? Below is the code in question:

side note: this is an application I developed but have to write a manual for it for my technical writing class. Thus, the reason for using Inno.

// File Name: ProductInventory.cpp
//
// Author: Travis Alt
// Date: 3/31/2014
// Assignment Number: 5
// CS 2308.256 Spring 2014
// Instructor: Jill Seaman
//
// An object that holds an array of Products. You can
// add or remove a product, sort the array by product name,
// get the total quantity in the inventory, or display the
// inventory in a table format.

#include <iostream>
#include <fstream>
#include "ProductInventory.h"

using namespace std;

ProductInventory::ProductInventory()
{
	count = 0;
	capacity = 0;

	ifstream inFile;
	inFile.open("inventory.txt");

	if (inFile.is_open())
	{
		Product product;
		string productName, locator;
		int quantity;
		double price;

		while (getline(inFile, productName))
		{
			//getline(inFile, productName);
			inFile >> locator;
			inFile >> quantity;
			inFile >> price;

			product.setProductName(productName);
			product.setLocator(locator);
			product.setQuantity(quantity);
			product.setPrice(price);
			inFile.ignore(50, '\n');

			addProduct(product);
		}
	}
	else
	{
		inFile.close();
		ofstream outFile;
		outFile.open("inventory.txt");
		outFile.close();
	}
}

//***********************************************************
// ProductInventory: constructor of the product inventory
//
// size: size of the array
// returns nothing
//***********************************************************
ProductInventory::ProductInventory(int size)
{
	//inventory = new vector<Product>;
	count = 0;
	capacity = size;

	ifstream inFile;
	inFile.open("inventory.txt");

	if (inFile.is_open())
	{
		Product product;
		string productName, locator;
		int quantity;
		double price;

		while ((inFile.peek() != '\n') && (getline(inFile, productName)))
		//while (getline(inFile, productName))
		{
			inFile >> locator;
			inFile >> quantity;
			inFile >> price;

			product.setProductName(productName);
			product.setLocator(locator);
			product.setQuantity(quantity);
			product.setPrice(price);

			addProduct(product);
		}
	}
	else
	{
		inFile.close();
		ofstream outFile;
		outFile.open("inventory.txt");
		outFile.close();
	}
}

//***********************************************************
// addProduct: adds a new product to the array. If the array is full
// it calls resize to double the size of the array.
//
// product: the product to be added
// returns false if there is a product with the same name and location,
// otherwise returns true if the product was added
//***********************************************************
bool ProductInventory::addProduct(Product product)
{
	/*if (count == capacity)
	{
		resize();
	}*/
	for (int x = 0; x < count; x++)
	{
		if (inventory[x].getProductName() == product.getProductName() &&
			inventory[x].getLocator() == product.getLocator())
		{
			return false;
		}

	}
	inventory.push_back(product);
	sortInventory();
	writeToFile();
	count++;
	return true;
}

//***********************************************************
// removeProduct: removes the product from the list by hiding it
// at the end of the array until it is replaced by another product
//
// productName: name of the product to be removed
// locator: the location of the product to be removed
// return true if the product was removed, else return false if it
// can't find the product and location.
//***********************************************************
bool ProductInventory::removeProduct(Product product)
{
	int index = 0; // index of where the product is in the array
	for (int x = 0; x < count; x++)
	{
		if (product.getProductName() == inventory[x].getProductName() && product.getLocator() == inventory[x].getLocator())
		{
			index = x;

			inventory.erase(inventory.begin() + index);
			count--;
			return true;
		}
	}
	return false;
}

//***********************************************************
// showInventory: displays the current inventory in the form of
// a table
//
// returns void
//***********************************************************
void ProductInventory::showInventory()
{
	sortInventory();

	cout << "\nLocation	Quantity	Price	        Name" << endl;
	cout << "--------------------------------------------------------------------------" << endl;

	for (int x = 0; x < count; x++)
	{
		cout << inventory[x].getLocator() << "\t\t";
		cout << inventory[x].getQuantity() << "\t\t";
		cout << inventory[x].getPrice() << "\t\t";
		cout << inventory[x].getProductName() << endl;
	}
}

//***********************************************************
// getTotalQuantity: adds up the quantity of each product in the array
//
// returns total number of products in the array
//***********************************************************
int ProductInventory::getTotalQuantity()
{
	int totalQuantity = 0; // initial size of the total quantity

	for (int x = 0; x < count; x++)
	{
		totalQuantity += inventory[x].getQuantity();
	}
	return totalQuantity;
}

//***********************************************************
// sortInventory: reorders the array by name from A-Z
//
// returns void
//***********************************************************
void ProductInventory::sortInventory()
{
	for (int i = 0; i < count - 1; i++)
	{
		for (int x = 0; x < count - 1; x++)
		{
			if (inventory[x] > inventory[x + 1])
			{
				Product temp = inventory[x + 1];
				inventory[x + 1] = inventory[x];
				inventory[x] = temp;
			}
			else
			{
				continue;
			}
		}
	}
}

//***********************************************************
// resize: doubles the current size of the array if there is
// not enough space to add more products
//
// returns void
//***********************************************************
void ProductInventory::resize()
{
	int newSize = capacity * 2;

	Product *newInventory = new Product[newSize];

	for (int x = 0; x < count; x++)
	{
		newInventory[x] = inventory[x];
	}

	capacity = newSize;
	//delete[] inventory;
	//inventory = newInventory;
}

void ProductInventory::writeToFile()
{
	ofstream outFile;

	outFile.open("inventory.txt");

	for (int x = 0; x < count; x++)
	{
		outFile << inventory[x].getProductName() << endl << inventory[x].getLocator() << endl << inventory[x].getQuantity() << endl << inventory[x].getPrice() << endl;
	}

	outFile.close();

	outFile.open("inventory_print.txt");

	outFile << "Location	Quantity	Price	        Name" << endl;
	outFile << "--------------------------------------------------------------------------" << endl;

	for (int x = 0; x < inventory.size(); x++)
	{
		outFile << inventory[x].getLocator() << "\t";
		outFile << inventory[x].getQuantity() << "\t\t";
		outFile << inventory[x].getPrice() << "\t\t";
		outFile << inventory[x].getProductName() << endl;
	}
	outFile.close();
}

bool ProductInventory::adjustQuantity(Product product)
{
	for (int x = 0; x < inventory.size(); x++)
	{
		if (inventory[x].getProductName() == product.getProductName() && inventory[x].getLocator() == product.getLocator())
		{
			inventory[x].setQuantity(product.getQuantity());
			return true;
		}
		else
			return false;
	}

	return false;
}

bool ProductInventory::adjustPrice(Product product)
{
	for (int x = 0; x < inventory.size(); x++)
	{
		if (inventory[x].getProductName() == product.getProductName() && inventory[x].getLocator() == product.getLocator())
		{
			inventory[x].setPrice(product.getPrice());
			return true;
		}
		else
			return false;
	}

	return false;
}

double ProductInventory::getTotalPrice()
{
	double totalPrice = 0.0;

	for (int x = 0; x < inventory.size(); x++)
	{
		totalPrice += inventory[x].getPrice();
	}

	return totalPrice;
}

C++ Need help with fstream

18 April 2014 - 09:20 PM

I need help with fstream and linked lists. Basically I am creating a basic inventory program for a school project where I have to write a manual for it. What I am trying to do is store products in a linked list. When the user quits, it writes stores the data into a txt file. When the user opens it again, I want it to read from the text file to re-populate the list. The problem I am having is reading a line with spaces. I can get it to read once, but when it goes to the next list, it doesn't copy all the information from the second product. Below is all the code I have so far and commented out what I have tried. The problem lays in LinkedProductInventory.cpp in the constructor.

LinkedProductInventory.h

Spoiler


LinkedProductInventory.cpp

Spoiler


Product.h

Spoiler


Product.cpp

Spoiler


ProductDriver.cpp

Spoiler

 

EDIT: Here is what the txt file looks like:

Samsung
Livingroom
2
12.99
Test this
Livingroom
4
56.88


PARTNERS