Using user input to create a new object [JAVA]

2020-03-28 02:53发布

问题:

Hi I am trying to create a program to create a new object whenever the user inputs a new information for a certain object. Currently I have this.

import java.util.Scanner;
public class Main
{
    public static void main (String args[]) 
{
    String input;
    Scanner scanner = new Scanner(System.in);
    do
    {
        System.out.println("Computer Menu");  
        System.out.println("1. Add a new Desktop Information");
        System.out.println("2. Add a new Laptop Information");
        System.out.println("3. Display all Computer Information");
        System.out.println("4. Quit");

        System.out.print("Please enter either 1 to 4: "); 
        input =(scanner.nextLine());
        if (input.equals("1"))
        {
            Desktop desktop1 = new Desktop();
            System.out.println("Please enter Desktop ID: ");
            desktop1.setID (scanner.nextLine());
            System.out.println("Please enter Desktop Processor Speed: ");
            desktop1.setProcess (Double.parseDouble(scanner.nextLine()));
            System.out.println("Please enter Desktop RAM: ");
            desktop1.setRam (Double.parseDouble(scanner.nextLine()));
            System.out.println("Please enter Desktop Harddisk Space: ");
            desktop1.setDisk (Double.parseDouble(scanner.nextLine()));
            System.out.println("Please enter Desktop Monitor Type: ");
            desktop1.setMonitor (scanner.nextLine());
            System.out.println("Please enter Desktop Price: ");
            desktop1.setPrice (Double.parseDouble(scanner.nextLine()));
            desktop1.displayComputer();
        }
        else if (input.equals("2"))
        {
            Laptop laptop1 = new Laptop();
            System.out.println("Please enter Laptop ID: ");
            laptop1.setID (scanner.nextLine());
            System.out.println("Please enter Laptop Processor Speed: ");
            laptop1.setProcess (Double.parseDouble(scanner.nextLine()));
            System.out.println("Please enter Laptop RAM: ");
            laptop1.setRam (Double.parseDouble(scanner.nextLine()));
            System.out.println("Please enter Laptop Harddisk Space: ");
            laptop1.setDisk (Double.parseDouble(scanner.nextLine()));
            System.out.println("Please enter Laptop Weight: ");
            laptop1.setWeight (Double.parseDouble(scanner.nextLine()));
            System.out.println("Please enter Laptop Price: ");
            laptop1.setPrice (Double.parseDouble(scanner.nextLine()));
            laptop1.displayComputer();
         }

Computer super class

public class Computer
{
    String ID;
    double process;
    double ram;
    double disk;
    double price;

    void setID (String _id)
    {
        ID = _id;
    }
    void setProcess (double _Process)
    {
        process = _Process;
    }
    void setRam (double _Ram)
    {
        ram = _Ram;
    }
    void setDisk (double _Disk)
    {
        disk = _Disk;
    }
    void setPrice (double _Price)
    {
        price = _Price;
    }

    String getID()
    {
        return ID;
    }
    double getProcess()
    {
        return process;
    }
    double getRam()
    {
        return ram;
    }
    double getDisk()
    {
        return disk;
    }
    double getPrice()
    {
        return price;
    }

    void displayComputer()
    {
        System.out.println("Computer ID: " + getID());
        System.out.println("Processor Speed: " + getProcess());
        System.out.println("RAM: " + getRam());
        System.out.println("Harddisk: " + getDisk());
        System.out.println("Price: " + getPrice());
    }
}

Desktop sub class

public class Desktop extends Computer
{
    String monitor;

    void setMonitor (String _Monitor)
    {
        monitor = _Monitor;
    }
    String getMonitor()
    {
        return monitor;
    }

    void displayComputer()
    {
        System.out.println("Computer ID: " + getID());
        System.out.println("Processor Speed: " + getProcess());
        System.out.println("RAM: " + getRam());
        System.out.println("Harddisk: " + getDisk());
        System.out.println("Monitor Type: " + getMonitor());
        System.out.println("Price: " + getPrice());
        System.out.println("");
    }
}

Laptop sub class

public class Desktop extends Computer
{
    double Weight;

    void setWeight(String _Weight)
    {
        Weight= _Weight;
    }
    String getWeight()
    {
        return Weight;
    }

    void displayComputer()
    {
        System.out.println("Computer ID: " + getID());
        System.out.println("Processor Speed: " + getProcess());
        System.out.println("RAM: " + getRam());
        System.out.println("Harddisk: " + getDisk());
        System.out.println("Weight: " + getWeight());
        System.out.println("Price: " + getPrice());
        System.out.println("");
    }
}

So There is one class and 2 sub classes but I don't think it matters. So if i entered 1 it should create a new object for Desktop and if i entered 2 it would create a new object for Laptop. And when I enter 3 it should show all the objects created. I don't know how to get any of them to work please help.

回答1:

With the limit information provided by you I try to solve the issue:

  1. I am not adding any attributes to Desktop and Laptop class, overriding toString Method in both the Classes i.e.

public class Laptop {
    public String toString() {
        return "Laptop [getClass()=" + getClass() + ", hashCode()="
                + hashCode() + ", toString()=" + super.toString() + "]";
    }
}

public class Desktop {
    public String toString() {
        return "Desktop [getClass()=" + getClass() + ", hashCode()="
                + hashCode() + ", toString()=" + super.toString() + "]";
    }
}

Your Main method look like:

public static void main (String args[]) 
    {
        String input;
        Scanner scanner = new Scanner(System.in);
        List<Object> listOfObject = new ArrayList<>();
        do
        {
            System.out.println("Computer Menu");  
            System.out.println("1. Add a new Desktop Information");
            System.out.println("2. Add a new Laptop Information");
            System.out.println("3. Display all Computer Information");
            System.out.println("4. Quit");

            System.out.print("Please enter either 1 to 4: "); 
            input =(scanner.nextLine());
            if (input.equals("1")){
                Desktop desktop1 = new Desktop();
                listOfObject.add(desktop1);
            }else if (input.equals("2")){
                Laptop laptop1 = new Laptop();
                listOfObject.add(laptop1);
             }else if(input.equals("3")){
                 for(Object obj : listOfObject){
                     if(obj instanceof Desktop){
                        Desktop d1 = (Desktop)obj;
                        System.out.println(d1.toString());
                     }else if(obj instanceof Laptop){
                         Laptop l1 = (Laptop)obj;
                         System.out.println(l1.toString());
                     }
                 }
             }
        }while(!input.equals("4"));
    }


回答2:

Assuming you have a class/interface named Computer from which Laptop and Desktop extend.

1) Add a list to store Computer instances.

List<Computer> computers = new ArrayList<Computer>()

2) Make actions "1" and "2" add to that list, e.g.

computers.add(desktop1);

3) Make action "3" print out the list. This assumes you've implemented toString()

for (Computer computer : computers) {
   System.out.println(computer);
}