As the title of the question states, my program is not able to store an integer from a seperate text file provided. For instance, instead of printing the desired output:
"$45.00 17 2222 Chuck Taylor All Star"
It prints:
"$45.00 17 0 Chuck Taylor All Star"
So even though I provided the price (double), quantity (integer) and name (string), my program can store all of it except for the blasted ID number (integer). If anyone is willing to guide me towards the light on this, I would greatly appreciate it. My code is below, and the last bit of data at the very end is what "productin.txt" contains.
import java.io.*;
import java.text.NumberFormat;
public class Product
{
private String name;
private double price;
private int idNum;
private int quantity;
public Product (int id, String title, double cost, int total)
{
id = idNum;
name = title;
price = cost;
quantity = total;
}
//-----------------------------------------------------------------
// Returns the product's ID number.
//-----------------------------------------------------------------
public int getID()
{
return idNum;
}
//-----------------------------------------------------------------
// Returns the product's name.
//-----------------------------------------------------------------
public String getName()
{
return name;
}
//-----------------------------------------------------------------
// Returns the product's price.
//-----------------------------------------------------------------
public double getPrice()
{
return price;
}
//-----------------------------------------------------------------
// Adds units to the quantity in inventory.
//-----------------------------------------------------------------
public void addQuantity(int units)
{
quantity += units;
}
//-----------------------------------------------------------------
// Returns the quantity in inventory.
//-----------------------------------------------------------------
public int getQuantity()
{
return quantity;
}
//-----------------------------------------------------------------
// Subtracts units to the quantity in inventory.
//-----------------------------------------------------------------
public void subQuantity(int units)
{
quantity -= units;
}
//-----------------------------------------------------------------
// Returns a printablle version of the product object.
//-----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String description;
description = fmt.format(price) + "\t" + quantity + "\t";
description += idNum + "\t" + name;
return description;
}
}
import java.text.NumberFormat;
public class ProductCollection
{
private Product[] collection;
private int count;
private double totalCost;
//-----------------------------------------------------------------
// Constructor: Creates an initially empty collection.
//-----------------------------------------------------------------
public ProductCollection ()
{
collection = new Product[100];
count = 0;
totalCost = 0.0;
}
//-----------------------------------------------------------------
// Adds a product to the collection.
//-----------------------------------------------------------------
public void addProduct (int prodID, String prodName, double unitPrice, int prodQty)
{
if (count == collection.length)
increaseSize();
int index = findProduct(prodID);
if (index == -1)
{
collection[count] = new Product (prodID, prodName, unitPrice, prodQty);
totalCost += unitPrice;
count++;
}
else
System.out.println("Product was not added, a product having the ID number " + prodID
+ " was found.\n");
}
//-----------------------------------------------------------------
// Increases the size of the product to the collection.
//-----------------------------------------------------------------
private void increaseSize ()
{
Product[] temp = new Product[collection.length * 2];
for (int prod = 0; prod < collection.length; prod++)
temp[prod] = collection[prod];
collection = temp;
}
//-----------------------------------------------------------------
// Finds product from collection.
//-----------------------------------------------------------------
public int findProduct(int prodID)
{
int index = -1;
for (int i = 0; i < count; i++)
if (collection[i].getID() == prodID)
index = i;
return index;
}
//-----------------------------------------------------------------
// Changes the price of the product from the collection.
//-----------------------------------------------------------------
public void changePrice(int prodID, double newPrice)
{
int index = findProduct(prodID);
if (index > -1)
collection[index].setPrice(newPrice);
else
System.out.println("The product having the ID number " + prodID
+ " was not found.\n");
}
//-----------------------------------------------------------------
// Deletes product from the collection.
//-----------------------------------------------------------------
public void deleteProduct(int prodID)
{
int index = findProduct(prodID);
if (index > -1)
{
for (int i = index; i < count; i++)
{
collection[i] = collection[i+1];
count--;
}
}
else System.out.println("The product having the ID number " + prodID
+ " was not found.\n");
}
//-----------------------------------------------------------------
// Buys product units to the collection.
//-----------------------------------------------------------------
public void addQty(int prodID, int prodQty)
{
int index = findProduct(prodID);
if (index > -1)
collection[index].addQuantity(prodQty);
else
System.out.println("The product having the ID number " + prodID
+ " was not found.\n");
}
//-----------------------------------------------------------------
// Sells product units from the collection.
//-----------------------------------------------------------------
public void subQty(int prodID, int prodQty)
{
int index = findProduct(prodID);
if (index > -1)
collection[index].subQuantity(prodQty);
else
System.out.println("The product having the ID number " + prodID
+ " was not found.\n");
}
//-----------------------------------------------------------------
// Displays product from the collection.
//-----------------------------------------------------------------
public void displayProduct(int prodID)
{
int index = findProduct(prodID);
if (index > -1)
System.out.println(collection[index]);
else
System.out.println("The product having the ID number " + prodID
+ " was not found.\n");
}
//-----------------------------------------------------------------
// Returns a report describing the Product collection.
//-----------------------------------------------------------------
public String createOutputFile()
{
String data = "";
for (int i = 0; i < count; i++)
{
data += collection[i].getID() + ",";
data += collection[i].getName() + ",";
data += collection[i].getPrice() + ",";
data += collection[i].getQuantity() + ",\n";
}
return data;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String report = "\n--------------------------------------------------\n\n";
report += "Collection of Converse\n\n";
report += "Number of products: " + count + "\n";
report += "Total cost: " + fmt.format(totalCost) + "\n";
report += "Average cost: " + fmt.format(totalCost/count);
report += "\n\nShoe List:\n\n";
for (int prod = 0; prod < count; prod++)
report += collection[prod].toString() + "\n";
return report;
}
}
import java.util.Scanner;
import java.io.*;
public class ProgTwo
{
public static void main (String[] args) throws FileNotFoundException, IOException
{
Scanner scan = new Scanner(new File("productin.txt"));
Scanner userInput = new Scanner(System.in);
ProductCollection data = new ProductCollection ();
String name = null;
double price = 0.0;
int idNum = 0, quantity = 0, input = 0;
while (scan.hasNextLine())
{
scan.useDelimiter(",");
idNum = scan.nextInt();
name = scan.next();
price = scan.nextDouble();
quantity = scan.nextInt();
scan.nextLine();
data.addProduct (idNum, name, price, quantity);
}
System.out.println("\nThis program asks you to select a number from");
System.out.println("the choices listed below of the popular brand");
System.out.println("Converse. Select shoes from Chuck Taylors to");
System.out.println("Jack Purcell are available. In addition, you");
System.out.println("are encouraged to add or delete shoe brands of");
System.out.println("your own, as well as a number of other options.\n");
do
{
System.out.println("1. Display one product");
System.out.println("2. Display all products");
System.out.println("3. Add a product");
System.out.println("4. Delete a product");
System.out.println("5. Buy product units");
System.out.println("6. Sell product units");
System.out.println("7. Change price of product");
System.out.println("8. Exit");
System.out.print("\nEnter your choice: ");
input = userInput.nextInt();
switch (input)
{
case 1: System.out.print("\nEnter the ID number of the product to display: " );
idNum = userInput.nextInt();
data.displayProduct(idNum);
break;
case 2: System.out.println (data);
break;
case 3: System.out.print("\nEnter the ID number: ");
idNum =userInput.nextInt();
System.out.print("\nEnter the name: " );
name = userInput.nextLine();
System.out.print("\nEnter the price: " );
price = userInput.nextDouble();
System.out.print("\nEnter the quantity: ");
quantity = userInput.nextInt();
data.addProduct(idNum, name, price, quantity);
System.out.println(data);
break;
case 4: System.out.print("\nEnter the ID number of the product to delete: ");
idNum = userInput.nextInt();
data.deleteProduct(idNum);
System.out.println(data);
break;
case 5: System.out.print("\nEnter ID number of product to modify: ");
idNum = userInput.nextInt();
System.out.print("\nHow many product units to buy? ");
quantity = userInput.nextInt();
userInput.nextLine();
data.addQty(idNum, quantity);
System.out.println(data);
break;
case 6: System.out.print("\nEnter ID number of product to modify: ");
idNum = userInput.nextInt();
System.out.print("\nHow many product units to sell? ");
quantity = userInput.nextInt();
userInput.nextLine();
data.subQty(idNum, quantity);
System.out.println(data);
break;
case 7: System.out.print("\nEnter the ID number of product to modify: ");
idNum = userInput.nextInt();
System.out.print("\nWhat is the new price? ");
price = userInput.nextDouble();
userInput.nextLine();
data.changePrice(idNum, price);
System.out.println(data);
}
} while (input != 8);
FileWriter fw = new FileWriter("productout.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println(data.createOutputFile());
pw.close();
}
}
1111,Chuck Taylor All Star Slim,50.00,22,
2222,Chuck Taylor All Star,45.00,17,
3333,Chuck Taylor All Star Trompe L'oeil,49.99,10,
4444,Chuck Taylor All Star Dble Tongue,39.99,15,
5555,Kids 0-3 years Chuck Taylors All Star,19.99,23,
6666,Jack Purcell Garment Dye,60.00,12,
7777,Jack Purcell African Canvas,65.00,18,
8888,Jack Purcell Low Profile Slip,70.00,29,
9999,Kids 4-7 years Jack Purcell,34.99,10,