I am writing a photo program that accepts the users internet speed and width and height of a photo. The program then displays the cost, storage size, and time to transfer. I split the program into two parts, one which sets the values and a class that has all the methods.
The main problem I am having with my program is adding up all the values of all the photos entered and showing the price, storage, and time at the end.
So far, I have a addTotal() method that adds the time, price, and storage to the values I have set. What keeps happening though is that the values for the last photo entered keeps displaying instead of the total for all photos.
For example:
If the internet speed is set to 150,000, and there are two photos with sizes 4,5 and 4.5,4.5, what is suppose to display at the end is:
Total cost = $4.83
4.14 Mbytes of storage
3 minutes, 40 seconds to transfer
Any help is appreciated.
Here is the main program:
import javax.swing.JOptionPane;
{
public static void main(String args[])
{
double speed;
double height;
double width;
double compression = 3.5;
double resolution = 600;
System.out.println("Welcome to the Scans-R-Us Digital Photo Shop!");
String userInput = JOptionPane.showInputDialog("Do you have a photo you would like us to scan(Yes or No)?");
while((!userInput.equals("Yes") && (!userInput.equals("YES")) && (!userInput.equals("yes"))))
{
System.out.println("test");
System.exit(0);
}
do
{
String internetString = JOptionPane.showInputDialog("What is your internet speed in bits/second?");
speed = Double.parseDouble(internetString);
}
while(speed < 0);
while((userInput.compareTo("No") != 0) && (userInput.compareTo("NO") != 0) && (userInput.compareTo("no") != 0))
{
DigitalPhoto one = new DigitalPhoto();
do
{
String heightString = JOptionPane.showInputDialog("What is the height of the photo?");
height = Double.parseDouble(heightString);
}
while(height >=8);
do
{
String widthString = JOptionPane.showInputDialog("What is the width of the photo?");
width = Double.parseDouble(widthString);
}
while(width >= 8);
one.setWidth(width);
one.setHeight(height);
one.setSpeed(speed);
one.setCompression(compression);
one.setResolution(resolution);
one.setPrice();
one.setStorage();
one.setTime();
one.showValues();
one.addTotal();
userInput = JOptionPane.showInputDialog("Do you have a photo you would like us to scan(Yes or No)?");
while((!userInput.equals("Yes") && (!userInput.equals("YES")) && (!userInput.equals("yes"))))
{
one.endValues();
System.out.println("\nThanks, have a nice day!");
System.exit(0);
}
}
}
}
And here is the class I am using to run the program:
import javax.swing.JOptionPane;
{
double total;
double width;
double height;
double speed;
double compression;
double resolution;
double price;
double time;
double totalPrice;
double totalStorage;
double totalTime;
double pricePerInch = .12;
int bytes = 8;
int megaSize = 1000000;
DigitalPhoto()
{
width = 0.0;
height = 0.0;
speed = 0.0;
compression = 0.0;
resolution = 0.0;
total = 0.0;
price = 0.0;
time = 0.0;
}
public void setWidth(double thisWidth)
{
width = thisWidth;
}
public void setHeight(double thisHeight)
{
height = thisHeight;
}
public void setSpeed(double thisSpeed)
{
speed = thisSpeed;
}
public void setCompression(double thisCompression)
{
compression = thisCompression;
}
public void setResolution(double thisResolution)
{
resolution = thisResolution;
}
public double getWidth()
{
return width;
}
public double getHeight()
{
return height;
}
public double getSpeed()
{
return speed;
}
public double getCompression()
{
return compression;
}
public double getResolution()
{
return resolution;
}
public double setStorage()
{
width = width * resolution;
height = height * resolution;
total = width * height;
total = total / compression;
total = total / megaSize;
return total;
}
public double setPrice()
{
price = height * width;
price = price * pricePerInch;
return price;
}
public double setTime()
{
total = total * megaSize;
time = total * bytes;
time = time / speed;
total = total / megaSize;
return time;
}
public void showValues()
{
System.out.println("\nScanned photo details: ");
System.out.println("Resolution: " + resolution);
System.out.println("Compression ratio: " + compression);
System.out.println("The customer photos require " + total + " Mbytes of storage.");
System.out.println("The customer photos cost $" + price);
System.out.println("It will take " + (int)time / 60 + " minutes and " + (int)time % 60 + " seconds");
}
public void addTotal()
{
totalTime = totalTime + time;
totalPrice = totalPrice + price;
totalStorage = totalStorage + total;
}
public void endValues()
{
System.out.println("\nThe photos require " + totalStorage + " Mbytes of storage.");
System.out.println("The photos cost $" + totalPrice);
System.out.println("It will take " + (int)totalTime / 60 + " minutes and " + (int)totalTime % 60 + " seconds");
}
}
You are creating a new object every time you enter the loop, so you only keep the last one. You need to create the object before the loop:
Then query the result after the loop ends.