使用用户输入创建新对象[JAVA]

时间:2015-08-18 12:31:55

标签: java class object dynamic user-input

您好我正在尝试创建一个程序,以便在用户输入特定对象的新信息时创建新对象。目前我有这个。

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();
         }

计算机超级

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());
    }
}

桌面子类

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("");
    }
}

笔记本电脑子课程

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("");
    }
}

所以有一个班级和2个子班级,但我认为这不重要。因此,如果我输入1,它应该为Desktop创建一个新对象,如果我输入2,它将为Laptop创建一个新对象。当我输入3时,它应该显示所有创建的对象。我不知道如何让他们中的任何一个工作,请帮助。

2 个答案:

答案 0 :(得分:2)

根据您提供的限制信息,我尝试解决问题:

  1. 我没有在Desktop和Laptop类中添加任何属性,在两个类中都覆盖了toString方法,即
  2. 
    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() + "]";
        }
    }

    您的主要方法如下:

    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"));
        }
    

答案 1 :(得分:0)

假设您有一个名为Computer的类/接口,笔记本电脑和桌面将从该接口扩展。

1)添加一个列表来存储计算机实例。

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

2)采取行动&#34; 1&#34;和&#34; 2&#34;添加到该列表,例如

computers.add(desktop1);

3)采取行动&#34; 3&#34;打印出清单。这假设您已实现toString()

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