首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java如何将多个类值添加到一个列表中,然后在重复提示的情况下解析该列表?

Java如何将多个类值添加到一个列表中,然后在重复提示的情况下解析该列表?
EN

Stack Overflow用户
提问于 2020-07-08 11:15:29
回答 1查看 84关注 0票数 1

我目前有5个类,它们接受几个条目并存储这些信息。这5个类是vanClass、truckClass等。在我的AutoPark类中,我创建了所有5个类的2个实例,并拥有10个变量,这些变量都包含我在声明它们时给出的信息。请看下面的内容以了解我所说的内容:

代码语言:javascript
复制
sedan1 = new sedan("Ford" , "Model-1" , "white" , 2015, 20000); // initialising sedan1 using sedan constructor
sedan2 = new sedan("Toyota" , "Model-2" , "gray" , 2010, 12000); // initialising sedan2 using sedan constructor

suv1 = new SUV("Ford" , "Model-1" , "white" , 2015, 20000, true); // initialising suv1 using SUV constructor
suv2 = new SUV("Toyota" , "Model-2" , "gray" , 2010, 12000, false); // initialising suv1 using SUV constructor
truck1 = new truckClass("Ford" , "Model-1" , 2015, 20000, true, "goods"); // initialising truck using truck constructor
truck2 = new truckClass("Toyota" , "Model-2" , 2010, 12000, false, "equipment"); // initialising truck using truck constructor

我需要能够搜索这5个类的10个实例中的所有字符串值。如何添加来自类的所有字符串值以生成以下内容:

下面是我的意思的一个例子:

代码语言:javascript
复制
Enter a string to search: FORD
There is a matching item available in our inventory
Enter a string to search: Sedan hatch back
No such item is available in our inventory.
Enter a string to search: honDA
There is a matching item available in our inventory
Enter a string to search: just a tire
No such item is available in our inventory.
Enter a string to search: quit

为了更好地理解,这里有完整的代码

代码语言:javascript
复制
import java.util.*;

class sedan {
    String make;
    String model;
    String color;
    int year;
    double price;
    boolean fourWD;
    boolean isheavyDuty;
    String carries;

    public sedan(String initMake, String initModel, String initColor, int initYear, double initPrice) {
        make = initMake;
        model = initModel;
        color = initColor;
        year = initYear;
        price = initPrice;
    }

    @Override
    public String toString() {
        String name = "Sedan";
        String main = (color + " " + make + " " + model + " " + name + " (" + year + ") costs $" + price);
        return main;
    }
}

class SUV {

    String make;
    String model;
    String color;
    int year;
    double price;
    boolean fourWD;
    String carries;

    public SUV(String initMake, String initModel, String initColor, int initYear, double initPrice, boolean initFourWD){
        make = initMake;
        model = initModel;
        color = initColor;
        year = initYear;
        price = initPrice;
        fourWD = initFourWD;
    }

    public String toString() {
        String name = "SUV";
        String main = new String();
        if (fourWD) {
            main = ("4WD " + color + " " + make + " " + model + " " + name + " (" + year + ") costs $" + price);
        }
        else {
            main = (color + " " + make + " " + model + " " + name + " (" + year + ") costs $" + price);
        }

        return main;
    }
}

class truckClass {

    String make;
    String model;
    String color;
    int year;
    double price;
    boolean fourWD;
    boolean isheavyDuty;
    String carries;

    public truckClass(String initMake, String initModel, int initYear, double initPrice, boolean initisheavyDuty, String initCarries) {
        make = initMake;
        model = initModel;
        year = initYear;
        price = initPrice;
        isheavyDuty = initisheavyDuty;
        carries = initCarries;

    }

    public String toString() {
        String name = "Truck";
        String main;
        if (isheavyDuty) {
            main = (make + " " + model + " heavy duty " + name + " (" + year + ") carries " + carries + " costs $" + price);

        }
        else {
            main = (make + " " + model + " " + name + " (" + year + ") carries " + carries + " costs $" + price);
        }

        return main;
    }
}
class vanClass {

    String make;
    String model;
    int year;
    double price;
    boolean isCovered;
    String carries;

    public vanClass(String initMake, String initModel, int initYear, double initPrice, boolean initisCovered, String initCarries){
        make = initMake;
        model = initModel;
        year = initYear;
        price = initPrice;
        isCovered = initisCovered;
        carries = initCarries;

    }

    public String toString() {
        String name;
        String main;
        if (isCovered){
            name = "covered Van";
            main = (make + " " + model + " " + name + " (" + year + ") carries " + carries + " costs $" + price);
        }
        else {
            name = "Van";
            main = (make + " " + model + " " + name + " (" + year + ") carries " + carries + " costs $" + price);
        }
        return main;
    }
}

class tireClass {

    int wheelDiameter;
    int sectionWidth;
    double price;
    boolean isPassengerTire;

    public tireClass(int initwheelDiameter, int initsectionWidth, double initPrice, boolean initisPassengerTire) {
        wheelDiameter = initwheelDiameter;
        sectionWidth = initsectionWidth;
        price = initPrice;
        isPassengerTire = initisPassengerTire;

    }

    public String toString() {
        String name;
        String main;
        if (isPassengerTire) {
            name = "Passenger tire ";
            main = (name + "with " + wheelDiameter + " in. wheel diameter " + sectionWidth +  "mm. section width, costs $" + price);
        }
        else {
            name = "Tire ";
            main = (name + "with " + wheelDiameter + " in. wheel diameter " + sectionWidth + "mm. section width, costs $" + price);
        }
        return main;

        }

    }

class AutoPark {
    String name;
    private sedan sedan1, sedan2;
    private SUV suv1, suv2;
    private truckClass truck1, truck2;
    private vanClass van1,van2;
    private tireClass tire1, tire2;
    private String item;
    private List myList;



    public AutoPark(String initName) {
        name = initName;
        sedan1 = new sedan("Ford" , "Model-1" , "white" , 2015, 20000); // initialising sedan1 using sedan constructor
        sedan2 = new sedan("Toyota" , "Model-2" , "gray" , 2010, 12000); // initialising sedan2 using sedan constructor

        suv1 = new SUV("Ford" , "Model-1" , "white" , 2015, 20000, true); // initialising suv1 using SUV constructor
        suv2 = new SUV("Toyota" , "Model-2" , "gray" , 2010, 12000, false); // initialising suv1 using SUV constructor

        truck1 = new truckClass("Ford" , "Model-1" , 2015, 20000, true, "goods"); // initialising truck using truck constructor
        truck2 = new truckClass("Toyota" , "Model-2" , 2010, 12000, false, "equipment"); // initialising truck using truck constructor

        van1 = new vanClass("Ford" , "Model-1" , 2015, 12000, true, "goods");
        van2 = new vanClass("Toyota" , "Model-2" , 2010, 45000, false, "equipment");

        tire1 = new tireClass(12,35,400,true);
        tire2 = new tireClass(8,45,350,false);

        List<String> myList = new ArrayList<String>();
        myList.add(sedan1.make);
        myList.add(sedan1.color);
        myList.add(sedan1.model);
        System.out.println(myList);


        }
    public void displayAllItems() {
        System.out.println("The " + name + " includes:");
        System.out.println(sedan1);
        System.out.println(sedan2);
        System.out.println(suv1);
        System.out.println(suv2);
        System.out.println(truck1);
        System.out.println(truck2);
        System.out.println(van1);
        System.out.println(van2);
        System.out.println(tire1);
        System.out.println(tire2);
    }

    public void searchItems(String initItem){
        //checks toString of any product in the parks inventory

        item = initItem;

        while (true) {
            if (myList.contains(item)) {
                System.out.println("TRUE");
            }
            else {
                System.out.println("False!");
            }
            break;
        }
    }
}

class AutoParkTesterProgram {
    public void main() {
        AutoPark a1;
        a1 = new AutoPark("Carleton Auto park");
        //a1.displayAllItems();

        //then loop repeatedly promt user to enter an item to search for
        //when type quit, process stops and program end
        //otherwise, searchitems of autopark used to search and result should be printed
        /*
Enter a string to search: FORD
There is a matching item available in our inventory
Enter a string to search: Sedan hatch back
No such item is available in our inventory.
Enter a string to search: honDA
There is a matching item available in our inventory
         */

    }
}


public class auto {
    public static void main(String args[]) {
        AutoPark a1;
        a1 = new AutoPark("Carleton Auto park");
        //a1.displayAllItems();
        a1.searchItems("ford");



    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-08 13:10:15

这类问题是使用类的绝佳机会。Java是一种面向对象的语言,您可以使用它来模拟类似的行为,这样您就不会在多个地方重复自己。

您的大多数类都是汽车,所以还有什么比创建Car类更好的方式来表示这一点呢?许多字段在car类之间共享,它们都可以移动到一个位置,如下所示:

代码语言:javascript
复制
public abstract class Car {
  public String make;
  public String model;
  public String color;
  public int year;
  public double price;
  public String carries;

  public Car(String make, String model, String color, int year, double price, String carries) {
    this.make = make;
    this.model = model;
    this.color = color;
    this.year = year;
    this.price = price;
    this.carries = carries;
  }
}

该类的abstract修饰符阻止您创建Car的新实例。那么为什么要为一个不能创建实例的类定义构造函数呢?这是因为你可以在任何子类的构造函数中调用它。

下面是一个如何对Sedan类建模的示例:

代码语言:javascript
复制
public class Sedan extends Car {
  public boolean fourWD;
  public boolean isHeavyDuty;

  public Sedan(String make, String model, String color, int year, double price, String carries, boolean fourWD, boolean isHeavyDuty) {
    super(make, model, color, year, price, carries);
    this.fourWD = fourWD;
    this.isHeavyDuty = isHeavyDuty;
  }

  @Override
  public String toString() {
    String name = "Sedan";
    String main = (color + " " + make + " " + model + " " + name + " (" + year + ") costs $" + price);
    return main;
  }
}

extends Car允许您在Car类中定义的逻辑之上进行构建。在这里,Sedan构造函数可以通过使用super并传递必要的参数来调用Car构造函数。然后,您可以像以前一样覆盖toString方法。

您可以为SUV、卡车和面包车创建类似的类,这样您就不需要重复公共字段,并且可以专注于区分每种类型的车辆。

那么,所有这些如何帮助您解决搜索字符串的初始问题呢?在您的AutoPark类中,您可以使用List<Car>来跟踪公园中的所有车辆。由于这些对象中的每一个都将是Car类型,因此您将能够对所有这些对象做出某些假设,例如,每个对象都将具有一个make字段。您还可以将Car类的任何实例添加到此List中,而不必创建新变量。

现在,当您在searchItems中搜索字符串时,可以遍历所有汽车,查看其中是否有字段包含要搜索的字符串。如果您使用的是for-each循环,它可能如下所示:

代码语言:javascript
复制
for (Car car: cars) {
  if (car.make.contains(initItem) || car.model.contains(initItem)) {
    System.out.println("TRUE");
    break;
  } else {
    System.out.println("False!");
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62786996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档