1. 程式人生 > >商品資訊系統五個小功能

商品資訊系統五個小功能

package pre.day04;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

public class PhoneProductInformation {

	public static void main(String[] args) {
		String[] product= {
				"三星Galaxy","iphoneX","華為P20","小米","vivo","oppo"
		};
		int[] quantity= {88,66,99,100,233,233
		};
		double[] dj= {
				5399.0,8888.0,3999.0,1999.0,2666.0,1999.0
		};
		System.out.println("1.商品列表");
		System.out.println("2.錄入商品");
		System.out.println("3.商品查詢");
		System.out.println("4.資料統計");
		System.out.println("5.退出程式");
		System.out.println();
		while(true) {
			Scanner scan=new Scanner(System.in);
		
		System.out.println("請輸入要選擇的功能:");
		int number=scan.nextInt();
		switch(number) {
		case 1:
			for(int i=0;i<product.length;i++) {
				System.out.print("商品"+ (i+1) +" "+product[i]);
				System.out.print(" ");
				for (int j=0;j<quantity.length;j++) {
					if (j==i) {
						System.out.print("數量: "+quantity[j]);
						System.out.print(" ");
						
						for (int s=0;s<dj.length;s++) {
							if(s==j) {
								System.out.print("單價:"+dj[s]);
								System.out.println(" ");
							}
						}
					}
					
				}
			}
			break;
		case 2:
			System.out.println("請輸入新增商品的名稱:");
			product=Arrays.copyOf(product, product.length +1);
			product[product.length -1]=scan.next();
			System.out.println("請輸入商品數量:");
			quantity=Arrays.copyOf(quantity,quantity.length+1);
			quantity[quantity.length-1]=scan.nextInt();
			System.out.println("請輸入商品單價");
			dj=Arrays.copyOf(dj,dj.length+1);
			dj[dj.length-1]=scan.nextDouble();
			System.out.println();			
			break;
		case 3:
			System.out.println("請輸入您要查詢的手機:");
			String check=scan.next();
			for (int i=0;i<product.length;i++) {
				if(check.equals(product[i])) {
					System.out.println("商品:"+product[i]+" 數量: "+quantity[i]+" 單價: "+dj[i]);
									
					break;
			}
			
			}break;
		case 4:
			System.out.println("計算所有商品的單價平均價:");
			double sum=0.0;
			for(int i=0;i<dj.length;i++) {
				sum = sum+dj[i];
				
			}
			System.out.println("單價平均價:"+(sum/dj.length));
			break;
		case 5:
			System.out.println("退出程式");
		 
			return;
			default:
				System.out.println("輸入錯誤");
				
				
				
		}
		}
		

	}

}