public class Ch5
{

// simulating a bank account
private double balance = 100;

public void withdraw(double amount)
{
if (balance > amount)
{      balance = balance - amount; // shortcut: balance -= amount;
System.out.println("Withdrawal successful. New balance: \$" + balance);
}
else
System.out.println("Withdrawal not possible. Insufficient funds.");
}

// this section is on comparing floating point numbers
public void section5_2_2()
{ double r = Math.sqrt(2);
double d = r * r - 2;

// first attempt:
if (d == 0)
else
// second attempt:
double tolerance = 1E-14; // = 0.0000000000001
if (d < tolerance)
System.out.println("d is so close to 0 that we'll consider it equal to 0.");
else
System.out.println("No sorry, d is still not that close to 0.");
}

// this section is on comparing Strings
public void section5_2_3()
{ String name = "daniel";
if ("Daniel".equals(name)) // don't use == to compare objects
System.out.println("The two names are the same.");
if ("DANIEL".equalsIgnoreCase(name))
System.out.println("Those two names have the same letters.");
int result = "abby".compareTo("daniel");
// result = 0 if the 2 names are the same.
// result < 0 if emily comes before daniel in the alphabet
// result > 0 if emily comes after daniel in the alphabet
System.out.println("result: " + result);
int res = "abby".compareTo("ABBY");
System.out.println("abby.compareTo(ABBY)" + res);
System.out.println("car.compareTo(cargo" + "car".compareTo("cargo"));
String name2 = null; // has no value assigned
System.out.println(name2);
//if (name2 == null)
//...
}

public void section5_3_1()
{ double richter = -5.1;
String r = "";
if (richter >= 8)
r = "Most structures fall";
else if (richter >= 7)
r = "Many buildings destroyed";
else if (richter >= 6)
r = "Many buildings considerably damaged";
else if (richter >= 4.5)
r = "Damage to poorly constructed buildings";
else if (richter >= 3.5)
r = "Felt by many people";
else if (richter >= 0)
r = "Generally not felt by people";
else // optional
r = "Negative numbers are invalid";
System.out.println("With a richter value = " + richter + " " + r);
}

public void section5_3_2()
{ double income = 50000;
String status = "single";
double tax;
if (status.equals("single"))
{      if (income <= 32000)
{      tax = income * 0.10;
}
else
{      tax = 32000 * 0.1;
tax = tax + (income - 32000) * 0.25;
}
}
else
{      if (income <= 64000)
tax = income * 0.1;
else
{
tax = 64000 * 0.1;
tax = tax + (income - 64000) * 0.25;
}
}
System.out.print("On an income of " + income + " and with a marital status of " + status);
System.out.println(" your tax owed would be \$" + tax);
}

public boolean section_5_4_1()
{ double amount = 0;
// printing a boolean expression:
System.out.println(amount < 1000);
System.out.println("7 is a digit: " + Character.isDigit('7'));
System.out.println("p is a letter: " + Character.isLetter('p'));
return amount < 1000;
}

public void section_5_4_3()
{ // && means 'and'
// || means 'or'
double amount = 500;
if (0 < amount && amount < 1000)
System.out.println("amount is between 0 and 1000");
// check whether 'input' equals 'S' or 'M'
String input = "S";
if (input.equals("S") || input.equals("M"))
System.out.println("input equals either S or M");
// not in Java is !
if (! input.equals("M"))
System.out.println("input does not equal M");
}

public static void main(String[] args)
{ Ch5 account = new Ch5();
//account.withdraw(125);
//account.section5_2_2();
//account.section5_2_3();
//account.section5_3_1();
//account.section5_3_2();
//account.section_5_4_1();
account.section_5_4_3();
}
}