Jump to content

  • Log In with Google      Sign In   
  • Create Account


Manhattanisgr8

Member Since 13 Nov 2007
Offline Last Active Jul 22 2014 08:06 PM
-----

Topics I've Started

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


having a couple of problems with Windown Forms

14 January 2014 - 08:53 PM

I have been having two problems that have been nagging with Windows Forms. One being the visibility modifier and updating a list box. To start off, I had trouble getting the menu strip highlight colors to change to what I wanted them to be. Fixed that by creating a new class that inherits from MenuStrip and then adding it to the main form. The program (more like game) is using several different panels to display different information and navigation controls. So instead of creating a bunch of different panels in the main form and setting the visibility to false until a button is pressed (putting a bunch of code in one place), I decided to make each panel a user control class (which automatically inherits UserControl). My problem with the visibility modifier is that I had to add each user control to the main form as public static in order for the other controls to set the visibility modifiers. Is there another way to implement something so I don't have to add all the user controls to the main form as public static?

 

The other problem I have is updating a listbox from one user control when I press a button on a different user control. Basically I have a user control which displays info from a list on a listbox with add, modify, delete buttons. When the user clicks add, they are taken to another user control to input the info they want. However, when they click save, I want to update the listbox with the added info. I tried to make the listbox static and have tried both class.listbox.DataSource = null then class.listbox.DataSource = list. That didn't work. Then I tried class.listbox.Refresh(). That didn't work as well.

 

I was thinking about maybe trying to use and Interface, but I am not exactly sure how they work or how to use them. To me, they seem like just a way of organizing like classes in order to save time typing a bunch of methods, but don't interact with each other.

 

Any help would greatly be appreciated.


Need help creating new objects based on user input

07 January 2014 - 09:25 PM

I am playing around with windows forms and was wondering if there was a way to create a new object based on user input. Basically I want to be able to create a new object with what ever the user puts into the make textbox, add it to a List<Car>() and then display the model in the listbox. Just need to know if there is a way to make an object without knowing the name (such as Car xxx = new Car(make, model, year)) or knowing how many objects will be created and be able to pass in parameters. Below is the code for the simple car class and a screen capture of the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CreateNewObjectsTest
{
    class Car
    {
        private string make;
        private string model;
        private int year;

        public Car(string make, string model, int year)
        {
            this.make = make;
            this.model = model;
            this.year = year;
        }

        public string GetModel()
        {
            return model;
        }
    }
}


PARTNERS