Ok, here is the problem. I have to ask a group of people for donations and get the total average and so on. The only problem I have is trying to get the lowest donation from the inputted numbers. I can't seem to get the actual lowest number. It just keeps getting the last "lowest" number. Any help would be appreciated.
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.lang.*;
public class prog2
{
public static void main(String[] args)
{
DecimalFormat df2 = new DecimalFormat( "$#,###.00" );
DecimalFormat df = new DecimalFormat( "#,###.00" );
int donor = 0; //single donor
int totalD = 0; //total donors
double money2 = 0; //Temp variable for saving the previous "money" input
double totalM = 0; //Money Total
double moneyHighest = 0; //Money Highest
double moneyLowest = 0; //Money Lowest
double money = 0; //original money
System.out.printf("\t%5s%20s%20s\n", "Name", "Amount", "Branch"); //print table names
while(donor != -1)
{
String name = JOptionPane.showInputDialog( null, "Enter your last name (Maximum of 20 letters)"); //name of donor
if (name.length() >= 20) //making sure lenght of name is not too long
{
JOptionPane.showMessageDialog(null, "Too long");
break;
}
else
{
money2 = money; //storing the previous money value
String moneyString = JOptionPane.showInputDialog( null, "Enter amount of money given"); //money input
money = Double.parseDouble(moneyString); //money
double moneyL = Math.min(money, money2);
moneyLowest = Math.min(moneyL, money);
if(moneyHighest < money)
{
moneyHighest = money;
}
}
String branchString = JOptionPane.showInputDialog( null, "Enter your branch (between 1 and 150)"); //branch input
int branch = Integer.parseInt(branchString); //branch
if (branch < 0 || branch > 151) //makes sure branch input is between 1 and 150
{
JOptionPane.showMessageDialog(null, "Wrong Input");
break;
}
else
{
System.out.printf("%-20s%s%-25s%-1s\n", name, "$", df.format(money), branch); //print out name, amount, and branch code
String donorS = JOptionPane.showInputDialog(null, "Enter 1 for another donor, or -1 to end the program"); //1 to keep going, -1 to end
donor = Integer.parseInt(donorS); //donors
totalD++; //total donors
totalM += money; // get total money from the money added together
}
}
double average = (totalM / totalD); // average of the Money divided by the number of donors
System.out.println(":");
System.out.println(":");
System.out.println("There are " + totalD + " donors.");//print total donors
System.out.println("The average is " + df2.format(average)+".");//print money average
System.out.println("The total is " + df2.format(totalM)+ ".");//print total money
System.out.println("The lowest is " + df2.format(moneyLowest) + ".");//print Lowest money amount only if there is more than 1 input
System.out.println("The highest is " + df2.format(moneyHighest) + "."); //print Highest money amount, if only one input, it is automatically highest
}
}