import java.util.Scanner; public class Money4 { public static void main(String[] args){ int Array = 5; String[] name = new String[Array]; //ประกาศตัวแปรรับชื่อสินค้าเป็นอาเรย์ int[] unit = new int[Array]; //ประกาศตัวแปรรับจำนวนสินค้าเป็นอาเรย์ double[] price = new double [Array]; //ประกาศตัวแปรรับราคาินค้าเป็นอาเรย์ String tmpName; double sumProduce = 0; // เก็บผลรวม double[] sumPrice = new double[Array]; double money = 0; // เก็บจำนวนเงิน Scanner sc = new Scanner(System.in); //ประกาศ instance สำหรับรับข้อมูลทาง keyboard for(int i = 0; i<Array; i++){ System.out.println("============================IST SHOP========================================"); System.out.println("product number "+(i+1)+"."); //System.out.print("name : "); //name = sc.nextLine(); //รอรั่าทางคีย์อร์ดก่อน รับแล้วถึงลงมาทำบรรทัดต่อไป //nameLoop: boolean yes; do{ yes = true; System.out.print("name : "); tmpName = sc.nextLine(); if(tmpName.equals("hanaga")){ System.out.print("name invalid \n"); //continue; yes = false; } for(int j=0; j<Array; j++){ if(tmpName.equalsIgnoreCase(name[j])){ System.out.println("order duplicated"); //continue nameLoop; yes = false; } } //break; }while (! yes); name[i] = tmpName; //System.out.print("unit : "); //unit = sc.nextInt(); //รอรั่าทางคีย์อร์ดก่อน รับแล้วถึงลงมาทำบรรทัดต่อไป do{ System.out.print("unit : "); unit[i] = sc.nextInt(); if(unit[i] < 0){ System.out.print("unit invalid \n"); } }while (unit[i] < 0); //System.out.print("price : "); //price = sc.nextInt(); //รอรั่าทางคีย์อร์ดก่อน รับแล้วถึงลงมาทำบรรทัดต่อไป do{ System.out.print("price : "); price[i] = sc.nextDouble(); if(price[i] < 0){ System.out.print("price invalid \n"); } }while (price[i] < 0); //System.out.println("name by keyboard = "+name); //System.out.println("name by keyboard = "+unit); //System.out.println("name by keyboard = "+price); sumPrice[i] = unit[i] * price[i]; sumProduce = sumProduce + (unit[i] * price[i]); sc.nextLine(); } System.out.println("============================================================================"); System.out.println("============================Display Order==================================="); StringBuffer bf; for(int i = 0; i<Array; i++){ bf = new StringBuffer(); bf.append("order "); bf.append((i+1)+":"); bf.append(" "+name[i]+" "); bf.append(sumPrice[i]+" "); bf.append("bath"); System.out.println(bf.toString()); } System.out.println("============================================================================"); double totalPrice = sumProduce; System.out.println("totalPrice = "+totalPrice); //System.out.print("money = "); //double money = sc.nextInt(); do{ System.out.print("money = "); money = sc.nextInt(); }while (money < 0 || money < totalPrice); System.out.println("money by keboard = "+money); double a = money - totalPrice; System.out.println("a ="+a); double b1000 = a / 1000; double bm1000 = (float)a % 1000; //System.out.println("1000 => "+(int)b1000); //System.out.println("1000 => "+bm1000); double b500 = (float)bm1000 / 500; double bm500 = (float)bm1000 % 500; //System.out.println("500 => "+(int)b500); //System.out.println("500 => "+bm500); double b100 = (float)bm500 / 100; double bm100 = (float)bm500 % 100; //System.out.println("100 => "+(int)b100); //System.out.println("100 => "+bm100); double b50 = (float)bm100 / 50; double bm50 = (float)bm100 % 50; //System.out.println("50 => "+(int)b50); //System.out.println("50 => "+bm50); double b20 = (float)bm50 / 20; double bm20 = (float)bm50 % 20; //System.out.println("20 => "+(int)b20); //System.out.println("20 => "+bm20); double b10 = (float)bm20 / 10; double bm10 = (float)bm20 % 10; //System.out.println("10 => "+(int)b10); //System.out.println("10 => "+bm10); double b5 = (float)bm10 / 5; double bm5 = (float)bm10 % 5; //System.out.println("5 => "+(int)b5); //System.out.println("5 => "+bm5); double b2 = (float)bm5 / 2; double bm2 = (float)bm5 % 2; //System.out.println("2 => "+(int)b2); //System.out.println("2 => "+bm2); double b1 = (float)bm2 / 1; double bm1 = (float)bm2 % 1; //System.out.println("1 => "+(int)b1); //System.out.println("1 => "+bm1); double b0_50 = (float)(bm1 / 0.50); double bm0_50 = (float)(bm1 % 0.50); //System.out.println("0_50 => "+(int)b0_50); //System.out.println("0_50 => "+bm50); double b0_25 = (float)(bm0_50 / 0.25); //System.out.println("c0_25 => "+(int)b0_25); //System.out.println("1 => "+bm0_25); String x1000 = b1000 == 0 ? "No": ""+(int)b1000+"unit"; String x500 = b500 == 0 ? "No": ""+(int)b500+"unit"; String x100 = b100 == 0 ? "No": ""+(int)b100+"unit"; String x50 = b50 == 0 ? "No": ""+(int)b50+"unit"; String x20 = b20 == 0 ? "No": ""+(int)b20+"unit"; String x10 = b10 == 0 ? "No": ""+(int)b10+"unit"; String x5 = b5 == 0 ? "No": ""+(int)b5+"unit"; String x2 = b2 == 0 ? "No": ""+(int)b2+"unit"; String x1 = b1 == 0 ? "No": ""+(int)b1+"unit"; String x0_50 = b0_50 == 0 ? "No": ""+(int)b0_50+"unit"; String x0_25 = b0_25 == 0 ? "No": ""+(int)b0_25+"unit"; if(b1000 > 0) System.out.println("1000 => "+x1000); if(b500 > 0) System.out.println("500 => "+x500); if(b100 > 0) System.out.println("100 => "+x100); if(b50 > 0) System.out.println("50 => "+x50); if(b20 > 0) System.out.println("20 => "+x20); if(b10 > 0) System.out.println("10 => "+x10); if(b5 > 0) System.out.println("5 => "+x5); if(b2 > 0) System.out.println("2 => "+x2); if(b1 > 0) System.out.println("1 => "+x1); if(b0_50 > 0) System.out.println("0_50 => "+x0_50); if(b0_25 > 0) System.out.println("c0_25 => "+x0_25); } }
เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง