0
点赞
收藏
分享

微信扫一扫

Java三个小项目

eelq 2022-01-23 阅读 49

文章目录

第一个小项目:产生随机验证码

功能:

用户输入位数产生对应位数的随机码(数字大小写字母随机)

代码实现:

package com.hkd.project;

import java.util.Random;
import java.util.Scanner;

public class YanZhengMa {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您想要生成几位随机验证码");
        int i = sc.nextInt();
        System.out.println(createCode(i));
    }

    //定义一个方法返回一个随机验证码,
    public static String createCode(int n){
        Random r = new Random();
        String code = "";
        for (int i = 0; i < n; i++) {
            int type = r.nextInt(3);
            switch (type){
                case 0:
                    //大写字母(65-90)           (0-25)+65=65-90
                    char m = (char) (r.nextInt(26)+65);
                    code += m;
                    break;
                case 1:
                    //大写字母(97-97+25)           (0-25)+65=65-90
                    char m1 = (char) (r.nextInt(26)+97);
                    code += m1;
                    break;
                case 2:
                    //大写字母(65-90)           (0-25)+65=65-90
                    code += r.nextInt(10);
                    break;
            }
        }
        return code;
    }
}

第二个小项目:计算飞机票

功能:

根据月份判断淡/旺季,经济舱与头等舱不同的价位来计算飞机票价格

代码实现:

package com.hkd.project;


import java.util.Scanner;

public class PlantTickets {
    //    机票的价格按照淡季/旺季以及头等舱/经济舱决定
    //    旺季(5-10月)头等舱9折,经济舱8.5折
    //    淡季(11月到来年4月)头等舱7折,经济舱6.5折
    public static void main(String[] args) {
        order();
    }


//  定义一个方法
//  对输入的月份及舱位进行计算票价
    public static int price(int price,int month,String rank){
        if (month>=5 && month<=10){
            if (rank.equals("头等舱")){
                price *=0.9;
            }else if(rank.equals("经济舱"))
                price *=0.85;
        }else {
            if (rank.equals("头等舱")){
                price *=0.7;
            }else {
                price *=0.65;
            }
        }
        return price;
    }

//定义另一个方法菜单
    public static void order(){
        int price1 = 1000;
        int price2 = 600;
        int n = 0;
        int month;
        String rank;
        boolean flag1 = false;
        boolean flag2 = false;
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎来到小轩航空!");
        //输入月份
        do {
            System.out.println("请输入您想查询航班的月份:");
            month = sc.nextInt();
            if (month >=0 && month <=12 ){
                flag1 = true;
            }else{
                System.out.println("您输入的月份不对,请输入1~12月份");
            }
        }while(flag1 == false);
        //输入舱
        do {
            System.out.println("请输入您想要选择的舱位(头等舱/经济舱):");
            rank = sc.next();
            if (rank.equals("头等舱")){
                flag2 = true;
                n = 1;
            }else if (rank.equals("经济舱")){
                flag2 = true;
                n = 2;
            }else{
                System.out.println("您输入的不对,请重新输入");
            }

        }while(flag2 == false);

        if (n == 1){
            System.out.println("您需要支付:"+price(price1, month, rank)+"元");
        }else if (n == 2){
            System.out.println("您需要支付:"+price(price2, month, rank)+"元");
        }

    }


}

第三个小项目:购物车

功能:

用户可以通过输入相应指令进行对应操作包括:

  1. 添加商品(编号,名字,价格,数量)
  2. 查看当前购物车中所有商品
  3. 通过编号修改购物车中对应商品的数量
  4. 购物车总计

代码实现

Goods类中:

package com.hkd.shopcar;

public class Goods {
    int id;
    String name;
    int number;
    double price;
}

实现类中:

package com.hkd.shopcar;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Goods[] g = new Goods[100];
        while (true) {
            System.out.println("请选择您想要进行的操作");
            System.out.println("添加商品进购物车:add");
            System.out.println("查看购物车中商品:query");
            System.out.println("更改商品的数量:update");
            System.out.println("结算:pay");
            System.out.println("请输入操作:");
            Scanner sc = new Scanner(System.in);
            String commond = sc.next();
            switch (commond){
                case "add":
                    //进行添加商品的操作
                    add(g,sc);
                    break;
                case "query":
                    //进行展示当前购物车操作
                    query(g);
                    break;
                case "update":
                    //进行更改商品数量的操作
                    update(g,sc);
                    break;
                case "pay":
                    //进行结算操作
                    pay(g);
                    break;
                default:
                    System.out.print("您输入的操作不对");
            }
        }

    }

    private static void pay(Goods[] g) {
        query(g);
        double money = 0.0;
        for (int i = 0; i < g.length; i++) {
            Goods good = g[i];
            if (good != null){
                money += good.price*good.number;
            }else{
                break;
            }
        }
        System.out.println("本次商品购买的总价为:" + money);
    }

    private static void update(Goods[] g, Scanner sc) {
        while (true) {
            System.out.println("请输入您想要修改商品的编号:");
            int id = sc.nextInt();
            Goods good = findgoodbyid(id, g);
            if (good != null){
                System.out.println("请输入您想要的数量:");
                int num = sc.nextInt();
                good.number = num;
                System.out.println("编号为"+good.id+"的商品数量成功修改了");
                break;
            }else{
                System.out.println("您输入的商品编号不存在");
            }
        }

    }

    private static Goods findgoodbyid(int id, Goods[] g) {
        for (int i = 0; i < g.length; i++) {
            Goods good = g[i];
            if (good != null && good.id == id){
                return good;
            }
        }
        return null;
    }

    private static void query(Goods[] g) {
        System.out.println("商品编号\t商品名字\t商品价格\t商品数量");
        for (int i = 0; i < g.length; i++) {
            Goods good = g[i];
            if (good != null){
                System.out.println(good.id+"\t\t"+good.name+"\t\t"+good.price+"\t\t"+good.number);
            }else{
                return;
            }
        }
    }

    private static void add(Goods[] g, Scanner sc) {
        System.out.println("请输入您想要添加商品的编号(不能重复):");
        int id = sc.nextInt();
        System.out.println("请输入商品的名字:");
        String name = sc.next();
        System.out.println("请输入商品的价格:");
        double price = sc.nextDouble();
        System.out.println("请输入想购买商品的数量:");
        int num = sc.nextInt();

        Goods good = new Goods();
        good.id = id;
        good.price = price;
        good.number = num;
        good.name = name;

        for (int i = 0; i < g.length; i++) {
            if (g[i] == null){
                g[i] = good;
                break;
            }
        }
        System.out.println("您的商品"+good.name+"添加成功");
    }
}

举报

相关推荐

0 条评论