超市商品管理系統-----------Java基礎班

個人覺的代碼沒問題,運行也可以,就是功能流程可以,實現沒反應
啊啊啊啊啊啊,好難
我的:

/*
 * 商品類,用於定義商品屬性
 * 商品ID、商品名、商品價格
 */
public class Shangping {
   int ID;
   String name;
   double price;
}

import java.util.ArrayList;
import java.util.Scanner;
/*
 * 實現超市商品管理系統
 * 功能:
 * 1.查詢商品清單
 * 2.增加商品
 * 3.刪除商品
 * 4.修改商品
 * 5.退出
 * 
 * 定義六個方法:初始化商品;+五個功能
 * 
 */
public class SuperMarketmainmeth {
	public static void main(String[] args) {
		
		ArrayList<Shangping> array = new ArrayList<Shangping>();
		init(array);
		while(true){
			System.out.println("");
			System.out.println("1.查看商品   2.添加商品   3.刪除商品   4.修改商品   5.退出");
			int a = getNumber();
			switch (a) {
			case 1:   //1.查看商品
				chaKan();
				break;
				
			case 2:    // 2.添加商品
				tianJia(array);
				break;
			
			case 3:
				deleteShangping(array); //3.刪除商品
				break;
				
			case 4:
				xiuGai(array);    //4.修改商品
				break;
				
			case 5:   //5.退出
				break;
				
			default: 
				System.out.println("您的輸入有誤");
				break;
			}
		}
		
	}
	
	/*
	 * 定義修改商品方法
	 * 參數:array
	 * 返回值:無
	 */
	public static void xiuGai(ArrayList<Shangping> array){
		System.out.println("您選擇的是修改功能");
		System.out.println("請輸入您要修改商品的編號:");
		Scanner s = new Scanner(System.in);
		int ID = s.nextInt();
		for (int i = 0; i < array.size(); i++) {
			Shangping a = array.get(i);
			if(ID == a.ID){
				System.out.println("請輸入新的編號");
				a.ID = s.nextInt();
				
				System.out.println("請輸入您要修改商品的名字:");
				String name = s.next();
				a.name = name;
				
				System.out.println("請輸入您要修改商品的價格:");
				double price = s.nextDouble();
				a.price = price;
				System.out.println("修改成功");
				return ;
			}
		}
		System.out.println("您的輸入有誤");
	}
	
	/*
	 * 定義刪除商品方法
	 */
	public static void deleteShangping(ArrayList<Shangping> array){
		Shangping sh = new Shangping();
		System.out.println("您選擇的刪除功能");
		System.out.println("請輸入您要刪除的商品編號:");
		Scanner s = new Scanner(System.in);
		int ID = s.nextInt();
		for (int i = 0; i < array.size(); i++) {
			Shangping a = array.get(i);
			if(ID == a.ID){
				array.remove(i);
				System.out.println("刪除成功");
				return ;
			}
		}
		System.out.println("您的輸入有誤");
	}
	
	/*
	 * 定義添加商品方法
	 * 參數:array集合
	 * 返回值:無
	 */
	public static void tianJia(ArrayList<Shangping> array){
		System.out.println("您選擇的是添加商品功能");
		System.out.println("請輸入商品名稱:");
		Shangping sh = new Shangping();
		Scanner s = new Scanner(System.in);
		sh.name = s.next();
		
		System.out.println("請輸入商品編號:");
		sh.ID = s.nextInt();
		
		System.out.println("請輸入商品價格:");
		sh.price = s.nextDouble();
		
		array.add(sh);
		
	}
	
	/*
	 * 實現輸入調用功能
	 */
	public static int getNumber(){
	System.out.println("請輸入您要操作的功能:");
		Scanner s = new Scanner(System.in);
		int number = s.nextInt();
		return number;
	}
	
	/*
	 * 查看商品清單功能
	 */
	public static void chaKan(){
		System.out.println("===========商品清單===========");
		System.out.println("商品名               商品編號        商品價格");
		System.out.println("新疆哈密瓜        001     28");
		System.out.println("新疆巴旦木        004     46.9");
		System.out.println("天山雪梨            002     6.8");
		System.out.println("新疆切糕            003     1005.8");
	}
	/*
	 * 定義商品初始化方法
	 * 商品存儲在集合中
	 * 參數:array集合
	 * 返回值:無
	 */
    public static void init(ArrayList<Shangping> array){
    	Shangping s1 = new Shangping();
    	s1.name = "新疆哈密瓜";
    	s1.ID = 001;
    	s1.price = 28;
    	
    	Shangping s2 = new Shangping();
    	s2.name = "新疆巴旦木";
    	s2.ID = 004;
    	s2.price = 46.9;
    	
    	Shangping s3 = new Shangping();
    	s3.name = "天山雪梨";
    	s3.ID = 002;
    	s3.price = 6.8;
    	
    	Shangping s4 = new Shangping();
    	s4.name = "新疆切糕";
    	s4.ID = 003;
    	s4.price = 1005.8;
    	
    	array.add(s1);
    	array.add(s2);
    	array.add(s3);
    	array.add(s4);
    	
    }
}

老師的:

/*
 *   自定義類, 描述商品信息
 *   商品的屬性
 *     編號, int
 *     品名, String
 *     價格, double
 *     數量, int
 *     總價, double
 *   
 *   商品類型,存儲到集合ArrayList中
 */
public class FruitItem {
	// 商品號
	int ID;
	// 商品名字
	String name;
	// 單價
	double price;
	// 數量
	int number;
	// 總金額
	double money;
}

import java.util.ArrayList;
import java.util.Scanner;

/*
 *   超市管理系統主
 *   實現:
 *     1. 商品數據的初始化
 *     2. 用戶的菜單選擇
 *     3. 根據選擇執行不同的功能
 *       3.1 Read    查看商品
 *       3.2 Create  添加商品
 *       3.3 Delete  刪除商品
 *       3.4 Update  修改商品
 *       
 *       
 *   所有功能 ,必須定義方法實現
 *   主方法main  調用作用
 */
public class Shopp {

	public static void main(String[] args) {
		//創建ArrayList集合,存儲商品類型,存儲數據類型FruitItem類型
		ArrayList<FruitItem> array = new ArrayList<FruitItem>();
		//調用商品初始化方法,傳遞集合
		init(array);
		while(true){
			//調用菜單方法
			mainMenu();
			//調用用戶選擇序號方法
			int choose = chooseFunction();
			switch (choose) {
			case 1:
				//調用1: 貨物 清單
				showFruitList(array);
			break;
			
			case 2:
				//2: 添加貨物
				addFruit(array);
			break;
			
			case 3:
				//3: 刪除貨物
				deleteFruit(array);
			break;
			
			case 4:
				//4: 修改貨物
				updateFruit(array);
			break;
			
			case 5:
				return ;

			default:
				System.out.println("輸入的序號沒有");
				break;
			}
		}
	}
	/*
	 *  定義方法,實現商品的修改功能
	 *  返回值: 無
	 *  參數: 集合
	 *  
	 *  提示用戶選擇的是修改功能
	 *  提示用戶輸入需要修改的商品編號
	 *  遍歷集合,獲取每個FruitItem變量
	 *  變量調用ID屬性,屬性和用戶輸入的編號比較
	 *  如果相同:
	 *    修改調FruitItem中的屬性值
	 *    鍵盤輸入
	 */
	public static void updateFruit(ArrayList<FruitItem> array){
		System.out.println("選擇的是修改功能");
		System.out.println("請輸入商品的編號");
		
		Scanner sc = new Scanner(System.in);
		int ID = sc.nextInt();
		//遍歷集合,獲取每個FruitItem變量
		for(int i = 0 ; i < array.size(); i++){
			FruitItem item = array.get(i);
			//獲取FruitItem的屬性ID,和用戶輸入的ID比較
			if(item.ID == ID){
				System.out.println("輸入新的商品編號");
				item.ID = sc.nextInt();
				
				System.out.println("輸入新的商品名字");
				item.name = sc.next();
				
				System.out.println("輸入新的商品價格");
				item.price = sc.nextDouble();
				System.out.println("商品修改成功");
				return ;
			}
		}
		System.out.println("輸入的編號不存在");
	}
	
	/*
	 *  定義方法,實現商品的刪除功能
	 *  返回值: 無
	 *  參數: 集合
	 *  
	 *  刪除依靠的是商品的編號
	 *  提示用戶選擇的是刪除功能
	 *  鍵盤輸入商品的編號
	 *  遍歷集合,獲取集合中的每個FruitItem變量
	 *  變量調用屬性 ID, 和用戶的輸入的編號,對比,相同就刪除
	 */
	public static void deleteFruit(ArrayList<FruitItem> array){
		System.out.println("選擇的是刪除功能");
		System.out.println("請輸入商品的編號");
		Scanner sc = new Scanner(System.in);
		
		int ID = sc.nextInt();
		//遍歷集合
		for(int i = 0 ; i < array.size(); i++){
			//獲取到每個FruitItem變量
			FruitItem item = array.get(i);
			//變量,調用屬性ID,和用戶輸入的編號比較
			if( item.ID == ID){
				//移除集合中的元素
				//集合的方法remove實現
				array.remove(i);
				System.out.println("刪除成功");
				return;
			}
		}
		System.out.println("你輸入的編號不存在");
	}
	
	/*
	 * 定義方法,實現商品的添加功能
	 * 返回值:無
	 * 參數: 集合
	 * 提示用戶選擇的是添加商品的功能
	 * 
	 * 提示用戶輸入的是什麼
	 * 
	 * 創建FruitItem變量,變量調用的屬性
	 * 將輸入的每個商品屬性進行賦值
	 */
	public static void addFruit(ArrayList<FruitItem> array){
		System.out.println("選擇的是添加商品功能");
		//創建Scanner變量
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入商品的編號");
		//輸入商品的編號
		int ID = sc.nextInt();
		//輸入商品的名字
		System.out.println("請輸入商品的名字");
		String name = sc.next();
		//輸入商品的單價
		System.out.println("輸入商品的單價");
		double price = sc.nextDouble();
		//創建FruitItem變量
		FruitItem item = new FruitItem();
		//item.屬性賦值
		item.ID = ID;
		item.name = name;
		item.price = price;
		array.add(item);
		System.out.println("商品添加成功");
	}
	
	/*
	 *  定義方法,實現顯示貨物清單功能
	 *  返回值: 無
	 *  參數: 集合
	 *  遍歷集合,獲取集合中的每個FruitItem變量,變量,調用屬性
	 */
	public static void showFruitList(ArrayList<FruitItem> array){
		System.out.println();
		System.out.println("================商品庫存清單================");
		System.out.println("商品編號         商品名稱                商品單價");
		//遍歷集合
		for(int i = 0 ; i < array.size(); i++){
			//集合get方法,獲取出每個FruitItem變量,可以使用FruitItem接受get結果
			FruitItem item = array.get(i);
			//變量item調用類中屬性
			System.out.println(item.ID+"   "+item.name+"        "+item.price);
		}
	}
	
	/*
	 *  定義方法,實現接受用戶的鍵盤輸入
	 *  返回編號
	 */
	public static int chooseFunction(){
		Scanner sc = new Scanner(System.in);
		return sc.nextInt();
	}
	
	/*
	 * 定義方法,實現主菜單
	 * 提示用戶哪些選擇 讓選擇序號
	 * 返回值: 無
	 * 參數: 無
	 */
	public static void mainMenu(){
		System.out.println();
        System.out.println("============歡迎光臨ItCast超市============");
        System.out.println("1: 貨物 清單   2: 添加貨物   3: 刪除貨物   4: 修改貨物  5: 退出");
        System.out.println("請您輸入要操作的功能序號");
	}
	
	/*
	 * 定義方法,實現商品數據的初始化
	 * 先將一部分數據,存儲集合中
	 * 返回值: 無
	 * 參數 : 集合
	 * 方法名: init
	 */
	public static void init(ArrayList<FruitItem> array){
		//創建出多個FruitItem類型,並且屬性賦值
		FruitItem f1 = new FruitItem();
		f1.ID = 9527;
		f1.name = "少林寺酥餅核桃";
		f1.price = 12.7;
		
		FruitItem f2 = new FruitItem();
		f2.ID = 9008;
		f2.name = "尚康雜糧牡丹餅";
		f2.price = 5.6;
		
		FruitItem f3 = new FruitItem();
		f3.ID = 9879;
		f3.name = "新疆原產哈密瓜";
		f3.price = 599.6;
		
		//創建的3個FruitItem類型變量,存儲到集合中
		array.add(f1);
		array.add(f2);
		array.add(f3);
	}
	
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章