โจทย์ จงจัดเก็บข้อมูลนิสิตได้แก่ รหัสนิสิต, ชื่อนิสิต, คะแนนสอบกลางภาค, คะแนนสอบปลายภาค, คะแนนควิช, คะแนน Project และ คะแนนเข้าห้องเรียง โดยใช้ ArrayIndexList จาก Library ของ Net. Data Structure
การออกแบบ และสิ่งที่ต้องรู้
- ArrayIndexList คือ โครงสร้างข้อมูล List หรือ Queue ซึ่งมีการจัดการข้อมูลแบบ FIFO (First In First Out) ง่ายๆเลย คือ ใครมาก่อนได้ก่อนนั้นเอง ตัวอย่างเช่น การเข้าแถวซื้ออาหาร
- Class ListTestNet เป็น Main ทำหน้าที่ทดสอบการใช้งาน โดยมีหน้าที่ต่างๆ ได้แก่
- เพิ่มข้อมูล
- ดึงข้อมูลออก
- ตรวจสอบว่า List ว่าง หรือไม่
- ลบข้อมูล
- แก้ไขข้อมูล
- แสดงขนาดของ List
- ออกจากโปรแกรม
- Class Student เป็น Data Class ไว้จัดเก็บข้อมูลนิสิต
โปรแกรมที่เขียนออกมา(ดูความอธิบายเพิ่มเติมตาม comment ได้เลย)
- Class ListTestNet
import javax.swing.*; import net.datastructures.*; class ListTestNet { public static void main(String[] args) { //ประกาศ loop ให้มีชนิดข้อมูลแบบ booloean เพื่อตรวจสถานะการวนloop boolean loop = true; //ประกาศ curIdx ให้มีชนิดข้อมูลเป็น Integer int curIdx = 0; //สร้าง instance ของ Class ArrayIndexList มีชื่อว่า list ArrayIndexList list = new ArrayIndexList(); try { while (loop) { //ขึ้นหน้าจอแสดงตัวเลือกพร้อมทำการรับค่า int pick = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter your choice:\n1.Add\n2.Get\n3.IsEmpty\n4.Remove" + "\n5.Set update\n6.Size\n7.quit")); //ถ้าตรวจสอบได้ค่า 1 if (pick == 1) { //ทำการใส่ข้อมูลเข้าไป โดยดึงมาจาก method getData list.add(curIdx, getData()); curIdx++; //ขึ้นข้อความแสดงการทำงานเสร็จสิ้น JOptionPane.showMessageDialog(null, "Add data success"); } else if (pick == 2) { if (curIdx & gt; 0) curIdx--; //ทำการดึงข้อมูลออกมาแสดง Student temp = (Student) list.get(curIdx); //ใช้ method display เพื่อทำการแสดงข้อมูล display(temp); } else if (pick == 3) { //ตรวจสอบว่า list ว่างหรือไม่ if (list.isEmpty()) { //ขึ้นข้อความแสดงการทำงาน JOptionPane.showMessageDialog(null, "list is empty"); } } else if (pick == 4) { if (curIdx & gt; 0) curIdx--; //System.out.println(curIdx); //ทำการลบข้อมูล list.remove(curIdx); //ขึ้นข้อความแสดงการทำงานเสร็จสิ้น JOptionPane.showMessageDialog(null, "remove data success"); } else if (pick == 5) { //ให้ตัว update ข้อมูล list.set(curIdx, getData()); //ขึ้นข้อความแสดงการทำงานเสร็จสิ้น JOptionPane.showMessageDialog(null, "Now at first node"); } else if (pick == 6) { //ทำหาขนาด int s = list.size(); //ขึ้นข้อความแสดงการทำงานเสร็จสิ้น JOptionPane.showMessageDialog(null, "Size: " + s); } else if (pick == 7) { //ให่ค่า loop มีค่าเป็น false loop = false; //ขึ้นข้อความแสดงการทำงานเสร็จสิ้น JOptionPane.showMessageDialog(null, "quit"); } } } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); e.printStackTrace(); } } public static Student getData() { //ประกาศตัวแปร id, name เป็น String String id, name; //ประกาศตัวแปร midScore, finalScore, quizScore, proScore, attentScore เป็น integer int midScore, finalScore, quizScore, proScore, attentScore; //รับข้อมูลเข้ามาผ่านทาง JOptionPane id = JOptionPane.showInputDialog(null, "Input Id:"); name = JOptionPane.showInputDialog(null, "Input Name:"); midScore = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Midterm Score:")); finalScore = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Final Score:")); quizScore = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Quiz Score:")); proScore = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Project Score:")); attentScore = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Attentdant Score:")); //ประกาศตัวแปร Student ชื่อ std Student std = new Student(id, name, midScore, finalScore, quizScore, proScore, attentScore); return std; } public static void display(Student std) { //แสดงข้อมูลที่ได้ออกมา System.out.println("id: " + std.getId()); System.out.println("name: " + std.getName()); System.out.println("midterm: " + std.getMid()); System.out.println("final: " + std.getFinal()); System.out.println("quiz: " + std.getQuiz()); System.out.println("project: " + std.getPro()); System.out.println("attentdant: " + std.getAttent()); System.out.println("total: " + std.getTotal()); System.out.println("-------------------------------------------"); } }
- Class Student
class Student { //Data Member private String name, id; private int midScore, finalScore, quizScore, proScore, attentScore; public Student(String id, String name, int midScore, int finalScore, int quizScore, int proScore, int attentScore) { this.name = name; this.id = id; this.midScore = midScore; this.finalScore = finalScore; this.quizScore = quizScore; this.proScore = proScore; this.attentScore = attentScore; } //id public String getId() { return id; } //name public String getName() { return name; } //mid public int getMid() { return midScore; } //final public int getFinal() { return finalScore; } //quiz public int getQuiz() { return quizScore; } //pro public int getPro() { return proScore; } //attent public int getAttent() { return attentScore; } public int getTotal() { return midScore + finalScore + quizScore + proScore + attentScore; } }
Reference
Discover more from naiwaen@DebuggingSoft
Subscribe to get the latest posts sent to your email.