簡單的java水果商店后台


創建Fruit.java文件:

package com.fruit;
//定義水果屬性類
public class Fruit {
        int ID;
        String name;
        String price;
        int num;
        double money;
}

創建Fruitshop.java文件:

package com.fruit;

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

public class Fruitshop {

    public static void main(String[] args) {
        //創建集合
        ArrayList<Fruit> array = new ArrayList<Fruit>();
        //調用方法,在集合里添加數據
        add(array);
        //進入循環
        while(true){
            //調用主菜單
            mainMenu();
            //獲取輸入數字
            int choose = chooseFunction();
            switch(choose){
            //調用展示商品信息方法
            case 1:showFruit(array);
            break;
            //調用添加商品信息方法
            case 2:addFruit(array);
            break;
            //調用刪除商品信息方法
            case 3:delFruit(array);
            break;
            //調用修改商品信息方法
            case 4:updateFruit(array);
            break;
            //退出循環
            case 5:
            return;
            //輸入超出范圍數字顯示“按鍵無效”
            default:
                System.out.println("按鍵無效");
                break;
            }
        }

    }
    //主菜單
    public static void mainMenu() {
        System.out.println();
        System.out.println("=====================歡迎光臨xx水果超市=========================");
        System.out.println("1 查看清單  2:添加水果  3:刪除水果  4:修改水果  5:退出");
        System.out.println("請選擇功能序號");
        
    }
    //商品信息
    public static void showFruit(ArrayList<Fruit> array) {
        System.out.println();
        System.out.println("========歡迎光臨xx水果超市============");
        System.out.println("編號                     水果名                   水果單價");
        for(int i = 0;i<array.size();i++){
            Fruit goods = array.get(i);
            System.out.println(goods.ID+"   "+goods.name+"  "+goods.price+"    ");
        }
    }
    //添加商品
    public static void addFruit(ArrayList<Fruit> array) {
        System.out.println("您選擇的是添加功能");
        System.out.println("請輸入水果編號");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        System.out.println("請輸入水果名");
        String text = sc.next();
        System.out.println("請輸入價格");
        String price = sc.next();
        Fruit goods = new Fruit();
        goods.ID = num;
        goods.name= text;
        goods.price = price;
        array.add(goods);
        System.out.println("添加成功");
    }
    //刪除商品
    public static void delFruit(ArrayList<Fruit> 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++){
            Fruit goods = array.get(i);
            if (goods.ID==ID){
                array.remove(i);
                System.out.println("刪除成功");
                return;
            }
        }
        System.out.println("您輸入的水果編號不存在");
    }
    //修改商品
    public static void updateFruit(ArrayList<Fruit> 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++){
            Fruit goods = array.get(i);
            if(goods.ID==ID){
                System.out.println("請輸入新的水果編號");
                goods.ID= sc.nextInt();
                System.out.println("請輸入新的水果名字");
                String name =sc.next();
                goods.name = name;
                System.out.println("請輸入新的價格");
                goods.price= sc.next();
                return;
            }
       }
       System.out.println("編號不存在");
}
    //返回循環
    public static int chooseFunction(){
        Scanner sc = new Scanner(System.in);
         return sc.nextInt();
    }
    //添加初始商品信息
    public static void add(ArrayList<Fruit> array) {
            Fruit s = new Fruit();
            s.ID = 01;
            s.name = "蘋果";
            s.price = "4.8元/千克";
            array.add(s);
            
            Fruit s1 = new Fruit();
            s1.ID = 02;
            s1.name = "紅提";
            s1.price = "4.9元/千克";
            array.add(s1);
            
            Fruit s2 = new Fruit();
            s2.ID = 03;
            s2.name = "金錢橘";
            s2.price = "4.1元/千克";
            array.add(s2);
            
            Fruit s3 = new Fruit();
            s3.ID = 04;
            s3.name = "皇冠梨";
            s3.price = "5.5元/千克";
            array.add(s3);
            
            Fruit s4 = new Fruit();
            s4.ID = 05;
            s4.name = "香蕉";
            s4.price = "4.3元/千克";
            array.add(s4);
            
            Fruit s5 = new Fruit();
            s5.ID = 06;
            s5.name = "火龍果";
            s5.price = "6.8元/千克";
            array.add(s5);
                    
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM