Java高编考试题

1.现附近有5个超市,每个超市都有自己的店名,离你的距离(double),是否正在营业(boole
an),营业额(double)四个属性,要求使用任意集合将5个超市添加进去,然后可以按照任
一一个属性进行排序。
要求:使用菜单完成4种排序(店名,距离,是否营业,营业额)查看。
参考代码

package kaoshi1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 * 1. 现附近有5个超市,每个超市都有自己的店名,离你的距离(double), 是否正在营业(boolean),营业额(double)四个属性,
 * 要求使用任意集合将5个超市添加进去,然后可以按照任一一个属性进行排序。 要求:使用菜单完成4种排序(店名,距离,是否营业,营业额)查看
 * 
 * @author ASUS Berlin 2017.07.15
 */
public class Test1 {
    public static void main(String[] args) {
        ArrayList<Supermarket> list = new ArrayList<>();
        list.add(new Supermarket("tianfu", 20.3, true, 5000));
        list.add(new Supermarket("jialefu", 30.6, true, 7000));
        list.add(new Supermarket("xinggan", 42, false, 8000));
        list.add(new Supermarket("tianfu", 50.9, false, 5000));
        list.add(new Supermarket("doulin", 15.4, true, 5000));
        ArrayList<Comparator<Supermarket>> sortList = new ArrayList<>();
        sortList.add(new NameSort());
        sortList.add(new DisSort());
        sortList.add(new IsOpenSort());
        sortList.add(new ValueSort());

        Scanner input = new Scanner(System.in);
        String next = "";
        do {
            System.out.println("选择排序的菜单");
            System.out.println("1.名称2.距离3.营业4.营业额");
            next = input.next();
            try {
                int i = Integer.parseInt(next);// 将字符串转换成整数
                if (i == 0) {
                    break;
                } else if (i > 0 && i < 5) {
                    i--;
                    // 排序
                    Collections.sort(list, sortList.get(i));
                    System.out.println("名称\t距离\t营业\t营业额");
                    for (Supermarket supermarket : list) {
                        System.out.println(supermarket.toString());
                    }

                } else {
                    System.out.println("输入错误");
                }

            } catch (Exception e) {
                System.out.println("输入有误,请重新输入");
            }

        } while (!next.equals("0"));
        input.close();
    }

    // 四种比较器
    // 1.名字比较
    public static class NameSort implements Comparator<Supermarket> {

        @Override
        public int compare(Supermarket o1, Supermarket o2) {
            return o1.getName().compareTo(o2.getName());
        }

    }

    // 2.距离比较
    public static class DisSort implements Comparator<Supermarket> {

        @Override
        public int compare(Supermarket o1, Supermarket o2) {
            if (o1.getDis() - o2.getDis() > 0) {
                return 1;
            } else if (o1.getDis() - o2.getDis() < 0) {
                return -1;
            }
            return 0;
        }

    }

    // 3.是否营业比较
    public static class IsOpenSort implements Comparator<Supermarket> {

        @Override
        public int compare(Supermarket o1, Supermarket o2) {
            if (o1.isOpen()) {
                return 1;
            }
            return -1;
        }

    }

    // 营业额比较
    public static class ValueSort implements Comparator<Supermarket> {

        @Override
        public int compare(Supermarket o1, Supermarket o2) {
            if (o1.getValue() > o2.getValue()) {
                return 1;
            } else if (o1.getValue() < o2.getValue()) {
                return -1;
            }
            return 0;
        }

    }

}


package kaoshi1;

public class Supermarket {
    private String name;

    private double dis;// 距离

    private boolean isOpen;// 是否营业

    private double value; // 营业额多少

    public Supermarket(String name, double dis, boolean isOpen, double value) {
        super();
        this.name = name;
        this.dis = dis;
        this.isOpen = isOpen;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getDis() {
        return dis;
    }

    public void setDis(double dis) {
        this.dis = dis;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setOpen(boolean isOpen) {
        this.isOpen = isOpen;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return name + "\t" + dis + "\t" + isOpen + "\t" + value;
    }

}

2.创建一个名为Timer的线程类,用来显示当前时间,格式为 2017年7月15日 上午9点42分23
秒,线程启动后 每5秒显示一次时间。(使用时间工具类)

package kaoshi2;

import java.text.SimpleDateFormat;
import java.util.Date;

/*
 *创建一个名为Timer的线程类,用来显示当前时间,
 *格式为 2017年7月15日 上午9点42分23秒,线程启动后 每5秒显示一次时间。
 *
 *   Berlin 2017.07.15
 */
public class Test1 {
    public static void main(String[] args) {
        new Timer().start();
    }
}

class Timer extends Thread {
    SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  a HH时mm分ss秒");

    @Override
    public void run() {
        super.run();
        while (true) {
            long time = System.currentTimeMillis();
            String strtime = format.format(new Date(time));
            System.out.println(strtime);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }

}

3.将下面的所有数据写入一个D盘中的test目录下的test.txt文件中。要求使用代码创建文件夹和文件。
Prepare your own dictionary of words. Every day, try to write down some of the key new words that you have mastered that day. 写入完成后,打印出文件的长度,并且将他 复制(不是剪切) 到E盘中的newdir目录中。
文件名仍然为test.txt

package kaoshi3;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 将下面的所有数据写入一个D盘中的test目录下的test.txt文件中。要求使用代码创建文件夹和文件。
 Prepare your own dictionary of words. Every day, try to write d
 own some of the key new words that you have mastered that day.
 写入完成后,打印出文件的长度,并且将他 复制(不是剪切) 到E盘中的newdir目录中。文件名仍然为test.tx
 *   Berlin 2017.07.15      
 */
public class Test1 {
    public static void main(String[] args) throws IOException {
        File file = new File("D:/test/test.txt");
        createFile(file);
        copyFile(file, "E:/newDir/test.txt");
    }

    // 创建文件
    private static void createFile(File file) throws IOException {
        if (!file.exists()) {
            file.getParentFile().mkdirs();// 创建父目录(文件夹)
            file.createNewFile();
        }
        String msg = "Prepare your own dictionary of words. Every day, try to write down some of the key new words that you have mastered that day.";
        // 文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。
        // 创建一个向指定 File 对象表示的文件中写入数据的文件输出流
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(msg.getBytes());
        fos.flush();
        fos.close();
        System.out.println("文件的长度:" + file.length());

    }

    // 复制文件 //新的文件路径
    private static void copyFile(File file, String strFile) throws IOException,
            FileNotFoundException {
        File newFile = new File(strFile);
        if (!newFile.exists()) {
            newFile.getParentFile().mkdirs();
            newFile.createNewFile();
        }
        // 从文件系统中的某个文件中获取输入字节
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(newFile);
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = fis.read(buf)) != -1) {
            fos.write(buf, 0, len);
        }
        fos.close();
        fis.close();
    }
}

4.有一名农夫,从2000.1.1开始三天打鱼,两天晒网,现在要求算出输入的任何大于2000年的一
天他正在打鱼还是晒网,输入时间不合法抛出异常。将每次应用程序输入的记录保存在文件中形
成日志(文件名如:log1.txt,log2.txt,log3.txt等排列,)。

package kaoshi4;

import java.io.File;
//import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

/*
 有一名农夫,从2000.1.1开始三天打鱼,两天晒网,现在要求算出输入的任何大于2000年的一
 天他正在打鱼还是晒网,输入时间不合法抛出异常。将每次应用程序输入的记录保存在文件中形
 成日志(文件名如:log1.txt,log2.txt,log3.txt等排列,)。
 *   Berlin 2017.07.15 
 */
public class Test1 {
    static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    static File file = null;

    public static void main(String[] args) throws ParseException {
        getFile();
        String next = "";
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("请输入需要搜索的日期(格式:2017-07-15)");
            next = input.next();
            try {
                long time = format.parse(next).getTime();
                long oldTime = format.parse("2000-01-01").getTime();
                if (time < oldTime) {
                    throw new IllegalArgumentException("日期必须大于2000.01.01");
                } else {
                    long day = ((time - oldTime) / 1000 / 60 / 60 / 24) % 5;
                    String msg;
                    if (day < 3) {
                        msg = next + "农夫在打鱼";
                    } else {
                        msg = next + "农夫在嗮网";
                    }
                    System.out.println(msg);
                    save(msg);
                }

            } catch (Exception e) {
                System.out.println("输入的日期有问题,请重新输入。");
            }
            if (next.equals("exit")) {
                break;
            }

        }

        input.close();
    }

    private static void save(String msg) throws IOException {
        // 追加
        FileOutputStream fos = new FileOutputStream(msg, true);
        fos.write(msg.getBytes());
        fos.write("\r\n".getBytes());
        fos.flush();
        fos.close();
    }

    public static void getFile() {
        int i = 1;
        do {

            file = new File("log" + i + ",txt");// 文件格式
            if (file.exists()) {
                file = null;
                i++;
            }

        } while (file == null);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值