Java-集合编程小练习
Posted HJ0101
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-集合编程小练习相关的知识,希望对你有一定的参考价值。
需求:
代码:
/** * * 汽车类 * */ public class Car { private String brand;//汽车品牌 private double price;//汽车价格 private String color;//汽车颜色 public Car() { } public Car(String brand, double price, String color) { this.setBrand(brand); this.setPrice(price); this.setColor(color); } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "品牌:" + brand + "\\n价格:" + (int)price + "\\n颜色:" + color+"\\n"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((brand == null) ? 0 : brand.hashCode()); result = prime * result + ((color == null) ? 0 : color.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if(this.getBrand().equals(brand)&&this.getPrice() == price&&this.getColor().equals(color)) { return true; }else return false; } }
/** * 汽车管理类 */ import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; import java.util.Set; public class CarManager { HashSet<Car> set = new HashSet<>(); Scanner sc = new Scanner(System.in); // 添加汽车的方法: public void add() { Car car = new Car(); System.out.println("请输入汽车的品牌:"); car.setBrand(sc.next()); System.out.println("请输入汽车的价格:"); car.setPrice(sc.nextDouble()); System.out.println("请输入汽车的颜色:"); car.setColor(sc.next()); set.add(car); System.out.println("汽车信息添加成功!!"); } // 查看所有汽车的信息 public void show() { if(set.isEmpty()) { System.out.println("还没有汽车信息,快添加一些吧!"); }else { System.out.println("汽车信息:"); for(Car car:set) { System.out.println(car); } } } //根据汽车品牌修改汽车信息 public void changBrand(){ boolean flag = false; System.out.println("请输入您要修改的汽车品牌:"); Scanner sc = new Scanner(System.in); String brand = sc.next(); System.out.println("请输入您要修改的价格:"); Double price = sc.nextDouble(); Iterator<Car> it = set.iterator(); while(it.hasNext()) { Car c = it.next(); if(c.getBrand().equals(brand)) { c.setPrice(price); flag = true; } } if(flag) { System.out.println("修改成功!!"); }else System.out.println("没有找到该品牌的汽车~"); } //根据汽车品牌删除汽车的方法 public void remove() { boolean flag = false; System.out.println("请输入您要删除的汽车品牌:"); Scanner sc = new Scanner(System.in); String brand = sc.next(); Set<Car> set1 = new HashSet<Car>(); for(Car car:set) { if(car.getBrand().equals(brand)) { set1.add(car); flag = true; } } if(flag) { set.removeAll(set1); System.out.println("删除成功!!"); }else System.out.println("没有找到该品牌的汽车~"); } }
/** * 测试类(程序启动类) */ import java.util.Scanner; public class CarTest { //菜单方法 public void menu() { System.out.println("**************************"); System.out.println("\\t1.添加汽车信息"); System.out.println("\\t2.查看所有汽车信息"); System.out.println("\\t3.根据汽车品牌修改汽车价格"); System.out.println("\\t4.根据汽车品牌删除汽车"); System.out.println("\\t0.退出程序"); System.out.println("**************************"); System.out.println("请输入您选择的操作:"); } public static void main(String[] args) { CarManager cm = new CarManager(); CarTest ct = new CarTest(); ct.menu(); Scanner sc = new Scanner(System.in); while(true) { int n = sc.nextInt(); switch(n) { case 1: cm.add(); ct.menu(); continue; case 2: cm.show(); ct.menu(); continue; case 3: cm.changBrand(); ct.menu(); continue; case 4: cm.remove(); ct.menu(); continue; case 0: System.exit(1); default: System.out.println("选择错误,请重新选择:"); } } } }
以上是关于Java-集合编程小练习的主要内容,如果未能解决你的问题,请参考以下文章