SumAll.java
From MyWiki
< source lang="java"> public class SumAll { /// Class to recieve three strings from calling program and summing up all the disk space figures // Variables String totalSpace; // these three are inputs String usedSpace; String freeSpace; float totalSum = 0.1f ; // these three are the ouptut float usedSum = 0.1f ; float freeSum = 0.1f ;
void initialise() // This will reset all of the input and output counters to zero
{
totalSpace = "";
usedSpace = "";
freeSpace = "";
totalSum = 0;
usedSum = 0;
freeSum = 0;
}
void addThis(String tot,String used,String free)
{
String _tot = tot;
String _used = used;
String _free = free;
char unit ;
String value ;
float longValue = 0 ;
// first the tot
// We have to convert the strings to bytes, find last char, B,K,G,T and multiply appropriately
// Example code : str.charAt(str.length() - 1)
// System.out.println("the string is " + _tot + " and the length is " + _tot.length());
unit = _tot.charAt(_tot.length() -1 ); // This gets the last character to see what the units are
// Example str = str.substring(0, str.length()-1); // to get all but the last character
value = _tot.substring(0, _tot.length()-1);
// Example Long.parseLong(numberAsString);
// Use some if statements to decide the mulitplier
/// Run the if statements
// Examples
// float f = Float.parseFloat("25");
// String s = Float.toString(25.0f);
if ( unit == 'B' ) { longValue = Float.parseFloat(value); }
else if ( unit == 'K' ) { longValue = Float.parseFloat(value) * 1000 ; }
else if ( unit == 'G' ) { longValue = Float.parseFloat(value) * 1000 * 1000 ; }
else if ( unit == 'T' ) { longValue = Float.parseFloat(value) * 1000 * 1000 * 1000 ; }
totalSum = totalSum + longValue;
// System.out.println("the current sum is : " + totalSum);
// Now used
// We have to convert the strings to bytes, find last char, B,K,G,T and multiply appropriately
// Example code : str.charAt(str.length() - 1)
unit = _used.charAt(_used.length() -1 ); // This gets the last character to see what the units are
// Example str = str.substring(0, str.length()-1); // to get all but the last character
value = _used.substring(0, _used.length()-1);
// Example Long.parseLong(numberAsString);
// Use some if statements to decide the mulitplier
/// Run the if statements
if ( unit == 'B' ) { longValue = Long.parseLong(value); }
else if ( unit == 'K' ) { longValue = Float.parseFloat(value) * 1000 ; }
else if ( unit == 'G' ) { longValue = Float.parseFloat(value) * 1000 * 1000 ; }
else if ( unit == 'T' ) { longValue = Float.parseFloat(value) * 1000 * 1000 * 1000 ; }
usedSum = usedSum + longValue;
// Now free
// We have to convert the strings to bytes, find last char, B,K,G,T and multiply appropriately
// Example code : str.charAt(str.length() - 1)
unit = _free.charAt(_free.length() -1 ); // This gets the last character to see what the units are
// Example str = str.substring(0, str.length()-1); // to get all but the last character
value = _free.substring(0, _free.length()-1);
// Example Long.parseLong(numberAsString);
// Use some if statements to decide the mulitplier
/// Run the if statements
if ( unit == 'B' ) { longValue = Long.parseLong(value); }
else if ( unit == 'K' ) { longValue = Float.parseFloat(value) * 1000 ; }
else if ( unit == 'G' ) { longValue = Float.parseFloat(value) * 1000 * 1000 ; }
else if ( unit == 'T' ) { longValue = Float.parseFloat(value) * 1000 * 1000 * 1000 ; }
freeSum = freeSum + longValue;
}
String returnStuff(String s )
{ // s can be SUM, TOT or FREE
if ( s.equals("SUM")) { // Create a nice human readable string from the long int for sum
long tempSum = (long)totalSum/1000000 ;
//int tempSum = (int)totalSum ;
/// System.out.println("the tempsum is " + tempSum);
String SUM;
// Examples
// String.valueOf(number) (my preference)
// Integer.toString(number)
SUM = String.valueOf(tempSum)+"G";
//// System.out.println("returning the return value of : " + SUM);
return SUM;
}
if ( s.equals("TOT")) { // Create a nice human readable string from the long int for total
long tempSum = (long)usedSum/1000000 ;
////System.out.println("the tempsum is " + tempSum);
String TOT;
// Examples
// String.valueOf(number) (my preference)
// Integer.toString(number)
TOT = String.valueOf(tempSum)+"G";
//// System.out.println("returning the return value of : " + TOT);
return TOT;
}
if ( s.equals("FREE")) { // Create a nice human readable string from the long int for total
long tempSum = (long)freeSum/1000000 ;
//// System.out.println("the tempsum is " + tempSum);
String FREE;
// Examples
// String.valueOf(number) (my preference)
// Integer.toString(number)
FREE = String.valueOf(tempSum)+"G";
/// System.out.println("returning the return value of : " + FREE);
return FREE;
}
return "asdf";
}
}
</source>