I need to modify a program to use arrays and a method call. Should I modify the running file, the data collection file, or both?
Posted
by
g3n3rallyl0st
on Stack Overflow
See other posts from Stack Overflow
or by g3n3rallyl0st
Published on 2011-03-04T23:20:42Z
Indexed on
2011/03/04
23:24 UTC
Read the original article
Hit count: 188
I have to have multiple classes for this program. The problem is, I don't fully understand arrays and how they work, so I'm a little lost. I will post my program I have written thus far so you can see what I'm working with, but I don't expect anyone to DO my assignment for me. I just need to know where to start and I'll try to go from there. I think I need to use a double array since I will be working with decimals since it deals with money, and my method call needs to calculate total price for all items entered by the user. Please help:
RUNNING FILE
package inventory2;
import java.util.Scanner;
public class RunApp
{
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
DataCollection theProduct = new DataCollection();
String Name = "";
double pNumber = 0.0;
double Units = 0.0;
double Price = 0.0;
while(true)
{
System.out.print("Enter Product Name: ");
Name = input.next();
theProduct.setName(Name);
if (Name.equalsIgnoreCase("stop"))
{
return;
}
System.out.print("Enter Product Number: ");
pNumber = input.nextDouble();
theProduct.setpNumber(pNumber);
System.out.print("Enter How Many Units in Stock: ");
Units = input.nextDouble();
theProduct.setUnits(Units);
System.out.print("Enter Price Per Unit: ");
Price = input.nextDouble();
theProduct.setPrice(Price);
System.out.print("\n Product Name: " + theProduct.getName());
System.out.print("\n Product Number: " + theProduct.getpNumber());
System.out.print("\n Amount of Units in Stock: " + theProduct.getUnits());
System.out.print("\n Price per Unit: " + theProduct.getPrice() + "\n\n");
System.out.printf("\n Total cost for %s in stock: $%.2f\n\n\n", theProduct.getName(), theProduct.calculatePrice());
}
}
}
DATA COLLECTION FILE
package inventory2;
public class DataCollection
{
String productName;
double productNumber, unitsInStock, unitPrice, totalPrice;
public DataCollection()
{
productName = "";
productNumber = 0.0;
unitsInStock = 0.0;
unitPrice = 0.0;
}
//setter methods
public void setName(String name)
{
productName = name;
}
public void setpNumber(double pNumber)
{
productNumber = pNumber;
}
public void setUnits(double units)
{
unitsInStock = units;
}
public void setPrice(double price)
{
unitPrice = price;
}
//getter methods
public String getName()
{
return productName;
}
public double getpNumber()
{
return productNumber;
}
public double getUnits()
{
return unitsInStock;
}
public double getPrice()
{
return unitPrice;
}
public double calculatePrice()
{
return (unitsInStock * unitPrice);
}
}
© Stack Overflow or respective owner