Java電器商場小系統--簡單的java邏輯,java電器--邏輯

來源:互聯網
上載者:User

Java電器商場小系統--簡單的java邏輯,java電器--邏輯

//商場類
public class Goods {
int no;    //編號    String name;  //商品名稱    double price;  //商品價格    int number;  //商品數量
  //初始化資料方法 public void setData(int iNo, String sName, double dPrice, int iNumber) { no = iNo; name = sName; price = dPrice; number = iNumber; } // 按格式輸出 public void showGoods() { System.out.println(no + "\t\t" + name + (name.getBytes().length > 10 ? "\t" : "\t\t") + price + "\t\t" + number); }}
//使用者類
public class User { String username;  //使用者名稱 String password;  //密碼 String nickName;  //暱稱// 購物車 Goods[] cart = new Goods[10];
  //顯示使用者資訊方法 public void showInfo() { System.out.println("使用者名稱:\t" + username); System.out.println("密碼:\t" + password); System.out.println("暱稱:\t" + nickName); }
  //購買商品 --> 傳入商品參數 public void buyGoods(Goods goods) { // 1.購物車中沒有該商品 // 2.購物車中有了該商品 int index = searchGoodsByName(goods.no); if (index != -1) { // 購物車已經有了該商品,直接增加數量 cart[index].number += goods.number; } else { // 沒有呢,就把他放入 第一個為null的位置 for (int i = 0; i < cart.length; i++) { if (cart[i] == null) { cart[i] = goods; return; } } System.out.println("商品已滿,請清空購物車後,在來購買"); } }
  //尋找購物車是否有相同的商品 public int searchGoodsByName(int goodsNo) { for (int i = 0; i < cart.length; i++) { if (cart[i] == null) return -1; else if (goodsNo == cart[i].no) { return i; } } return -1; }
  //顯示商品 public void showGoods() { System.out.println("商品編號\t\t商品名稱\t\t商品價格\t\t庫存"); for (int i = 0; i < cart.length; i++) { if (cart[i] == null) return; cart[i].showGoods();   //調用商場的顯示方法 顯示商品 } } // 返回購物車中需要的金額 public double getGoodsMoney() { double sum = 0; for (int i = 0; i < cart.length; i++) { if (cart[i] == null) break; double value = cart[i].price * cart[i].number; sum += value; } return sum; } // 清空購物車 public void clearCart() { for (int i = 0; i < cart.length; i++) { cart[i] = null; } }}
import java.util.Scanner;//商場系統public class ShoppingManager {    //使用者數組    User[] users = new User[10];    //商品數組    Goods[] goods = new Goods[10];    Scanner input = new Scanner(System.in);    //當前登入使用者    User loginUser;    // 顯示menu之前,先初始化資料    public void init() {        goods[0] = new Goods();        goods[0].setData(10101, "海爾冰箱", 3999.9, 50);        goods[1] = new Goods();        goods[1].setData(10102, "格力冰箱", 2888.8, 50);        goods[2] = new Goods();        goods[2].setData(10103, "TCL冰箱", 3333.0, 100);        goods[3] = new Goods();        goods[3].setData(10104, "美的冰箱", 5100.5, 80);        goods[4] = new Goods();        goods[4].setData(10105, "海爾空調", 4622, 50);        goods[5] = new Goods();        goods[5].setData(10106, "格力空調", 1555, 70);        goods[6] = new Goods();        goods[6].setData(10107, "TCL空調", 2100.5, 30);        goods[7] = new Goods();        goods[7].setData(10108, "美的空調", 3355.5, 150);        goods[8] = new Goods();        goods[8].setData(10109, "小天鵝洗衣機", 2500, 30);        goods[9] = new Goods();        goods[9].setData(10110, "西門子冰箱", 3800, 80);        // 預存使用者 測試用的        User user = new User();        user.nickName = "李四";        user.username = "lisi";        user.password = "123456";        users[0] = user;    }    //顯示商場菜單    public void showMenu() {        // 初始化商品        init();        String choose = "";        do {            // 列印菜單系統            System.out.println("*****************國美電器*****************");            System.out.println("\t\t1.使用者登入");            System.out.println("\t\t2.使用者註冊");            System.out.println("\t\t3.退出系統");            System.out.println("*****************國美電器*****************");            choose = input.next();            switch (choose) {            case "1":                userLogin(); //登入                break;            case "2":                userRegister();    //註冊                break;            case "3":                System.out.println("退出系統成功!");                break;            default:                System.out.println("輸入錯誤,請重新輸入");                break;            }        } while (!choose.equals("3"));        // 退出系統        input.close();    }    //使用者註冊    private void userRegister() {        System.out.print("請輸入使用者名稱:");        String name = input.next();        if (isDuplicationName(name)) {             System.out.println("使用者名稱已存在!註冊失敗");            return;        }        // 沒有重名        System.out.print("請輸入密碼:");        String password = input.next();        if (password.length() < 6) {            System.out.println("密碼輸入錯誤!註冊失敗");            return;        }        System.out.print("請輸入暱稱:");        String nickName = input.next();        if (addUser(name, password, nickName)) {            System.out.println("註冊成功");        } else {            System.out.println("註冊失敗,使用者已滿!");        }    }    //添加使用者方法,判斷使用者數組中是否存在該使用者    public boolean addUser(String username, String password, String nickname) {        User user = new User();        user.username = username;        user.nickName = nickname;        user.password = password;        for (int i = 0; i < users.length; i++) {            if (users[i] == null) {                users[i] = user;                return true;            }        }        return false;    }    //判斷使用者名稱是否存在,返回ture或false    private boolean isDuplicationName(String name) {        for (int i = 0; i < users.length; i++) {            // 為空白就跳出迴圈            if (users[i] == null)                break;            // 有重名            else if (users[i].username.equals(name))                return true;        }        return false;    }    private void userLogin() {        System.out.print("請輸入使用者名稱:");        String username = input.next();        System.out.print("請輸入密碼:");        String password = input.next();        User user = login(username, password);        if (user == null) {            System.out.println("登入失敗!");        } else {            // 下一級菜單            loginUser = user;            System.out.println("登入成功,你好:" + loginUser.nickName);            userMenu();    //使用者菜單        }    }    public User login(String username, String password) {        for (int i = 0; i < users.length; i++) {            if (users[i] == null)                return null;            else if (users[i].username.equals(username)                    && users[i].password.equals(password)) {                return users[i];            }        }        return null;    }    // 使用者登入後的菜單    public void userMenu() {        String choose = "";        do {            System.out.println("1.商品購買");            System.out.println("2.我的購物車");            System.out.println("3.個人資訊");            System.out.println("4.登出");            choose = input.next();            switch (choose) {            case "1":                buyGoods();    //購買商品                break;            case "2":                showMyGoods();    //顯示購物車                break;            case "3":                showUserInfo();    //顯示使用者資訊                break;            case "4":                System.out.println("登出成功");                break;            default:                System.out.println("輸入錯誤,請重新輸入!");                break;            }        } while (!choose.equals("4"));    }    private void showUserInfo() {        loginUser.showInfo();    }    //顯示購物車    private void showMyGoods() {        System.out.println("********************購物車********************");        loginUser.showGoods();        // 開始付款        System.out.println("\t\t你購買的商品總價格為:" + loginUser.getGoodsMoney());        // 是否付款        if (loginUser.getGoodsMoney() == 0)            return;        System.out.println("是否付款?Y/N");        String choose = input.next();        if (choose.equalsIgnoreCase("y")) {            // 付款            System.out.println("輸入實付金額:");            double money = input.nextDouble();            if (money < loginUser.getGoodsMoney()) {                System.out.println("輸入的金額不夠啊!");            } else {                double zero = money - loginUser.getGoodsMoney();                System.out.println("恭喜你付款成功,找您" + zero + "元");                loginUser.clearCart();            }        }    }    private void buyGoods() {        System.out.println("********************商品購買*********************");        showGoodsList();        String choose = "";        while (!choose.equalsIgnoreCase("exit")) {            System.out.print("請輸入需要購買的商品編號,輸入exit後退出");            choose = input.next();            if (!choose.equalsIgnoreCase("exit")) {                // 購買商品                int index = getGoodsIndex(choose);                if (index == -1) {                    System.out.println("沒有該商品,請重新輸入");                } else {                    System.out.print("請輸入商品數量:");                    int number = input.nextInt();                    // 判斷由沒有這麼多庫存                    if (number <= goods[index].number) {                        // 1.商城需要減少數量                        // 2.購物車是增加                        goods[index].number -= number;                        // 使用者買的商品                        Goods good = new Goods();                        good.setData(goods[index].no, goods[index].name,                                goods[index].price, number);                        loginUser.buyGoods(good);                        System.out.println("購買成功");                    } else {                        System.out.println("商品不夠了。。。");                    }                }            }        }        // 顯示購物車        showMyGoods();    }    //顯示商品列表    public void showGoodsList() {        System.out.println("商品編號\t\t商品名稱\t\t商品價格\t\t庫存");        for (int i = 0; i < goods.length; i++) {            Goods good = goods[i];            if (good == null)                return;            good.showGoods();        }    }    // 判斷購買的商品在數組中的位置    /**     *      * @param no     *            輸入的商品編號     * @return 找到 返回索引號,沒有找到返回-1     */    public int getGoodsIndex(String no) {        // 將編號轉成int        // int iNo = Integer.parseInt(sNo);        for (int i = 0; i < goods.length; i++) {            if ((goods[i].no + "").equals(no)) {                return i;            }        }        return -1;    }}
//測試類別public class Test {    public static void main(String[] args) {        ShoppingManager manager = new ShoppingManager();        manager.showMenu();    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.