本周代码练习

文章展示了两个Java程序,一个是宾馆管理系统的实现,包括查看房间、订房和退房功能;另一个是商品录入系统,具备商品列表、录入、查询和统计信息功能。此外,还讨论了面向对象编程中的分数封装和图形打印,涉及形状类及其子类的绘制方法。
摘要由CSDN通过智能技术生成
  1. 宾馆管理系统

1.主程序(调用功能)

import java.util.Scanner;
public class Project {
    public static void main(String[] args) {
        //创建宾馆对象
        Hotel hotel = new Hotel();
        //主界面
        System.out.println("欢迎使用宾馆管理系统,请认真阅读以下使用说明");
        System.out.println("[1]表示查看房间列表  [2]表示订房  [3]表示退房  [0]表示退出系统");
        Scanner s = new Scanner(System.in);
        while(true) {
            System.out.print("请输入编号:");
            int i = s.nextInt();
            if (i == 1) {
                hotel.print();
            } else if (i == 2) {
                System.out.print("请输入房间编号:");
                int roomNo = s.nextInt();//前台输入编号1
                hotel.order(roomNo);
            } else if (i == 3) {
                System.out.print("请输入房间编号:");
                int roomNo = s.nextInt();//前台输入编号
                hotel.exit(roomNo);
            } else if (i == 0) {
                System.out.println("再见,谢谢使用");
                return;
            } else {
                System.out.println("输入错误,请重新输入");
            }
        }
    }
}

2.宾馆(实现功能)

public class Hotel {
    private Room[][] rooms;
    //构造方法给宾馆分配房间,创建Room对象
    public Hotel() {
        rooms = new Room[3][10];
        for (int i = 0; i < rooms.length; i++) {
            for (int j = 0; j < rooms[i].length; j++) {
                if (i == 0)
                    rooms[i][j] = new Room((i + 1) * 100 + j + 1, "单人间", true);
                else if (i == 1)
                    rooms[i][j] = new Room((i + 1) * 100 + j + 1, "标准间", true);
                else
                    rooms[i][j] = new Room((i + 1) * 100 + j + 1, "总统套房", true);
            }
        }
    }
    //打印功能
    public void print() {
        for (int i = 0; i < rooms.length; i++) {
            for (int j = 0; j < rooms[i].length; j++) {
                Room room = rooms[i][j];
                System.out.print(room);
            }
            System.out.println();
        }
    }
    //订房功能
    public void order(int roomNo) {
        Room room = rooms[roomNo / 100 - 1][roomNo % 100 - 1];
        room.setStatus(false);
        System.out.println(roomNo + "已订房");
    }
    //退房功能
    public void exit(int roomNo) {
        Room room = rooms[roomNo / 100 - 1][roomNo % 100 - 1];
        room.setStatus(true);
        System.out.println(roomNo + "已退房");
    }
}

3.房间(具体实现)

public class Room {
    private int no;
    private String type;
    private boolean status;
    //构造方法
    public Room(int no,String type,boolean status){
        this.no=no;
        this.type=type;
        this.status=status;
    }
    //set和get方法
    public int getNO(){
        return no;
    }
    public void setNO(int no){
        this.no=no;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public boolean getStatus() {
        return status;
    }
    public void setStatus(boolean status) {
        this.status = status;
    }
    //重写toString方法
    public String toString() {
        return "[" + no + "," + type + "," + (status ? "空闲" : "占用") + "]";
    }
}
  1. 商品录入系统

/*通过java基础的相关知识,设计并完成一个简单的商品录入系统,
可以实现:菜单显示、商品信息展示、
商品信息录入、商品信息查询、退出的功能
*/

import java.util.Scanner;
public class Project{
        static String[] names={"iPhoneXS","华为 Mate 20 pro","小米X","vivo NEX","oppo Find"};
        static double[] prices={8999,5399,2399,4399,3999};
        static int[] numbers={50,20,80,120,90};
    public static void main(String[] args) {
        //死循环显示菜单
        outer:
        while(true){
            int c=menu();
            switch(c){
                case 1:f1();break ;
                case 2:f2();break ;
                case 3:f3();break ;
                case 4:f4();break ;
                case 5:break outer;
            }
        }
    }
    //主界面
    private static int menu(){
        System.out.println("--------------------------------");
        System.out.println("1.商品列表");
        System.out.println("2.商品录入");
        System.out.println("3.商品查询");
        System.out.println("4.统计信息");
        System.out.println("5.退出");
        System.out.println("--------------------------------");
        System.out.print("选择: >");
        return new Scanner(System.in).nextInt();
    }
    private static void f1(){
        for(int i=0;i<names.length;i++){
            String  name=names[i];
            double price=prices[i];
            int number=numbers[i];
            System.out.println((i+1)+".名称:"+name+",价格:"+price+",数量:"+number);// 1. 名称:xx,价格:xx,数量:xx
        }
    }
    private static void f2(){
        //遍历数组
        for(int i=0;i< names.length;i++){
            System.out.println("录入第"+(i+1)+"件商品:");
            System.out.println("名称:");
            String name=new Scanner(System.in).nextLine();
            System.out.println("价格:");
            double price=new Scanner(System.in).nextDouble();
            System.out.println("数量:");
            int number=new Scanner(System.in).nextInt();
            names[i]=name;
            prices[i]=price;
            numbers[i]=number;
        }
        f1();
    }
    private static void f3(){
            System.out.println("输入查询的商品名:");
            String n=new Scanner(System.in).nextLine();
        for(int i=0;i< names.length;i++) {
            //字符串,比较是否相等,要用equals()方法
                if(n.equals(names[i])){
                    String name=names[i];
                    double price=prices[i];
                    int number=numbers[i];
                    System.out.println((i+1)+".名称:"+name+",价格:"+price+",数量:"+number);
                    return;
                }
        }
        System.out.println("找不到商品");
    }
    private static void f4(){
        double spzj = 0;//商品总价
        double djzj = 0;//单价总价
        double zgdj = 0;//最高单价
        double zgzj = 0;//最高总价
        //遍历数组
        for (int i = 0; i < names.length; i++) {
            spzj += prices[i] * numbers[i];
            djzj += prices[i];
            if(prices[i] > zgdj) {
                zgdj = prices[i];
            }
            if(prices[i]*numbers[i] > zgzj) {
                zgzj = prices[i]*numbers[i];
            }
        }
        System.out.println("商品总价:"+spzj);
        System.out.println("单价均价:"+(djzj/names.length));
        System.out.println("最高单价:"+zgdj);
        System.out.println("最高总价:"+zgzj);
    }
}
  1. 面向对象 封装百分制分数

需求:封装百分制分数,和它对应的五档分制分数

分数类

public class Score {
    int score;
    char level;
    public Score(int score){
         this.score=score;
         this.level=setLevel(score);
    }
    private char setLevel(int s){
        char r=0;
        switch(s/10){
            case 10: case 9:
                r='A'; break;
            case 8: case 7:
                r='B'; break;
            case 6:
                r='C'; break;
            case 5: case 4: case 3: case 2:
                r='D'; break;
            case 1:
                r='E'; break;
        }
        return r;
    }
    public String toString(){
        return "分数:"+score+",等级:"+level;
    }
}

测试类

public class Test
{
    public static void main(String[] args) {
        Score s=new Score(100);
        System.out.println(s);//默认调用toString方法;
    }
}

2.面向对象 打印图形

需求:设计一个可以随机打印形状的代码

形状类(父类)

public class Shape {
    public void draw(){
        System.out.println("图形形状");
    }
    public void clear(){
        System.out.println();
    }
}

圆形类、方形类,直线类(子类)

class Circle extends Shape{
    @Override//方法重写(覆盖)
    public void draw() {
        System.out.println("打印一个圆形 O");
    }
}
class Square extends Shape{
    @Override
    public void draw() {
        System.out.println("打印一个方形 口");
    }
}
class Line extends Shape{
    @Override
    public void draw() {
        System.out.println("打印一条直线 ————————");
    }
    public void length(){
        System.out.println("一米");
    }
}

测试类

import java.util.Random;
import java.util.Scanner;
public class Test
{
    public static void main(String[] args) {
        while (true){
            System.out.println("按回车继续");
            int r=new Random().nextInt(4);
            switch (r){
                case 0:f(new Shape());break;
                case 1:f(new Line());break;
                case 2:f(new Circle());break;
                case 3:f(new Square());break;
            }
        }
    }

     public static void f(Shape s) {
         System.out.println("-----------------------");
         new Scanner(System.in).nextLine();
         s.draw();
         //向上转型后,只能调用父类定义的通用成员,子类特有成员不能调用:s.length();
         //s对象的真实类型是 Line 类型
         if(s instanceof Line){
             Line line=(Line) s;//向下转型(父转子,显式转换)成Line类型,才能调用它特有的方法
             line.length();
         }
         s.clear();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值