Java - 无法创建对象。收到错误消息:"Object cannot be resolved to a type"

Java - Cannot create object. Getting error message: "Object cannot be resolved to a type"

新程序员学习Java。我有一个任务,我必须创建一个数据库来存储消费者信息。现在我已经构建了两个 classes。 Consumer class 本身,第二个,将 class 与 main 方法分开。我仍在构建程序,但我已经 运行 遇到了一些我似乎无法克服的问题。我的程序不允许我创建 class 消费者的任何对象。如果有人有机会请帮忙。

消费者class代码:

import java.lang.Enum;

public class Consumer {
    
    private String name;
    private int streetNumber;
    private String streetName;
    private String city;
    private String postalCode;
    private int age;
    private String gender;
    private static int numConsumers;
    

    
    public Consumer(String name) {
        numConsumers++;
        this.name = name;
        this.streetNumber = 0;
        this.streetName = " ";
        this.city = " ";
        this.postalCode = " ";
        this.age = 0;
        this.gender = " ";
        MaritalStatus maritalStatus = MaritalStatus.SINGLE;
        Education education = Education.HIGH_SCHOOL;
    }
    
    public enum MaritalStatus {
        SINGLE,
        COMMON_LAW,
        MARRIED,
        SEPARATED,
        DIVORCED,
        WIDOWED
    }
    
    public enum Education {
        HIGH_SCHOOL,
        COLLEGE,
        UNIVERSITY,
        NONE
    }
    
    public void setName(String newName) {
        name = newName;
    }
    
    public String getName() {
        return name;
    }
    
    public void setStreetNumber (int n) {
        streetNumber = n;
    }
    
    public int getStreetNumber() {
        return streetNumber;
    }
    
    public void setStreetName(String newStreetName) {
        streetName = newStreetName;
    }
    
    public String getStreetName() {
        return streetName;
    }
    
    public void setCity(String newCity) {
        city = newCity;
    }
    
    public String getCity() {
        return city;
    }
    
    public void setPostalCode(String newPostalCode) {
        postalCode = newPostalCode;
    }
    
    public String getPostalCode() {
        return postalCode;
    }
    
    public void setAge (int n) {
        age = n;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setGender(String newGender) {
        gender = newGender;
    }
    
    public String getGender() {
        return gender;
    }
    
    public String toString() {
        System.out.println();
        System.out.println("Displaying information for consumer " + name);
        System.out.println("Address: " + streetNumber + " " + streetName + ", " + city + ", " + postalCode);
        System.out.println("Age: " + age);
        System.out.println("Gender: " + gender);
        System.out.println("Marital status: " + MaritalStatus.class);
        System.out.println("Education level: " + Education.class);
        return " ";
    }
    
    public static String menu() {
        System.out.println("What do you want to do?");
        System.out.println("\t 1. Enter a new Consumer (password required)");
        System.out.println("\t 2. Change all information of a Consumer (password required)");
        System.out.println("\t 3. Display all Consumers similar to a given consumer");
        System.out.println("\t 4. Display all Consumers with a given age and location");
        System.out.println("\t 5. Quit");
        System.out.println("Please enter your choice > ");
        return " ";
    }
    
    public static int getNumConsumers() {
        return numConsumers;
    }
    

}

以及我的主要代码 class/method

import java.util.Scanner;

public class Capitalism {
    
    private static boolean validatedPassword;
    private static String correctPassword = "password";
    private static String passwordGuess;
    private static int passwordAttempts;
    private static int totalPasswordAttempts;
    private static int maxNumConsumers;
    private static int userMenuChoice;
    
    static int getMaxConsumers() {
        
        Scanner userInput = new Scanner(System.in); 
        System.out.println("Please enter the maximum number of consumers your company can handle");
        
        do {
            if(userInput.hasNextInt()) {
                maxNumConsumers = userInput.nextInt();
                if(maxNumConsumers <= 0) {
                    System.out.println("Invalid input. Please enter a positive number");
                }
            } else {
                System.out.println("Invalid input. Please enter a positive number (1, 8, 15, etc).");
                userInput.nextLine();
            }
        } while (maxNumConsumers <= 0);
        
        System.out.println("Your company can handle " + maxNumConsumers + " consumers.\n");
        return maxNumConsumers;
    }
    
    static void menu() {
        
        System.out.println("What do you want to do?");
        System.out.println("\t 1. Enter a new Consumer (password required)");
        System.out.println("\t 2. Change all information of a Consumer (password required)");
        System.out.println("\t 3. Display all Consumers similar to a given consumer");
        System.out.println("\t 4. Display all Consumers with a given age and location");
        System.out.println("\t 5. Quit");
        System.out.println("Please enter your choice > ");
        
    }
    
    static int getUserMenuChoice() {
        Scanner userInput = new Scanner(System.in); 
        
        do {
            if(userInput.hasNextInt()) {
                userMenuChoice = userInput.nextInt();
                if(userMenuChoice <= 0 || userMenuChoice >= 6) {
                    System.out.println("Invalid input. Please enter a number between 1 and 5.");
                }
            } else {
                System.out.println("Invalid input. User must enter a number with no letters (i.e. 4 instead of four).");
                userInput.nextLine();
            }
        } while (userMenuChoice <= 0 || userMenuChoice >=6);
        
        System.out.println("You have selected option " + userMenuChoice + ".\n");
        return userMenuChoice;
    }

    static void validatePassword() {
        while (!validatedPassword && passwordAttempts < 3) {
            System.out.println("Please enter your password.");
            Scanner userInput = new Scanner(System.in);
            passwordGuess = userInput.next();
            if (passwordGuess.equals(correctPassword)) {
                System.out.println("Correct password. Okay to proceed.");
                validatedPassword = true;
                break;
            } else if (passwordAttempts < 2) {
                System.out.println("Incorrect password. Max of 3 attempts before returning to main menu.");
                passwordAttempts++;
                totalPasswordAttempts++;
            } else {
                passwordAttempts = 0;
                totalPasswordAttempts++;
                if (totalPasswordAttempts == 12) {
                    System.out.println("Program has detected suspicious activity and will terminate immediately.");
                    System.exit(0);
                }
                System.out.println("Maximum number of attempts reached. Returning to main menu.");
                break;
            }   
        }
    }
    
    static void validatePassword2() {
        while (!validatedPassword && passwordAttempts < 3) {
            System.out.println("Please enter your password.");
            Scanner userInput = new Scanner(System.in);
            passwordGuess = userInput.next();
            if (passwordGuess.equals(correctPassword)) {
                System.out.println("Correct password. Okay to proceed.");
                validatedPassword = true;
                break;
            } else if (passwordAttempts < 2) {
                System.out.println("Incorrect password. Max of 3 attempts.");
                passwordAttempts++;
                totalPasswordAttempts++;
            } else {
                System.out.println("Program terminated due to safety reasons.");
                System.exit(0);
            }   
        }
    }
    
    public static void main(String[] args) {
        
        Consumer consumerOne = new Consumer(); // this is the line where the error message comes up
        
        boolean runProgram = true;
        System.out.println("Welcome to the consumer information database. A place to store and update consumer information. \n");

        getMaxConsumers();
        
        while(runProgram) {
            menu();
            getUserMenuChoice();
            
            switch(userMenuChoice) {
            case 1:
                validatePassword();
                System.out.println("Then we continue the rest of this nonesense");
                break;
            case 2:
                validatePassword2();
                break;
            case 3:
            case 4:
            case 5:
                runProgram = false;
                break;
            }
        
        }
        
        System.out.println("Thank you for using the database. May all your capitalist dreams come true.");
        System.out.println("Program is now terminated.");

        }

    

}

您正在尝试使用构造函数 Consumer()[=23= 创建 class Consumer 的实例] 未定义。

您定义的唯一构造函数是Consumer(String name).

您需要向 Consumer 添加默认构造函数 class:

 public Consumer() {
    numConsumers++;
    this.name = "Undefined"; //Or just set it to null
    this.streetNumber = 0;
    this.streetName = " ";
    this.city = " ";
    this.postalCode = " ";
    this.age = 0;
    this.gender = " ";
    MaritalStatus maritalStatus = MaritalStatus.SINGLE;
    Education education = Education.HIGH_SCHOOL;
}

您的 class Consumer 只有一个构造函数和一个必需的字符串参数 public Consumer(String name),但在 main 中您没有向构造函数传递任何内容。

你应该写

Consumer consumerOne = new Consumer("John");