> ## Documentation Index
> Fetch the complete documentation index at: https://v1-learn.neoartd.my.id/llms.txt
> Use this file to discover all available pages before exploring further.

# Conditions, Input Validation, Strings

# Introduction 1

<Tabs>
  <Tab title="Question">
    As part of promotion, a department store provides discounts for its customers.
    All products whose prices are above IDR 300,000 are given a discount of 40%. For example, a pair of shoes that cost IDR 499,000 are now sold for only IDR 299,400.
    However, the labels printed on the products are still showing prices before the discount, so the department store employee still needs to calculate in advance to get the final price.
    **Write a program to help department store employees to calculate the final price of the products.**
  </Tab>

  <Tab title="Answer">
    ```java theme={null}
      total = price;
      if (price > 300000 && date == 7) {
          total = total - 0.4 * total; // 40% discount
          total = total - 0.2 * total; // additional 20% discount
      } else if (price > 300000) {
          total = total - 0.4 * total; // 40% discount
      } else if (date == 7) {
          total = total - 0.2 * total; // additional 20% discount
      } 
    ```
  </Tab>
</Tabs>

# Introduction 2

<Tabs>
  <Tab title="Question">
    Because the strategy of providing discounts successfully attracted customers, department store management plans to hold additional events. On the 7th of each month, department stores will provide an additional discount of 20% for all products. For products with prices above IDR 300,000, of course the discounts obtained will double.
    For example, for a bag worth IDR 1,000,000, the initial discount obtained is 40% x IDR 1,000,000 = IDR 400,000 so the price of the item before the additional discount = IDR 600,000.
    With an additional discount of 20%, the total to be paid is IDR 600,000 - (20% x IDR 600,000) = IDR 480,000.
    **Update the program you have previously written.**
  </Tab>

  <Tab title="Answer">
    ```java theme={null}
      total = price;
      if (price > 300000) {
          total = total - 0.4 * total;
      }
      if (date == 7) {
          total = total - 0.2 * total;
      }
    ```
  </Tab>
</Tabs>

# Flag Variables

```java theme={null}
boolean varFlag = condition;
```

<Tabs>
  <Tab title="Sample Case">
    Write a program to determine whether the size of the baby at birth is normal
    or not

    * Normal baby weight at birth ranges from 2.5 to 4.5 kg and lengths between 45 and 60 cm
    * Input: **baby's weight in kg and length in cm**
    * Output: **whether the baby's weight and length are normal, its weight is normal, its length is normal, or both are not normal**
  </Tab>

  <Tab title="Sample Solution">
    ```java theme={null}
      if (weight >= 2.5 && weight <= 4.5 && length >= 45 && length <= 60) {
          // both weight & length are normal
      } else if (weight >= 2.5 && weight <= 4.5) {
          // weight is normal, length is not
      } else if (length >= 45 && length <= 60) {
          // length is normal, weight is not
      } else {
          // both weight & length are not normal
      }
    ```
  </Tab>

  <Tab title="Using Flag Variables">
    ```java theme={null}
      // Flag Variables
      boolean weightNormal = (weight >= 2.5 && weight <= 4.5);
      boolean lengthNormal = (length >= 45 && length <= 60);

      if (weightNormal && lengthNormal) {
          // both weight & length are normal
      } else if (weightNormal) {
          // weight is normal, length is not
      } else if (lengthNormal) {
          // length is normal, weight is not
      } else {
          // both weight & length are not normal
      }
    ```
  </Tab>
</Tabs>

# Nested Ifs

```java theme={null}
if (condition1) {
    // do something
    if (condition2) {
        // do something
    } else {
        // do something
    }
    // do something
}
```

<Tabs>
  <Tab title="Sample Case">
    Write a program to calculate the total user must pay for an imported item.

    * If the price of the item exceeds USD 100, then an import tax of 20% is imposed
    * Input: **price of items before tax in USD**
    * Output: **total paid (item price plus tax)**
  </Tab>

  <Tab title="Negative price allowed?">
    ```java theme={null}
      Scanner in = new Scanner(System.in);
      
      double price = in.nextDouble();
      double total = price;

      if (price > 100) {
          total = price + 0.2 * price;
      }
      System.out.println(total);
    ```
  </Tab>

  <Tab title="Solution">
    ```java theme={null}
      Scanner in = new Scanner(System.in);
      double price = in.nextDouble();

      if (price > 0) {
          double total = price;
          if (price > 100) {
              total = price + 0.2 * price;
          }
          System.out.println(total);
      } else {
          System.out.println("The price must be positive");
      }
    ```
  </Tab>
</Tabs>

# Input Validation

<Tabs>
  <Tab title="Sample Case">
    Write a program to calculate the volume of the cube

    * Input: **the length of any edge of the cube**
    * Output: **volume of the cube**
  </Tab>

  <Tab title="Solution">
    ```java theme={null}
      Scanner in = new Scanner(System.in);

      System.out.print("Enter the length of the cube's edge: ");
      double edge = in.nextDouble();
      double volume = edge * edge * edge;
      System.out.println(volume);
    ```
  </Tab>

  <Tab title="Negative edge length allowed?">
    ```java theme={null}
      Scanner in = new Scanner(System.in);
      
      System.out.print("Enter the length of the cube's edge: ");
      double edge = in.nextDouble();
      if (edge > 0) {
          double volume = edge * edge * edge;
          System.out.println(volume);
      } else {
          System.out.println("The length of the cube's edge must be positive");
      }
    ```
  </Tab>

  <Tab title="User don't enter a number?">
    ```java theme={null}
      Scanner in = new Scanner(System.in);
      System.out.print("Enter the length of the cube's edge: ");
      if (in.hasNextDouble()) {
          double edge = in.nextDouble();
          ...
      } else {
          String word = in.next();
          System.out.println(word + " is not a number");
      }
    ```
  </Tab>

  <Tab title="Return Keyword">
    ```java theme={null}
      Scanner in = new Scanner(System.in);
      System.out.print("Enter the length of the cube's edge: ");
      if (! in.hasNextDouble()) {
          String word = in.next();
          System.out.println(word + " is not a number");
          return; // return keyword
      }
      double edge = in.nextDouble();
      ...
    ```
  </Tab>
</Tabs>

# String

## Strings in Conditionals

```java theme={null}
Scanner in = new Scanner(System.in);
System.out.print("What is your name: ");
String name = in.next();
if (name == "Ani") {
    System.out.println("Your name is Ani");
} else {
    System.out.println("Your name is not Ani");
}
```

## String Comparison

<Tabs>
  <Tab title="equals">
    `.equals()` : digunakan untuk membandingkan isi dari dua buah string, dan mengembalikan true jika isi kedua string tersebut sama.

    ```java theme={null}
      string.equals(anotherString)
      string.equalsIgnoreCase(anotherString)

      string1 = "text";
      string2 = "text";
      string3 = "Text";
      string4 = "txt";

      string1.equals(string2); // true
      string1.equals(string3); // false
      string1.equals(string4); // false

      string1.equalsIgnoreCase(string2); // true
      string1.equalsIgnoreCase(string3); // true
      string1.equalsIgnoreCase(string4); // false
    ```
  </Tab>

  <Tab title="contains">
    `.contains()` : digunakan untuk mengecek apakah suatu string mengandung karakter tertentu atau tidak.

    ```java theme={null}
      string.contains(anotherString)

      string1 = "once upon a time";
      string2 = "time";
      string3 = "once upon a time";
      string4 = "a book";

      string1.contains(string2); // true
      string2.contains(string1); // false
      string1.contains(string3); // true
      string1.contains(string4); // false
    ```
  </Tab>

  <Tab title="startswith/endswith">
    `.startswith()` : digunakan untuk mengecek apakah suatu string diawali dengan karakter tertentu atau tidak.\
    `.endswith()` : digunakan untuk mengecek apakah suatu string diakhiri dengan karakter tertentu atau tidak.

    ```java theme={null}
      string.startsWith(anotherString)
      string.endsWith(anotherString)

      string1 = "once upon a time ";
      string2 = "once";
      string3 = "time";

      string1.startsWith(string2); // true
      string1.startsWith(string3); // false
      string1.endsWith(string2); // false
      string1.endsWith(string3); // true
    ```
  </Tab>

  <Tab title="compareTo/compareToIgnoreCase">
    `.compareTo()` : digunakan untuk membandingkan dua buah string berdasarkan urutan leksikografisnya.\
    `.compareToIgnoreCase()` : digunakan untuk membandingkan dua buah string berdasarkan urutan leksikografisnya tanpa memperdulikan huruf besar atau kecil.

    ```java theme={null}
      string.compareTo(anotherString)
      string.compareToIgnoreCase(anotherString)

      string1 = "budi";
      string2 = "ani";
      string3 = "chandra";
      string4 = "Budi";

      string1.compareTo(string2); // > 0
      string1.compareTo(string3); // < 0
      string1.compareTo(string1); // 0
      string1.compareTo(string4); // > 0
      string1.compareToIgnoreCase(string4); // 0
    ```
  </Tab>
</Tabs>

## String Manipulation

<Tabs>
  <Tab title="substring">
    `.substring()` : digunakan untuk mengambil sebagian karakter dari suatu string.

    ```java theme={null}
      string.substring(beginIndex)
      string.substring(beginIndex, endIndex)

      string = "abcdefghij";

      string.substring(3); // "defghij"
      string.substring(3, 5); // "de"
    ```
  </Tab>

  <Tab title="replaceAll/replaceFirst">
    `.replaceAll()` : digunakan untuk mengganti semua kemunculan suatu karakter atau string dengan karakter atau string lain.\
    `.replaceFirst()` : digunakan untuk mengganti kemunculan pertama suatu karakter atau string dengan karakter atau string lain.

    ```java theme={null}
      string.replaceAll(pattern, replacement)
      string.replaceFirst(pattern, replacement)

      string = "he's a doctor while she's a nurse";
      string.replaceAll("'s", " is");
          // "he is a doctor while she is a nurse"
      string.replaceFirst("'s", " is");
          // "he is a doctor while she's a nurse"
    ```
  </Tab>

  <Tab title="toUpperCase/toLowerCase">
    `.toUpperCase()` : digunakan untuk mengubah semua karakter dalam suatu string menjadi huruf besar.\
    `.toLowerCase()` : digunakan untuk mengubah semua karakter dalam suatu string menjadi huruf kecil.

    ```java theme={null}
     string.toUpperCase()
      string.toLowerCase()

      string = "My name is Ani";
      string.toUpperCase();
          // "MY NAME IS ANI"
      string.toLowerCase();
          // "my name is ani"
    ```
  </Tab>

  <Tab title="trim">
    `.trim()` : digunakan untuk menghapus spasi di awal dan di akhir suatu string.

    ```java theme={null}
      string.trim()

      string =" Ani ";
      string.trim();
          // "Ani"
    ```
  </Tab>
</Tabs>

## String → Number

`.parseInt()` : digunakan untuk mengubah string menjadi integer.\
`.parseDouble()` : digunakan untuk mengubah string menjadi double.

```java theme={null}
Integer.parseInt(string)
Double.parseDouble(string)

String time = "18:49";
String h = time.substring(0, 2);
String m = time.substring(3);
int hour1 = Integer.parseInt(h);
int minute1 = Integer.parseInt(m);
double hour2 = Double.parseDouble(h);
double minute2 = Double.parseDouble(m);
```
