Last week I went to bowling with my friends in Fremont and we enjoyed it a lot. I paid on behalf of them for the game so that everyone else pay me back later. Despite their repeated query to know how much they should pay me, I was too busy the whole last week to calculate that. Right now, having a little relax time, so I downloaded JDK 6 in my new laptop from office and wrote the small java program to calculate it. Compile and run the program to know how much I owe to each of them.
/**
* To compile: javac -d . BowlingCost.java
* To run: java BowlingCost
*/
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
public class BowlingCost {
public static final double shoeRent = 4.0;
public static final double perGame = 4.25;
private static int male = 5;
private static int female = 4;
private static Map payersMaritalStatus = new HashMap();
public BowlingCost() {
payersMaritalStatus.put("Shahriar", Boolean.TRUE);
payersMaritalStatus.put("Ezaz", Boolean.TRUE);
payersMaritalStatus.put("Hasinur", Boolean.TRUE);
payersMaritalStatus.put("Nitol", Boolean.FALSE);
payersMaritalStatus.put("Ashik", Boolean.TRUE);
}
public static void main(String []args) {
BowlingCost cost = new BowlingCost();
System.out.println("Total Cost of Bowling = " + calculateTotal() + " dollars.\n");
displayPaymentAmounts();
System.out.println("\nThank you! It was a pleasure playing bowling with you guys!!");
}
private static double calculateTotal() {
return perGame * (male * 3 + female * 1) + shoeRent * (male + female);
}
private static void displayPaymentAmounts() {
Iterator it = payersMaritalStatus.keySet().iterator();
String payerName = null;
boolean isMarried = false;
while(it.hasNext()) {
payerName = (String) it.next();
isMarried = ((Boolean) payersMaritalStatus.get(payerName)).booleanValue();
if(isMarried) {
System.out.println(payerName + " should pay " + (perGame * (3 + 1) + shoeRent * (1 + 1)) + " dollars.");
} else {
System.out.println(payerName + " should pay " + (perGame * 3 + shoeRent * 1) + " dollars.");
}
}
}
}
If you don't want to run the program in command line or opening your favorite IDE, then you can use the following online java compiler to copy paste the code there (select Java SE 1.4 class from the drop down box at the bottom).
http://www.zamples.com/JspExplorer/index.jsp?format=jdk16cl
Here is the output if you run the program:
Total Cost of Bowling = 116.75 dollars.
Ezaz should pay 25.0 dollars.
Ashik should pay 25.0 dollars.
Hasinur should pay 25.0 dollars.
Shahriar should pay 25.0 dollars.
Nitol should pay 16.75 dollars.
Thank you! It was a pleasure playing bowling with you guys!!
I am an engineering leader, a chess enthusiast and an avid reader. I have moved as a software engineer inUSA from my home country Bangladesh at 2005 leaving many of my family members, relatives, friends and good memories. In this journal I want to capture how are my days going here in this migrated place with my wife, son and daughter.
Subscribe to:
Posts (Atom)
Why I’m Voting for Change: A Call for Anti-War Leadership in 2024
As an American, I’m increasingly troubled by our country’s role in Middle Eastern foreign policy. Despite their domestic differences, both m...
-
We moved to a new rental house near BART end of last month. We have stayed in LakeView Apartments for three and half years before moving he...
-
Just when I was thinking we (Bangladeshis) are moving forward with the newly elected Govt bringing hopes for a better democracy, I was shock...
-
After a month of my Civic being completely destroyed in a car accident, I bought my new car yesterday. It's Honda Accord 2009 LX-P Maro...