Manhattanisgr8

Members
  • Content count

    60
  • Joined

  • Last visited

Community Reputation

487 Neutral

About Manhattanisgr8

  • Rank
    Member
  1. Help with ArrayList Java

    Thanks guys. The combination of using ArrayList<Object> and then casting the Object to a specific class type worked. You guys saved me several hours of coding. I can't thank you guys enough.
  2. Help with ArrayList Java

    Thank you megadan, I now have it reading the ArrayList using Class<?>. Now my problem is adding elements to an ArrayList<Class> from ArrayList<Class<?>>.   Example: ArrayList<Moves> moves; ArrayList<Class<?>> importedMoves; for (int x = 0; x < importedMoves.size(); x++){ moves.add(importedMoves.get(x)); Is there a way around this?
  3. Help with ArrayList Java

    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?
  4. 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.
  5. Open a new panel on top of another

    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.
  6. Need help with installation of program

    Forgot to mention that I am targeting Windows users. In the manual, I am going to instruct the users how to use it in Linux via makefile due to the lack of information I was finding on the internet for packaging distros.
  7. Need help with installation of program

    I didn't even think about the permissions. I was having the installer put it into the program files folder in the windows directory. Could I perhaps have the program write to the public folder in users? If so would the following statement be correct?: outFile.open("C:\\Users\\Public\\Documents\\ProductManager\\inventory.txt");
  8. C++ Need help with fstream

      Thanks Alvaro, I fixed the problem I was having by consuming the newline character with cin.ignore() before it looped back around to the getline() statement.
  9. 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; }
  10. 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] #include <cstddef> #include "Product.h" using namespace std; class LinkedProductInventory { private: struct ListNode { Product product; ListNode *next; }; void sort(); ListNode *head; int count; public: LinkedProductInventory(); //~LinkedProductInventory(); void appendNode(Product); void insertNode(Product); void deleteNode(Product); void displayList(); void writeToFile(); }; ,[/spoiler] LinkedProductInventory.cpp [spoiler] #include <iostream> #include <fstream> #include <string> #include "LinkedProductInventory.h" using namespace std; LinkedProductInventory::LinkedProductInventory() {     head = NULL;     count = 0;     ifstream inFile;     inFile.open("inventory.txt");     if (inFile)     {         Product product;         string productName, locator;         int quantity;         double price;         //while ((inFile.peek() != '\n') && (getline(inFile, productName)))         //while (getline(inFile, productName))         while (inFile.getline(productName,productName.length))         {             //getline(inFile, productName);             inFile >> locator;             inFile >> quantity;             inFile >> price;             product.setProductName(productName);             product.setLocator(locator);             product.setQuantity(quantity);             product.setPrice(price);             inFile.clear();             appendNode(product);         }     }     else         inFile.close(); } void LinkedProductInventory::appendNode(Product product) {     ListNode *newNode; // To point to the new node     // Create a new node and store data in it     newNode = new ListNode;     newNode->product = product;     newNode->next = NULL;     //If empty, make head point to new node     if (head == NULL)     {         head = newNode;     }     else     {         ListNode *p = head; // To move through the list         //traverse list to find last node         while (p->next != NULL)             p = p->next;         // now p points to last node         // make last node point to newNode         p->next = newNode;     }     count++; //    sort(); } void LinkedProductInventory::displayList() {     ListNode *p;     // start at head of the list     p = head;     cout << "\nLocation    Quantity    Price            Name" << endl;     cout << "--------------------------------------------------------------------------" << endl;     // while p points to something, continue     while (p)     {         //Display the value in the current node         cout << p->product.getLocator() << "\t";         cout << p->product.getQuantity() << "\t\t";         cout << p->product.getPrice() << "\t\t";         cout << p->product.getProductName() << endl;         //Move to the next node         p = p->next;     }     cout << endl; } //LinkedProductInventory::~LinkedProductInventory() //{ //    ListNode *p; // traversal pointer //    ListNode *n; // saves the next node //    p = head; // //    while (p) //    { //        n = p->next; //save the next //        delete p; // delete current //        p = n; //    } //} void LinkedProductInventory::deleteNode(Product product) {     ListNode *p = head; // to traverse list     ListNode *n = p; // trailing node pointer     // skip nodes not equal to product, stop at last     while (p && p->product.getProductName() != product.getProductName() && p->product.getLocator() != product.getLocator())     {         n = p;         p = p->next;     }     //p not null: product was found, set links + delete     if (p)     {         if (p == head)         {             head = p->next;             delete p;         }         else         {             n->next = p->next;             delete p;         }     }     count--; } void LinkedProductInventory::writeToFile() {     ofstream outFile;     outFile.open("inventory.txt");     ListNode *p = head;     while (p)     {         outFile << p->product.getProductName() << endl;         outFile << p->product.getLocator() << endl;         outFile << p->product.getQuantity() << endl;         outFile << p->product.getPrice() << endl;         p = p->next;     }     outFile.close(); } void LinkedProductInventory::sort() {     ListNode *p = head; //used to traverse     ListNode *temp;     for (int x = 0; x < count; x++)     {         while (p)         {             if (p->product.getProductName() > p->next->product.getProductName())             {                 temp = p->next;                 p->next = p;                 p = temp;             }             else                 continue;         }     } } [/spoiler] Product.h [spoiler] // File Name: Product.h // // Author: Travis Alt // Date: 3/31/2014 // Assignment Number: 5 // CS 2308.256 Spring 2014 // Instructor: Jill Seaman // // Header file for Product.cpp #include <string> using namespace std; class Product { private:     string productName; // name of the product     string locator; // location of the product     int quantity; // number of products     double price; // price of the products public:     Product();     Product(string, int, double, string);     string getProductName();     string getLocator();     int getQuantity();     double getPrice();     void setProductName(string);     void setLocator(string);     void setQuantity(int);     void setPrice(double);     bool operator<(Product);     bool operator>(Product);     bool operator==(Product); }; [/spoiler] Product.cpp [spoiler] // File Name: Product.cpp // // Author: Travis Alt // Date: 3/31/2014 // Assignment Number: 5 // CS 2308.256 Spring 2014 // Instructor: Jill Seaman // // A representation of a Product object that stores // the product name, quantity, price, and location #include "Product.h" using namespace std; //*********************************************************** // Product: Constructor of Product that takes no arguments // // returns nothing //*********************************************************** Product::Product() {     productName = " ";     locator = " ";     quantity = 0;     price = 0.00; } //*********************************************************** // Product: Constructor of Product that sets each member variable // // locator: location of the product // quantity: the number of products // price: the price of the products // productName: name of the product // returns nothing //*********************************************************** Product::Product(string locator, int quantity, double price, string productName) {     this->productName = productName;     this->locator = locator;     this->quantity = quantity;     this->price = price; } //*********************************************************** // getProductName: gets the name of the product stored in the // member variable // // returns member variable productName //*********************************************************** string Product::getProductName() {     return productName; } //*********************************************************** // getLocator: gets the location of the product stored in the // member variable // // returns member variable locator //*********************************************************** string Product::getLocator() {     return locator; } //*********************************************************** // getQuantity: gets the number of the products stored in the // member variable // // returns member variable quantity //*********************************************************** int Product::getQuantity() {     return quantity; } //*********************************************************** // getPrice: gets the price of the product stored in the // member variable // // returns member variable price //*********************************************************** double Product::getPrice() {     return price; } //*********************************************************** // setProductName: sets the name of the product stored in the // member variable // // returns void //*********************************************************** void Product::setProductName(string productName) {     this->productName = productName; } //*********************************************************** // setLocator: sets the location of the product stored in the // member variable // // returns void //*********************************************************** void Product::setLocator(string locator) {     this->locator = locator; } //*********************************************************** // setQuantity: sets the number of products stored in the // member variable // // returns void //*********************************************************** void Product::setQuantity(int quantity) {     this->quantity = quantity; } //*********************************************************** // setPrice: sets the price of the product stored in the // member variable // // returns void //*********************************************************** void Product::setPrice(double price) {     this->price = price; } //*********************************************************** // operator<: An overloaded operator that checks if the product // name is less than product on the right // // right: the product on the right side of the argument // returns true if the product on the left is less than the product // on the right //*********************************************************** bool Product::operator<(Product right) {     if (productName < right.getProductName())     {         return true;     }     else         return false; } //*********************************************************** // operator>: An overloaded operator that checks if the product // name is greater than product on the right // // right: the product on the right side of the argument // returns true if the product on the left is greater than the product // on the right //*********************************************************** bool Product::operator>(Product right) {     if (productName > right.getProductName())     {         return true;     }     else     {         return false;     } } //*********************************************************** // operator==: An overloaded operator that checks if the product // name is equal to than product on the right // // right: the product on the right side of the argument // returns true if the product on the left is equal to than the product // on the right //*********************************************************** bool Product::operator==(Product right) {     if (productName == right.getProductName())     {         return true;     }     else     {         return false;     } } [/spoiler] ProductDriver.cpp [spoiler] #include <iostream> #include <string> #include "LinkedProductInventory.h" using namespace std; void readFromFile(); Product *addProduct(); Product *removeProduct(); Product *adjustQuantity(); string displayProduct(); int main() {     const int ADD_NEW = 1,         REMOVE = 2,         ADJUST_QUANTITY = 3,         DISPLAY_INFO = 4,         DISPLAY_INVENTORY = 5,         QUIT = 6;     int userSelection;     LinkedProductInventory pi;     do     {         cout << "Manage Inventory Menu" << endl;         cout << "1. Add product" << endl;         cout << "2. Remove product" << endl;         cout << "3. Adjust Quantity of A Product" << endl;         cout << "4. Display A Product" << endl;         cout << "5. Display Inventory" << endl;         cout << "6. Quit The Program" << endl;         cout << "\nEnter your choice: ";         cin >> userSelection;         string productName, locator;         int quantity;         double price;         Product product;         switch (userSelection)         {         case 1:                          cout << endl;             cout << "Please enter the product's name: ";             cin.ignore(100, '\n');             getline(cin, productName);             product.setProductName(productName);             cout << "Please enter the product's location: ";             cin >> locator;             product.setLocator(locator);             cout << "Please enter the quantity of the product (DO NOT INCLUDE SPACES): ";             cin >> quantity;             product.setQuantity(quantity);             cout << "Please enter the price of the product: ";             cin >> price;             product.setPrice(price);             pi.appendNode(product);             cout << endl;             break;         case 2: pi.deleteNode(*removeProduct());             cout << endl;             break;         case 3: adjustQuantity();             cout << endl;             break;         case 4: displayProduct();             cout << endl;             break;         case 5: pi.displayList();             cout << endl;             break;         case 6: pi.writeToFile();             break;         }     } while (userSelection != QUIT);      } Product *addProduct() {     string productName;     string locator;     int quantity;     double price;     cout << endl;     cout << "Please enter the product's name: ";     cin.ignore(100, '\n');     getline(cin, productName);     //product.setProductName(productName);     cout << "Please enter the product's location: ";     cin >> locator;     //cin.ignore(100, '\n');     //getline(cin, locator);     //product.setLocator(locator);     cout << "Please enter the quantity of the product (DO NOT INCLUDE SPACES): ";     cin >> quantity;     //product.setQuantity(quantity);     cout << "Please enter the price of the product: ";     cin >> price;     //product.setPrice(price);     Product product(locator, quantity, price, productName);     Product *p = &product;     return p; } Product *removeProduct() {     string productName, locator;     cout << endl;     cout << "Please enter the product's name to remove: ";     cin.ignore(100, '\n');     getline(cin, productName);     cout << "Please enter the location of the product: ";     cin.ignore(100, '\n');     getline(cin, locator);          Product product(locator, 0, 0.0, productName);     Product *p = &product;     return p; } // may have to redo this Product *adjustQuantity() {     Product product;     string productName, locator;     int newQuantity;     cout << endl;     cout << "Enter the name of the product: ";     cin.ignore(100, '\n');     getline(cin, productName);     cout << "Enter the location of the product: ";     cin.ignore(100, '\n');     getline(cin, locator);     cout << "Enter the new quantity of the product: ";     cin >> newQuantity;     product.setProductName(productName);     product.setLocator(locator);     product.setQuantity(newQuantity);     Product *p = &product;     return p; } string displayProduct() {     Product product;     string productName;     cout << endl;     cout << "Enter the product name: ";     cin.ignore(100, '\n');     getline(cin, productName);     return productName; } [/spoiler]   EDIT: Here is what the txt file looks like: Samsung Livingroom 2 12.99 Test this Livingroom 4 56.88
  11. having a couple of problems with Windown Forms

    I figured it out, thank you for your help and patience.
  12. having a couple of problems with Windown Forms

    Which lines do I change? All the initializing of the buttons are in the classxx.deigner.cs portion of the panel.
  13. having a couple of problems with Windown Forms

    How would I go around this? I tried to implement your second example, however, instead of setting the list, I tried to set the visibility of another panel, and that didn't work.  
  14. having a couple of problems with Windown Forms

    Form 1: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace InterfacesTest { public partial class Form1 : Form { public Panel1 panel1 = new Panel1(); public ListboxPanel listboxPanel = new ListboxPanel(); public AddItems addItems = new AddItems(); public Form1() { InitializeComponent(); this.Controls.Add(panel1); this.Controls.Add(listboxPanel); this.Controls.Add(addItems); panel1.Link = listboxPanel.Link; listboxPanel.Link = panel1; } } } panel1 - first panel the user sees using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace InterfacesTest { public partial class Panel1 : UserControl { public Panel1 Link; public Panel1() { InitializeComponent(); this.Visible = false; button1.Click += delegate { if (Link != null) Link.Visible = !Link.Visible; }; } } } ListboxPanel using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace InterfacesTest { public partial class ListboxPanel : UserControl { public ListboxPanel Link; public ListboxPanel() { InitializeComponent(); this.Visible = false; button1.Click += delegate { if (Link != null) Link.Visible = !Link.Visible; }; } } } AddPanel - will be used to add items to listbox in ListboxPanel using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace InterfacesTest { public partial class AddItems : UserControl { public AddItems Link; public AddItems() { InitializeComponent(); this.Visible = false; button1.Click += delegate { if (Link != null) Link.Visible = !Link.Visible; }; } } } Hope this helps, didn't add the other .designer.cs of the classes that VS uses for the design
  15. having a couple of problems with Windown Forms

    Ok, created a new project and tried to implement the link controls where they all use the same namespace. However, now I am getting this:   Error    1    Cannot implicitly convert type 'InterfacesTest.ListboxPanel' to 'InterfacesTest.Panel1'    c:\users\travis\google drive\visual studio 2013\projects\interfacestest\interfacestest\form1.cs    26   in Form1.