ขั้นตอนการทำโจทย์โปรแกรมภาษา Java

 โปรแกรมจองตั๋วเครื่องบิน

ขั้นตอนที่1  วิเคราะห์งาน
           
สิ่งที่โจทย์ให้มา
         ชื่อ      จุดหมายปลายทาง      จำนวนที่นั่ง        ระดับชั้นที่นั่ง
สิ่งที่โจท์ต้องการ
         โปรแกรมจองต๋วเครื่องบิน
ตัวแปร
        Name=ชื่อ
        destination=จุดหมายปลายทาง
        seatType=ระดับชั้นที่นั่ง
        seatNum=จำนวนที่นั่ง
        price=เงินสุทธิ
 

 วิธีการ
1. price =0
2. รับค่า Name,destination,seatType,seatNum
3. เงื่อนไข if destination=1
     Y ให้ price=25000
         destinationName="Tokyo"
        ไปข้อ6
     N ไปข้อ4
4. เงื่อนไข if destination=2
     Y ให้ price=20000
         destinationName="Beijing"
        ไปข้อ6
     N ไปข้อ5
5.  เงื่อนไข if destination=3
      Y ให้ price=30000
          destinationName="Doha"
        ไปข้อ 6
6. เงื่อนไข if seatType=1
     Y ให้ price = price*4
         seatTypeName = "First Class"
         ไปข้อ 9
    N ไปข้อ 7
7. เงื่อนไข if seatType=2
     Y ให้ price = price*2
         seatTypeName = "Business Class"
         ไปข้อ 9
     N ไปข้อ 8
8. เงื่อนไข if seatType=3
     Y ให้ price = price*1
         seatTypeName = "Doha"
         ไปข้อ 9
9. price = price*seatNum
10. แสดง ("::: Your Ticket Info ::")
11. แสดง ("From Bangkok To "+destinationName)
12. แสดง ("Seat : "+seatTypeName)
13. แสดง ("Price : "+price)
14. จบการทำงาน




ขั้นตอนที่2  Flow Chart
 
ขั้นตอนที่3 เขียนโปรแกรม 

โปรแกรมจองตั๋วเครื่องบิน
import java.io.*;         //ต้องประกาศ import เมื่อต้องการใช้ class java.io รับค่าจาก user
public class TicketBooking {              //ชื่อ class จะต้องชื่อเดียวกับ file
    public static void main(String[] args) throws IOException, Exception {       //method ที่ใช้สำหรับ run program, โยนทิ้งเมื่อมี exeption
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  //ประกาศคำสั่งที่ใช้ในการรับค่า
        System.out.println("Choose Destination");                  //แสดงข้อความ
        System.out.println("1. Tokyo, Japan ");                     //แสดงข้อความ
        System.out.println("2. Beijing, China ");                     //แสดงข้อความ
        System.out.println("3. Doha, Qatar ");                       //แสดงข้อความ
        String strDestination = in.readLine();              //รับค่าจุดหมายปลายทาง จาก user เป็นข้อความ

        System.out.println("Choose Seat Type ");                      //แสดงข้อความ
        System.out.println("1. First Class ");                              //แสดงข้อความ
        System.out.println("2. Business Class ");                         //แสดงข้อความ
        System.out.println("3. Economy Class ");                         //แสดงข้อความ
        String strSeatType = in.readLine();         //รับค่าชนิดที่นั่งจาก user เป็นข้อความ
       
        System.out.println("Choose Number of Seat ");               //แสดงข้อความ
        String strSeatNum = in.readLine();                 //รับค่าจำนวนที่นั่งจาก user เป็นข้อความ

        int destination = Integer.parseInt(strDestination);        //แปลงข้อความ strDestination เป็นตัวเลขจำนวนเต็ม
        int seatType = Integer.parseInt(strSeatType);          //แปลงข้อความ strSeatType เป็นตัวเลขจำนวนเต็ม
        int seatNum = Integer.parseInt(strSeatNum);        //แปลงข้อความ strSeatNum เป็นตัวเลขจำนวนเต็ม
        double price = 0;                            //ประกาศตัวแปร price เป็นตัวเลขทศนิยม
        String destinationName = "";          //ประกาศตัวแปร destinationName เป็นข้อความ
        String seatTypeName = "";            //ประกาศตัวแปร seatTypeName เป็นข้อความ

        switch(destination){                                          //ใช้คำสั่ง switch ด้วยตัวแปร destination
            case 1:price=25000;destinationName="Tokyo";break;             //ใส่ค่า price และใส่ค่า destinationName เมื่อ destination มีค่าเท่ากับ 1
            case 2:price=20000;destinationName="Beijing";break;            //ใส่ค่า price และใส่ค่า destinationName เมื่อ destination มีค่าเท่ากับ 2
            case 3:price=30000;destinationName="Doha";break;                //ใส่ค่า price และใส่ค่า destinationName เมื่อ destination มีค่าเท่ากับ 3
            default:System.exit(0);                                          //ออกจากโปรแกรมทันที
        }

        switch(seatType){             //ใช้คำสั่ง switch ด้วยตัวแปร seatType
            case 1:price=price*4;seatTypeName="First Class";break;        //ใส่ตัวคุณ price และใส่ค่า destinationName เมื่อ seatType มีค่าเท่ากับ 1
            case 2:price=price*2;seatTypeName="Business Class";break;   //ใส่ตัวคุณ price และใส่ค่า destinationName เมื่อ seatType มีค่าเท่ากับ 2
            case 3:seatTypeName="Economy Class";break;                       //ใส่ตัวคุณ price และใส่ค่า destinationName เมื่อ seatType มีค่าเท่ากับ 3
            default:System.exit(0);                           //ออกจากโปรแกรมทันที
        }

        price = price*seatNum;                                             //คุณจำนวนที่นั่งกับราคา

        System.out.println("::: Your Ticket Info ::");                          //แสดงผลลัพธ์
        System.out.println("From Bangkok To "+destinationName);        //แสดงผลลัพธ์
        System.out.println("Seat            : "+seatTypeName);                  //แสดงผลลัพธ์
        System.out.println("Price           : "+price);                         //แสดงผลลัพธ์
    }
}
            เมื่อเขียนโค้ดตามขั้นตอนเส็ดแล้ว ก็มา Compile กันเลย
        หลังจาก Compile แล้ว ไม่มี Error ก็มาลอง Run โปรแกรมกันดูครับ