diff --git a/AddStudent.java b/AddStudent.java new file mode 100644 index 0000000..834d7aa --- /dev/null +++ b/AddStudent.java @@ -0,0 +1,29 @@ +import java.util.Scanner; +public class AddStudent{ + public static void main(String[] args){ + Scanner sc=new Scanner(System.in); + System.out.print("Enter a fullname : "); + String fullname=sc.nextLine(); + int i=0; + while(i display = new TreeSet<>(); + System.out.print("Enter numbers 1 : "); + String[] num1Before = sc.nextLine().split(" "); + double[] num1 = new double[num1Before.length]; + for (int i = 0; i < num1Before.length; i++) { + num1[i] = Double.parseDouble(num1Before[i]); + } + System.out.print("Enter numbers 2 : "); + String[] num2Before = sc.nextLine().split(" "); + double[] num2 = new double[num2Before.length]; + for (int i = 0; i < num2Before.length; i++) { + num2[i] = Double.parseDouble(num2Before[i]); + } + for (int i = 0; i < num1.length; i++) { + for (int j = 0; j < num2.length; j++) { + if (num1[i] == num2[j]) { + countEquals++; + display.add(num2[j]); + } + } + } + System.out.println("Equals : " + countEquals); + System.out.print("The number of equals is "); // 3.0 3.5 + for (double elements : display) { + System.out.print((elements == Math.ceil(elements)) ? ft.format(elements) + " " : elements + " "); + } + System.out.println(); + } +} \ No newline at end of file diff --git a/ComputeGrade.java b/ComputeGrade.java new file mode 100644 index 0000000..7fa7935 --- /dev/null +++ b/ComputeGrade.java @@ -0,0 +1,25 @@ +import java.util.Scanner; +public class ComputeGrade{ + public static void main(String[] args){ + Scanner sc=new Scanner(System.in); + System.out.print("Enter your scores : "); + double score=sc.nextDouble(); + char grade='?'; + if(score>79){ + grade='A'; + } + else if(score>=70 && score<=79){ + grade='B'; + } + else if(score>=60 && score<=69){ + grade='C'; + } + else if(score>=50 && score<=59){ + grade='D'; + } + else{ + grade='F'; + } + System.out.println(grade); + } +} diff --git a/ConnectionCreator.java b/ConnectionCreator.java new file mode 100644 index 0000000..bc50335 --- /dev/null +++ b/ConnectionCreator.java @@ -0,0 +1,11 @@ +public class ConnectionCreator { + public static Connection building(){ + Connection con=null; + try { + con=DriverManager.getConnection("jdbc:derby://localhost:1527/sample","app","app"); + } catch (SQLException ex) { + Logger.getLogger(ConnectionCreator.class.getName()).log(Level.SEVERE, null, ex); + } + return con; + } +} diff --git a/CountWord.java b/CountWord.java new file mode 100644 index 0000000..77505b0 --- /dev/null +++ b/CountWord.java @@ -0,0 +1,88 @@ +package testheap; +import java.util.Scanner; +import java.util.TreeSet; +public class CountWord{ + public static void main(String[] args){ + Scanner sc=new Scanner(System.in); + int round=0; + char [] checkLower=new char[52]; + for(int i=97;i<=122;i++){ + checkLower[i-97]=(char)i; + } + for(int i=65;i<=90;i++){ + checkLower[i-39]=(char)i; // 26-51 + } + System.out.print("Enter a string : "); + char[] strArray=sc.next().toCharArray(); + int [][] count=new int[52][strArray.length]; + int i=0; + for(int j=0;j 1 + count[j][round]++; + } + else{ + round++; + i=round; + if(i!=strArray.length){ + continue; + } + else{ + i=0; + round=0; + j++; + } + } + i++; + if(i==strArray.length){ + round++; + i=round; + if(i!=strArray.length){ + continue; + } + else{ + i=0; + round=0; + j++; + } + } + } + int [] maxBefore=new int[52]; + int [] maxBeforeIndex=new int[52]; + for(i=0;imaxBefore[i]){ + maxBefore[i]=count[i][j]; + maxBeforeIndex[i]=j; + } + } + } + int j=0; + int [] maxCount=new int[52]; + for(i=0;i maxForPrint2=new TreeSet<>(); + for(i=1;imax){ + max=maxCount[i]; + maxIndex=i; + } + } + for(i=0;i>>>>>> b6a6c40dd105e06bd28b9e6293559e113c7e7a7e +package forquiz; + +/** + * + * @author oam + */ +public class DisplayH { + + public static void main(String[] args) { + for (int i = 0; i < 5; i++) { + for (int j = 1; j <= 5; j++) { + if (i == 2) { + System.out.print("*"); + } else { + if (j == 1 || j == 5) { + System.out.print("*"); + } else { + System.out.print(" "); + } + } + } + System.out.println(); + } + } +} diff --git a/DisplayIN.java b/DisplayIN.java new file mode 100644 index 0000000..9667443 --- /dev/null +++ b/DisplayIN.java @@ -0,0 +1,65 @@ +/* + I N + +***456*789X* + * * * * + * * * * + * * ** +*** * * + + */ +package forquiz; + +/** + * + * @author oam + */ +public class DisplayIN { + + public static void main(String[] args) { + for (int i = 0; i < 5; i++) { + for (int j = 1; j <= 12; j++) { + switch (i) { + case 0: + if (j == 1 || j == 2 || j == 3 || j == 7 || j == 12) { + System.out.print("*"); + } else { + System.out.print(" "); + } + break; + case 1: + if (j == 2 || j == 7 || j == 9 || j == 12) { + System.out.print("*"); + } else { + System.out.print(" "); + } + break; + case 2: + if(j==2 || j==7 || j==10 || j==12){ + System.out.print("*"); + } + else{ + System.out.print(" "); + } + break; + case 3: + if(j==2 || j==7 || j==11 || j==12){ + System.out.print("*"); + } + else{ + System.out.print(" "); + } + break; + case 4: + if (j == 1 || j == 2 || j == 3 || j == 7 || j == 12) { + System.out.print("*"); + } else { + System.out.print(" "); + } + break; + } + } + System.out.println(); + } + } +} diff --git a/FiveLine.java b/FiveLine.java new file mode 100644 index 0000000..d673dd6 --- /dev/null +++ b/FiveLine.java @@ -0,0 +1,18 @@ +public class FiveLine{ + public static void main(String[] args){ + final int PLINE=5; + int count=1; + java.util.Scanner sc=new java.util.Scanner(System.in); + System.out.print("Enter a number : "); + int end=sc.nextInt(); + for(int i=0;i<=end;i++){ + if(count%PLINE==0){ + System.out.println(i); + } + else{ + System.out.print(i+" "); + } + count++; + } + } +} diff --git a/ForRam.java b/ForRam.java new file mode 100644 index 0000000..6ee0bd6 --- /dev/null +++ b/ForRam.java @@ -0,0 +1,24 @@ +import java.util.Scanner; +import java.text.DecimalFormat; +public class ForRam{ + public static void main(String[] args){ + Scanner sc=new Scanner(System.in); + DecimalFormat ft=new DecimalFormat("0,000.00"); + System.out.print("Enter the number of terms : "); + int term=(sc.nextInt()*12)/12+1; + System.out.print("Enter the payments : "); + double payments=sc.nextDouble(); + double termOfP=0; + double interest=0; + double total=0; + System.out.println("Terms\t\tTerm of payments\tInterest\tTotal of terms\t\tPayments"); + System.out.println("0\t\t"+ft.format(termOfP)+"\t\t"+ft.format(interest)+"\t"+ft.format(total)+"\t\t"+ft.format(payments)); + termOfP=payments/(term-1); + for(int i=1;i> ncd111f uppercase a + Scanner sc=new Scanner(System.in); + System.out.print("Enter a username : "); + String username=sc.next(); + String password=username.substring(1,4)+((int)(Math.random()*999)+111)+(char)(username.charAt(username.length()-1)-32)+(char)(username.charAt(0)+32); + System.out.println("Your username is "+username+" and password is "+password); + } +} diff --git a/Id.java b/Id.java new file mode 100644 index 0000000..fa1b439 --- /dev/null +++ b/Id.java @@ -0,0 +1,14 @@ +import java.util.Scanner; +public class Id{ + public static void main(String[] args){ + Scanner sc=new Scanner(System.in); + System.out.print("Enter a number : "); + int num=sc.nextInt(); + if(num%2==0 && num%5==0){ + System.out.println(true); + } + else{ + System.out.println(false); + } + } +} diff --git a/JavaFinal b/JavaFinal new file mode 100644 index 0000000..93c4693 --- /dev/null +++ b/JavaFinal @@ -0,0 +1,176 @@ +//Quiz1 +class Quiz1{ + public static void main(String[] args){ + double y = 3; + double sum = 0; + for(int x = 1;x<=49;x++){ + sum += (x/y); + y += 2; + } + //p'thery -> for(int x = 1;x<=49;sum += (x/y),x++,y += 2); + System.out.print(sum); + } +} + +//Quiz2 +xxxo +xxxoo +xxxooo +xxxoooo +oooooooo +ooooooooo +oooooooo +xxxoooo +xxxooo +xxxoo +xxxo + + +//Quiz3 +class Quiz3{ + public static boolean equalArrays(int a1[], int a2[]){ + if(a1.length != a2.length){ + return false; + } + for(int i = 0;i=1 & no<=9) + dNo = "000"+no; + else if(no<=99) + dNo = "00"+no; + else if(no<=999) + dNo = "0"+no; + else if(no<=9999) + dNo = ""+no; + + deliveryNumber = ""+year+dNo; + + if(code == 1){ + if(weight<5) + fee = 10.00; + else if(weight<=20) + fee = 25.50; + else if(weight>20) + fee = 40.00; + } + else if(code == 2){ + if(weight<5) + fee = 50.00; + else if(weight>=5) + fee = 100.00; + } + } //end of Delivery Constructor ^^ + + //Getters Method ^^ + public String getDeliveryNumber(){ + return deliveryNumber; + } + public int getCode(){ + return code; + } + public double getWeight(){ + return weight; + } + public double getFee(){ + return fee; + } + + //Setters Method ^^ + public void setCode(int code){ + this.code = code; + } + public void setWeight(double weight){ + this.weight = weight; + } + public void setFee(double fee){ + this.fee = fee; + } + + //toString Method ^^ + public String toString(){ + return "Delivery Number : "+getDeliveryNumber()+ + "\nCode : "+getCode()+ + "\nWeight : "+getWeight()+ + "\nFee : "+getFee(); + } +} + +import java.util.Scanner; +class TestDelivery{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + int dNo = 0; + int year = 0; + double weight = 0; + int code = 0; + do{ + System.out.print("Enter Delivery Number : "); + dNo = sc.nextInt(); + } + while(dNo<1 | dNo>9999); + + do{ + System.out.print("Enter Year : "); + year = sc.nextInt(); + } + while(year<2001 | year > 2050); + + do{ + System.out.print("Enter Weight : "); + weight = sc.nextDouble(); + } + while(weight < 0.10 | weight > 100); + + do{ + System.out.print("Enter Distance Code : "); + code = sc.nextInt(); + } + while(code != 1 & code !=2); + + Delivery dl1 = new Delivery(year,dNo,code,weight); + + System.out.println(dl1); //revoke toString Method ^^ + + //Revoke Setters Method ^^ + dl1.setCode(2); + dl1.setWeight(12.50); + dl1.setFee(50.50); + + + //Revoke Getters Method ^^ + System.out.println("getDeliveryNumber() : "+dl1.getDeliveryNumber()); + System.out.println("getCode(): "+dl1.getCode()); + System.out.println("getWeight() : "+dl1.getWeight()); + System.out.println("getFee() : "+dl1.getFee()); + } } + +//Quiz5 +จำไม่ได้ค่ะ - -" +รู้แต่ว่าข้อแรกอ่ะ ไม่มีค่า เพราะจองแีรมไว้เฉยๆ ยังไม่ได้ init - -" +และก็มีข้อหลังๆ ที่หลอก มี int ตัวแปรในลูป เพราะฉะนั้นทุกๆ ครั้งที่วน ค่ามันก็จะเริ่มใหม่ +และก็ข้อบนๆ ตัวเลขที่ประกาศใน loop for ออกนอกลูป for แล้วจะหายไปนะ ^^ diff --git a/JavaII/Question2/Employee.java b/JavaII/Question2/Employee.java new file mode 100644 index 0000000..0c1284a --- /dev/null +++ b/JavaII/Question2/Employee.java @@ -0,0 +1,79 @@ +package JavaII; +public class Employee { + private static int n; + private long empId; + private String empName; + private String empSurName; + private String empPosition; + private double empSalary; + private double empBonus; + + public Employee() { + this(null,null,null,0.0,0.0); + } + + public Employee(String name, String surname, String pos, double sal, double bo) { + this.empName = name; + this.empSurName = surname; + this.empPosition = pos; + this.empSalary = sal; + this.empBonus = bo; + this.genEmployeeId(); + } + + public long genEmployeeId(){ + n++; + this.empId=20000+n; + return this.empId; + } + + public long getEmpId() { + return empId; + } + + public String getEmpName() { + return empName; + } + + public String getEmpSurName() { + return empSurName; + } + + public String getEmpPosition() { + return empPosition; + } + + public double getEmpSalary() { + return empSalary; + } + + public double getEmpBonus() { + return empBonus; + } + + public void setEmpName(String name) { + this.empName = name; + } + + public void setEmpSurName(String sur) { + this.empSurName = sur; + } + + public void setEmpPosition(String pos) { + this.empPosition = pos; + } + + public void setEmpSalary(double sal) { + this.empSalary = sal; + } + + public void setEmpBonus(double bonus) { + this.empBonus = bonus; + } + + @Override + public String toString() { + return "Employee{" + "empId=" + empId + ", empName=" + empName + ", empSurName=" + empSurName + ", empPosition=" + empPosition + ", empSalary=" + empSalary + ", empBonus=" + empBonus + '}'; + } + +} diff --git a/JavaII/Question2/TestEmployee.java b/JavaII/Question2/TestEmployee.java new file mode 100644 index 0000000..458dcb0 --- /dev/null +++ b/JavaII/Question2/TestEmployee.java @@ -0,0 +1,22 @@ +package JavaII; +public class TestEmployee { + public static void main(String[] args) { + Employee [] emp=new Employee[10]; + for(int i=0;i b) { + if (b > c) { + System.out.println("b ("+b+") is the median"); + } + else if (a > c) { + System.out.println("c ("+c+") is the median"); + } + else{ + System.out.println("a ("+a+") is the median"); + } + } + else{ + if (a > c){ + System.out.println("a ("+a+") is the median"); + } + else if (b > c){ + System.out.println("c ("+c+") is the median");; + } + else{ + System.out.println("b ("+b+") is the median"); + } + } + } +} + + + +2. รับค่าเลขจำนวนเต็มหกหลัก เอาสองตัวกลาง สองตัวหลัง มา + - * และเฉลื่ย + +import java.util.Scanner; +class Exam2{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.print("Enter Number : "); + int input = sc.nextInt(); + int mid = input%10000/100; + int last = input%100; + System.out.println(mid+" + "+last+" = "+(mid+last)); + System.out.println(mid+" - "+last+" = "+(mid-last)); + System.out.println(mid+" * "+last+" = "+(mid*last)); + System.out.println("Mean of "+mid+", "+last+" = "+((mid+last)/2.0)); + } +} + + + +3. สุ่มเบอร์ 10 หลัก 3 หลักแรกไม่มีเลข 8,9 สามหลักกลางอยู่ในช่วง100-888 ที่เหลือไม่กำหนด + +import java.text.DecimalFormat; +class Exam3{ + public static void main(String[] args){ + DecimalFormat fmt = new DecimalFormat("0000"); + int a = ((int)(Math.random() * (7+1))); + int b = ((int)(Math.random() * (7+1))); + int c = ((int)(Math.random() * (7+1))); + int d = ((int)(Math.random() * (888-100+1)+100)); + String e = (fmt.format(Math.random() * (9999+1))); + System.out.println("Tel : "+a+b+c+"-"+d+"-"+e); + } +} + + + + + +4. ถ้ากรอกตัวอักษรเล็กให้พิมพ์ตัวอักษรใหญ่ ถ้ากรอกตัวอักษรใหญ่ให้พิมพ์ตัวอักษรเล็ก + +import java.util.Scanner; +class Exam4{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.print("Enter Letter : "); + String input = sc.nextLine(); + int letter = ( (int)input.charAt(0) ); + if ( letter>='A' & letter<='Z' ){ + System.out.println( (char)(letter+32) ); + } + else if ( letter>='a' & letter<='z' ){ + System.out.println( (char)(letter-32) ); + } + else{ + System.out.println("Not an Alphabelt"); + } + } +} + + + +5. สุ่มตัวอักษร A-Z + +class Exam5{ + public static void main(String[] args){ + System.out.println( (char)((int)(Math.random() * ('Z'-'A'+1)+'A'))); + } +} + + + +6. รับตัวอักษรจากผู้ใช้ ถ้า 0-9 แสดงว่า Numbers ถ้า a-z แสดงว่า LowerCase ถ้า A-Z แสดงว่า UpperCase ถ้าอย่างอื่นให้แสดงว่า Others ถ้าเกิน 1 ตัวให้บอกว่า Invalid Character + +import java.util.Scanner; +import java.text.DecimalFormat; +class Exam6{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.print("Input : "); + String input = sc.nextLine(); + int letter = ((int)input.charAt(0)); + if( input.length() == 1 ){ + if( letter>='0' & letter <= '9' ){ + System.out.println("Numbers"); + } + else if ( letter >='a' & letter <= 'z' ){ + System.out.println("Lower Case"); + } + else if ( letter >='A' & letter <= 'Z' ){ + System.out.println("Upper Case"); + } + else{ + System.out.println("Others"); + } + } + else{ + System.out.println("Invaild Character"); + } + } +} + + + +7. รับค่า miles hours minutes seconds แล้วมาแสดงออกมาเป็นกิโลเมตรต่อชม. + +import java.util.Scanner; +import java.text.DecimalFormat; +class Exam7{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.print("Enter Miles : "); + int miles = sc.nextInt(); + System.out.print("Enter Hours : "); + int hr = sc.nextInt(); + System.out.print("Enter Minutes : "); + int min = sc.nextInt(); + System.out.print("Enter Seconds : "); + int sec = sc.nextInt(); + + double time = hr+(min/60.0)+(sec/60.0/60.0); + + DecimalFormat fmt = new DecimalFormat("0.00"); + System.out.println("Result : "+ fmt.format((miles*1.609344)/time)+" KM/Hr"); + } +} diff --git a/Location.java b/Location.java new file mode 100644 index 0000000..0a46bbd --- /dev/null +++ b/Location.java @@ -0,0 +1,38 @@ +import java.util.Scanner; + +public class Location { + + public int row; + public int column; + public double maxValue; + + public static Location locateLargest(double[][] a) { + Location forReturn = new Location(); + for (int i = 0; i < a.length; i++) { + for (int j = 0; j < a[i].length; j++) { + if (a[i][j] > forReturn.maxValue) { + forReturn.maxValue = a[i][j]; + forReturn.row = i; + forReturn.column = j; + } + } + } + return forReturn; + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter the number of row and collumn of array: "); + int row = sc.nextInt(); + int column = sc.nextInt(); + double size[][] = new double[row][column]; + System.out.print("Enter the array: "); + for (int i = 0; i < row; i++) { + for (int j = 0; j < column; j++) { + size[i][j] = sc.nextDouble(); + } + } + Location display = Location.locateLargest(size); + System.out.println("The location of largest element is " + display.maxValue + " at (" + display.row + "," + display.column + ")"); + } +} \ No newline at end of file diff --git a/ProductModel.java b/ProductModel.java new file mode 100644 index 0000000..bf04034 --- /dev/null +++ b/ProductModel.java @@ -0,0 +1,310 @@ +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.text.DecimalFormat; +import java.util.LinkedList; +import java.util.logging.Level; +import java.util.logging.Logger; + +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +/** + * + * @author ESSser + */ +public class ProductModel extends javax.swing.JFrame { + + /** + * Creates new form ProductModel + */ + LinkedList productList = new LinkedList<>(); + LinkedList proId = new LinkedList<>(); + DecimalFormat ft = new DecimalFormat("0,000"); + + public ProductModel() { + initComponents(); + loadProduct(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // + private void initComponents() { + + jComboBox1 = new javax.swing.JComboBox(); + jScrollPane3 = new javax.swing.JScrollPane(); + jTextArea2 = new javax.swing.JTextArea(); + jLabel1 = new javax.swing.JLabel(); + jScrollPane1 = new javax.swing.JScrollPane(); + productId = new javax.swing.JList(); + pc = new javax.swing.JTextField(); + jLabel2 = new javax.swing.JLabel(); + qOnH = new javax.swing.JTextField(); + aval = new javax.swing.JCheckBox(); + jLabel3 = new javax.swing.JLabel(); + jButton1 = new javax.swing.JButton(); + jButton2 = new javax.swing.JButton(); + jScrollPane2 = new javax.swing.JScrollPane(); + jTextArea1 = new javax.swing.JTextArea(); + jLabel4 = new javax.swing.JLabel(); + jLabel5 = new javax.swing.JLabel(); + jLabel6 = new javax.swing.JLabel(); + jLabel7 = new javax.swing.JLabel(); + jScrollPane4 = new javax.swing.JScrollPane(); + desc = new javax.swing.JTextArea(); + + jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); + + jTextArea2.setColumns(20); + jTextArea2.setRows(5); + jScrollPane3.setViewportView(jTextArea2); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + jLabel1.setText("Purchase Cost"); + + productId.setModel(new javax.swing.AbstractListModel() { + String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; + public int getSize() { return strings.length; } + public Object getElementAt(int i) { return strings[i]; } + }); + productId.addListSelectionListener(new javax.swing.event.ListSelectionListener() { + public void valueChanged(javax.swing.event.ListSelectionEvent evt) { + selectionEvent(evt); + } + }); + jScrollPane1.setViewportView(productId); + + jLabel2.setText("Quantity On Hand"); + + qOnH.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + qOnHActionPerformed(evt); + } + }); + + aval.setText("Available"); + + jLabel3.setText("Description"); + + jButton1.setText("Save"); + + jButton2.setText("Exit"); + + jTextArea1.setColumns(20); + jTextArea1.setRows(5); + jScrollPane2.setViewportView(jTextArea1); + + jLabel4.setText("Code"); + + jLabel5.setText("Discount Code"); + + jLabel6.setText("Description"); + + jLabel7.setText("Product ID"); + + desc.setColumns(20); + desc.setRows(5); + jScrollPane4.setViewportView(desc); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addGroup(layout.createSequentialGroup() + .addGap(42, 42, 42) + .addComponent(jLabel4) + .addGap(81, 81, 81) + .addComponent(jLabel5) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jLabel6)) + .addGroup(layout.createSequentialGroup() + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButton2))) + .addGap(36, 36, 36)) + .addGroup(layout.createSequentialGroup() + .addGap(34, 34, 34) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jLabel7) + .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addContainerGap()) + .addGroup(layout.createSequentialGroup() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jLabel1)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(pc, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE) + .addComponent(qOnH))) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jLabel3) + .addComponent(aval, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addGap(12, 12, 12) + .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel7) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton2) + .addComponent(jButton1)) + .addGap(26, 26, 26) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel4) + .addComponent(jLabel5) + .addComponent(jLabel6))) + .addGroup(layout.createSequentialGroup() + .addGap(29, 29, 29) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel1) + .addComponent(pc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel2) + .addComponent(qOnH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(18, 18, 18) + .addComponent(aval) + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jLabel3) + .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(42, Short.MAX_VALUE)) + ); + + pack(); + }// + + private void qOnHActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } + + private void selectionEvent(javax.swing.event.ListSelectionEvent evt) { + int index = proId.indexOf((Integer) (productId.getSelectedValue())); + Product first = productList.get(index); + pc.setText(ft.format(first.getPurchaseCost())); + qOnH.setText(ft.format(first.getQuantityOnHand())); + aval.setSelected(first.isAvailable()); + desc.setText(first.getDescription()); + } + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(ProductModel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(ProductModel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(ProductModel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(ProductModel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new ProductModel().setVisible(true); + } + }); + } + // Variables declaration - do not modify + private javax.swing.JCheckBox aval; + private javax.swing.JTextArea desc; + private javax.swing.JButton jButton1; + private javax.swing.JButton jButton2; + private javax.swing.JComboBox jComboBox1; + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jLabel2; + private javax.swing.JLabel jLabel3; + private javax.swing.JLabel jLabel4; + private javax.swing.JLabel jLabel5; + private javax.swing.JLabel jLabel6; + private javax.swing.JLabel jLabel7; + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JScrollPane jScrollPane2; + private javax.swing.JScrollPane jScrollPane3; + private javax.swing.JScrollPane jScrollPane4; + private javax.swing.JTextArea jTextArea1; + private javax.swing.JTextArea jTextArea2; + private javax.swing.JTextField pc; + private javax.swing.JList productId; + private javax.swing.JTextField qOnH; + // End of variables declaration + + private void loadProduct() { + Product forStoring = null; + Connection con = ConnectionCreator.building(); + try { + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery("select * from APP.PRODUCT"); + while (rs.next()) { + forStoring = new Product(); + forStoring.setId(rs.getInt(1)); + forStoring.setPurchaseCost(rs.getDouble(4)); + forStoring.setQuantityOnHand(rs.getInt(5)); + forStoring.setAvailable(rs.getBoolean(7)); + forStoring.setDescription(rs.getString(8)); + productList.add(forStoring); + proId.add(rs.getInt(1)); + } + Product first = productList.getFirst(); + pc.setText(ft.format(first.getPurchaseCost())); + qOnH.setText(ft.format(first.getQuantityOnHand())); + aval.setSelected(first.isAvailable()); + desc.setText(first.getDescription()); + Integer[] revId = new Integer[productList.size()]; + int i = 0; + for (Product elements : productList) { + revId[i] = elements.getId(); + i++; + } + java.util.Arrays.sort(revId); + productId.removeAll(); + productId.setListData(revId); + } catch (SQLException ex) { + Logger.getLogger(ProductModel.class.getName()).log(Level.SEVERE, null, ex); + } + + } +} diff --git a/Question8_3.java b/Question8_3.java new file mode 100644 index 0000000..dc5bbbe --- /dev/null +++ b/Question8_3.java @@ -0,0 +1,27 @@ +import java.util.Date; +public class Question8_3 { + public static void main(String[] args) { + int k=0; + Date [] d=new Date[9]; + for(int i=0;i=4 && i<=7 && k!=0){ + d[i+1]=new Date((long)Math.pow(10,(4+(i)))); + } + System.out.println(d[i].toString()); + } + } +} diff --git a/README.md b/README.md index eb836de..2096e00 100644 --- a/README.md +++ b/README.md @@ -1,4 +1 @@ -java -==== - -java \ No newline at end of file +Java Sample Programs diff --git a/Report.java b/Report.java new file mode 100644 index 0000000..85bd174 --- /dev/null +++ b/Report.java @@ -0,0 +1,14 @@ +package report; + +/** + * + * @author Student Lab + */ +public interface Report { + String getHeader(); + String getBody(); + String getFooter(); + void setHeader(String header); + void setBody(String body); + void setFooter(String footer); +} diff --git a/Reverse.java b/Reverse.java new file mode 100644 index 0000000..fdcfb93 --- /dev/null +++ b/Reverse.java @@ -0,0 +1,8 @@ +public class Reverse { + public static void main(String[] args){ + java.util.Scanner sc=new java.util.Scanner(System.in); + System.out.print("Enter a number : "); + int num=sc.nextInt(); + System.out.println("The result is "+(num%10)+""+((num/10)%10)+""+(num/100)); + } +} diff --git a/ShiftFollowIndex.java b/ShiftFollowIndex.java new file mode 100644 index 0000000..e5aac6d --- /dev/null +++ b/ShiftFollowIndex.java @@ -0,0 +1,36 @@ +public class ShiftFollowIndex { + public static void main(String[] args) { + java.util.Scanner sc=new java.util.Scanner(System.in); + System.out.print("Enter a letter : "); + String letter=sc.nextLine(); + while(letter.length()<=0 || letter.length()>300){ + System.out.print("Try again ! , Enter a letter : "); + letter=sc.nextLine(); + } + char [] forShift=letter.toCharArray(); + for(int i=0;i90){ + System.out.print((char)(((forShift[i]+i)-65)%26+65)); + } + else if(Character.isLowerCase(forShift[i]) && (forShift[i]+i)>122){ + System.out.print((char)(((forShift[i]+i)-97)%26+97)); + } + else{ + System.out.print((char)(forShift[i]+i)); + } + break; + } + } + } +} diff --git a/ShiftWords.java b/ShiftWords.java new file mode 100644 index 0000000..e6a60f2 --- /dev/null +++ b/ShiftWords.java @@ -0,0 +1,36 @@ +public class ShiftWords { + public static void main(String[] args) { + java.util.Scanner sc=new java.util.Scanner(System.in); + System.out.print("Enter a letter : "); + String letter=sc.nextLine(); + while(letter.length()<=0 || letter.length()>300){ + System.out.print("Try again ! , Enter a letter : "); + letter=sc.nextLine(); + } + char [] forShift=letter.toCharArray(); + for(char e : forShift){ + switch(e){ + case '^': + case '&': + case '#': + case '@': + case '*': + case '(': + case '!': + case '/': + case ' ': System.out.print(e); break; + default : + if(!Character.isLowerCase(e) && (e+3)>90){ + System.out.print((char)(((e+3)-65)%26+65)); + } + else if(Character.isLowerCase(e) && (e+3)>122){ + System.out.print((char)(((e+3)-97)%26+97)); + } + else{ + System.out.print((char)(e+3)); + } + break; + } + } + } +} diff --git a/Tester.java b/Tester.java new file mode 100644 index 0000000..b5b9341 --- /dev/null +++ b/Tester.java @@ -0,0 +1,7 @@ +public class Tester { + public static void main(String arg[]){ + Transaction t1 = new Transaction(); + t1.writeAccountObject(); + t1.readAccountObject(); + } +} diff --git a/Transaction.java b/Transaction.java new file mode 100644 index 0000000..dd11d47 --- /dev/null +++ b/Transaction.java @@ -0,0 +1,142 @@ +import java.util.*; +import java.text.*; +import java.io.ObjectOutputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.FileInputStream; + +public class Transaction implements Report { + + private BankAccount account[]; + private String header; + private String body; + private String footer; + + public Transaction() { + + account = new BankAccount[10]; + Address add1 = new Address("111", "Pracha-U-thid 45", "Pracha-U-thid", "Thung-Kru", "Bangkok", 10140); + Address add2 = new Address("402", "Pracha-U-thid 48", "Pracha-U-thid", "Thung-Kru", "Bangkok", 10140); + Address add3 = new Address("100", "Pracha-U-thid 44", "Pracha-U-thid", "Thung-Kru", "Bangkok", 10140); + Address add4 = new Address("103", "Pracha-U-thid 46", "Pracha-U-thid", "Thung-Kru", "Bangkok", 10140); + Address add5 = new Address("205", "Pracha-U-thid 55", "Pracha-U-thid", "Thung-Kru", "Bangkok", 10140); + Address add6 = new Address("189", "Pracha-U-thid 79", "Pracha-U-thid", "Thung-Kru", "Bangkok", 10140); + Address add7 = new Address("199", "Pracha-U-thid 80", "Pracha-U-thid", "Thung-Kru", "Bangkok", 10140); + Address add8 = new Address("181", "Pracha-U-thid 78", "Pracha-U-thid", "Thung-Kru", "Bangkok", 10140); + Address add9 = new Address("134", "Pracha-U-thid 90", "Pracha-U-thid", "Thung-Kru", "Bangkok", 10140); + Address add10 = new Address("11", "Pracha-U-thid 105", "Pracha-U-thid", "Thung-Kru", "Bangkok", 10140); + + + String ph0[] = {"0-24709850"}; + String ph1[] = {"081-123-4567", "0-24709850"}; + String ph2[] = {"0" + (int) (Math.random() * 10000000)}; + String ph3[] = {"0" + (int) (Math.random() * 10000000)}; + String ph4[] = {"0" + (int) (Math.random() * 10000000)}; + String ph5[] = {"0" + (int) (Math.random() * 10000000)}; + String ph6[] = {"0" + (int) (Math.random() * 10000000)}; + String ph7[] = {"0" + (int) (Math.random() * 10000000)}; + String ph8[] = {"0" + (int) (Math.random() * 10000000)}; + String ph9[] = {"0" + (int) (Math.random() * 10000000), "0" + (int) (Math.random() * 10000000)}; + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + String input = "1980-12-01"; + Date t = null; + try { + t = sdf.parse(input); + } catch (ParseException e) { + System.out.println("Unparseable using " + sdf); + } + + Customer custs[] = new Customer[10]; + custs[0] = new Customer("somkiat", "kiatpisarn", add1, ph0, "somkiat@gmail.com", t); + custs[1] = new Customer("suttiporn", "saisuwan", add2, ph1, "suttiporn@gmail.com", new Date(1999-1900,04,01)); + custs[2] = new Customer("vasinee", "wattanawat", add3, ph2, "vasinee@gmail.com", new Date(1986-1900,00,31)); + custs[3] = new Customer("adisorn", "meeboon", add4, ph3, "adisorn@gmail.com", new Date(1990-1900,01,01)); + custs[4] = new Customer("onkamon", "wongwisa", add5, ph4, "wongwisa@gmail.com", new Date(1991-1900,02,11)); + custs[5] = new Customer("charoensri", "kiatpisarn", add6, ph5, "charoensri@gmail.com", new Date(1980-1900,03,03)); + custs[6] = new Customer("paisarn", "meeboon", add7, ph6, "sathit@gmail.com", new Date(1981-1900,04,14)); + custs[7] = new Customer("sathit", "charoensri", add8, ph7, "somkiat@gmail.com", new Date(1992-1900,05,24)); + custs[8] = new Customer("somrak", "wattanawat", add9, ph8, "somrak@gmail.com", new Date(1999-1900,06,30)); + custs[9] = new Customer("somkiat", "parnpiti", add10, ph9, "parnpiti@gmail.com", new Date(1999-1900,07,10)); + + for (int i = 0; i < custs.length; i++) { + if (i % 2 == 0) { + account[i] = new SavingsAccount(custs[i], 500, 5); + } else { + account[i] = new CurrentDepositAccount(custs[i], account[i - 1].getAccId(), 100000); + } + } + + } + + public void writeAccountObject(){ + try{ + ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("account.dat")); + for(BankAccount e : this.account){ + oos.writeObject(e); + } + oos.close(); + } + catch(IOException ioe){ + ioe.printStackTrace(); + } + } + + public void readAccountObject(){ + ObjectInputStream ois=null; + try{ + ois=new ObjectInputStream(new FileInputStream("account.dat")); + while(true){ + BankAccount ba=(BankAccount)(ois.readObject()); + System.out.println(ba); + } + } + catch(IOException | ClassNotFoundException eof){ + try{ + ois.close(); + } + catch(IOException iof){ + System.out.println("IO Error"); + } + } + } + + @Override + public String getHeader() { + return header; + } + + @Override + public String getBody() { + return body; + } + + @Override + public String getFooter() { + return footer; + } + + @Override + public void setHeader(String header) { + this.header = header; + } + + @Override + public void setBody(String body) { + this.body = body; + } + + @Override + public void setFooter(String footer) { + this.footer = footer; + } + + public String toString() { + String result = ""; + for (int i = 0; i < account.length; i++) { + result += account[i] + "\n" + "------------------------------------------------------------------------------------------------------------------------------" + "\n"; + } + return result; + } +} diff --git a/bankaccountquiz/BankAccount.java b/bankaccountquiz/BankAccount.java new file mode 100644 index 0000000..1907090 --- /dev/null +++ b/bankaccountquiz/BankAccount.java @@ -0,0 +1,90 @@ +package bankaccountquiz; +import java.util.Date; +/** + * + * @author Ares + */ +public class BankAccount { + + private long accId; + private double balance; + private Date dateCreated; + private Customer cust; + private static final String ACCTYPE=""; + private static int generate_id=10001; + + public BankAccount() { + } + + public String getAccType(){ + return BankAccount.ACCTYPE; + } + + public long getAccId() { + return accId; + } + + public long genAccountId(){ + accId=generate_id++; + return accId; + } + + public double getBalance() { + return balance; + } + + private void setBalance(double balance) { + this.balance = balance; + } + + public Date getDateCreated() { + return dateCreated; + } + + public void setDateCreated(Date dateCreated) { + this.dateCreated = dateCreated; + } + + public Customer getCust() { + return cust; + } + + public void setCust(Customer cust) { + this.cust = cust; + } + + public void deposit(double amount){ + if(!(amount<0)){ + this.setBalance(this.getBalance()+amount); + } + else{ + System.err.println("Error from deposit : Amount is less than zero."); + } + } + + public void withdraw(double amount){ + if(!(amount<0 || (amount>this.getBalance()))){ + this.setBalance(this.getBalance()-amount); + } + else{ + System.err.println("Error from withdraw : Amount is less than zero or amount is greater than your balance."); + } + } + + public void transfer(BankAccount target,double amount){ + if(!(amount<0 || (amount>this.getBalance()))){ + this.setBalance(this.getBalance()-amount); + target.deposit(amount); + } + else{ + + } + } + + @Override + public String toString() { + return "BankAccount{" + "accId=" + accId + ", balance=" + balance + ", dateCreated=" + dateCreated + ", cust=" + cust + ", ACCTYPE="+BankAccount.ACCTYPE+'}'; + } + + +} diff --git a/bankaccountquiz/CurrentDepositAccount.java b/bankaccountquiz/CurrentDepositAccount.java new file mode 100644 index 0000000..525bacf --- /dev/null +++ b/bankaccountquiz/CurrentDepositAccount.java @@ -0,0 +1,32 @@ +package bankaccountquiz; + +/** + * + * @author Ares + */ +public class CurrentDepositAccount extends BankAccount { + private static final String ACCTYPE="Current Deposit Account"; + private long linkSavingsAccount; + + public CurrentDepositAccount() { + } + + @Override + public String getAccType() { + return CurrentDepositAccount.ACCTYPE; + } + + public long getLinkSavingsAccount() { + return linkSavingsAccount; + } + + public void setLinkSavingsAccount(long linkSavingsAccount) { + this.linkSavingsAccount = linkSavingsAccount; + } + + @Override + public String toString() { + return "CurrentDepositAccount{" +super.toString() + ", linkSavingsAccount=" + linkSavingsAccount + '}'; + } + +} diff --git a/bankaccountquiz/Customer.java b/bankaccountquiz/Customer.java new file mode 100644 index 0000000..aa9639b --- /dev/null +++ b/bankaccountquiz/Customer.java @@ -0,0 +1,8 @@ +package bankaccountquiz; +/** + * + * @author Ares + */ +public class Customer { + // do something +} diff --git a/bankaccountquiz/SavingsAccount.java b/bankaccountquiz/SavingsAccount.java new file mode 100644 index 0000000..7b225ec --- /dev/null +++ b/bankaccountquiz/SavingsAccount.java @@ -0,0 +1,59 @@ +package bankaccountquiz; + +/** + * + * @author Ares + */ +public class SavingsAccount extends BankAccount { + private static final String ACCTYPE="Savings Account"; + private double interestRate; + private static double TRANSFER_FEE=20; + + public SavingsAccount() { + } + + public double getInterestRate() { + return interestRate; + } + + public void setInterestRate(double interestRate) { + this.interestRate = interestRate; + } + + public void addInterest(){ + double balance=super.getBalance()*this.interestRate; + super.deposit(balance); + } + + @Override + public String getAccType(){ + return SavingsAccount.ACCTYPE; + } + + @Override + public void transfer(BankAccount target, double amount) { + if(target instanceof CurrentDepositAccount){ + CurrentDepositAccount tg=(CurrentDepositAccount)target; + if(this.getAccId()==tg.getLinkSavingsAccount()){ + super.withdraw(amount+SavingsAccount.TRANSFER_FEE); + target.deposit(amount); + } + else{ + super.withdraw(amount); + target.deposit(amount); + } + } + else{ + super.withdraw(amount); + target.deposit(amount); + } + } + + @Override + public String toString() { + return "SavingsAccount{"+super.toString() + ", interestRate=" + interestRate + '}'; + } + + + +} diff --git a/bankaccountquiz/TestBankAccount.java b/bankaccountquiz/TestBankAccount.java new file mode 100644 index 0000000..915a777 --- /dev/null +++ b/bankaccountquiz/TestBankAccount.java @@ -0,0 +1,17 @@ +package bankaccountquiz; + +/** + * + * @author Ares + */ +public class TestBankAccount { + public static void main(String[] args) { + BankAccount ba=new SavingsAccount(); + BankAccount ba1=new CurrentDepositAccount(); + ba.deposit(3000); + CurrentDepositAccount ba1Tran=(CurrentDepositAccount)ba1; + ba1Tran.setLinkSavingsAccount(1); + ba.transfer(ba1Tran,300); + System.out.println(ba.getBalance()); + } +}