在数组中添加对象并将它们保存在txt文件中

时间:2017-11-20 22:27:14

标签: java object

我是java的新手,我想我想尝试一下。我制作的程序更像是2007年至2017年iphone的时间表,每年都有iphone名称,内存大小,描述和利润。我能够显示所有信息,但我现在希望用户能够输入新的事件,如新的iPhone,内存大小和利润。到目前为止,所有信息都只是在控制台中打印出来:

Name:Iphone (2007)
Year:2007
Minimum Memeory Size avalible:4
Maximum MemorySize avaliable:16
iphone Details:It is the first generation of iPhone that was
 announced on January 9, 2007
Profit made in (billions $):2.0

但现在我希望用户输入新手机,例如

enter the iphone year: 
enter the name of the iphone: 
enter minimum memory size: 

这是我的第一堂课:

import java.io.*;
public class iphoneDescriptions {

   String nameOfPhone;
   int year;
   int MinMemorySize;
   int MaxMemorySize;
   String details;
   double profit;
   String Creator;
  //String iphonemaker;


   public iphoneDescriptions(String nameOfPhone) {
      this.nameOfPhone = nameOfPhone;
   }


  // public void iphoneDescriptions(String string) {
       //this.nameOfPhone = nameOfPhone;
//}


public void iphoneYear(int iphoneYear) {
      year = iphoneYear;
   }


   public void MaxMemorySize(int iphoneMaxMemorySize) {
       MaxMemorySize = iphoneMaxMemorySize;
       }
   public void MinMemorySize(int iphoneMinMemorySize) {
       MinMemorySize = iphoneMinMemorySize;
       }


   public void iphoneDetails(String iphoneDetails) {
       details = iphoneDetails;
   }

 public void Creator (String iphonemaker) {
      Creator = iphonemaker;
 }


   public void ProfitMade(double ProfitMade) {
      profit = ProfitMade;
   }





   /* Print the Employee details */
   public void printiphoneDescriptions() {
      System.out.println("Name:"+ nameOfPhone );
      System.out.println("Year:" + year );
      System.out.println("Minimum Memeory Size avalible:" + MinMemorySize);
      System.out.println("Maximum MemorySize avaliable:" + MaxMemorySize );
      System.out.println("iphone Details:" + details);
      System.out.println("Profit made in (billions $):" + profit);
    // System.out.println("Name of the creator:" + Creator);

   }

public void setIphone(String nameOfPhone) {
     this.nameOfPhone = nameOfPhone;
}

public String getIphone() {
    return nameOfPhone;

}
public void setCreator (String iphonemaker) {
     this.Creator = iphonemaker;
}

public String getCreator() {
    return Creator;

}


public void setiphoneDetails(String  nameOfPhone) {
     this.nameOfPhone = nameOfPhone;
}

public String getiphoneDetails() {
    return nameOfPhone;

}

//public void setCreator1 (String  iphonemaker) {
     //this.iphonemaker = iphonemaker;
//}

//public String getCreator1() {
    //return Creator;

}

和我的第二堂课:

import java.io.*;
public class Iphone {

   public static void main(String args[]) {
      /* Create two objects using constructor */
      iphoneDescriptions Iphone1 = new iphoneDescriptions("Iphone (2007)");

Iphone1.iphoneYear(2007);
      Iphone1.MinMemorySize(4);    
      Iphone1.MaxMemorySize(16);
      Iphone1.iphoneDetails("It is the first generation of iPhone that was\r\n announced on January 9, 2007");
      Iphone1.ProfitMade(2.0);
      Iphone1.printiphoneDescriptions();
}
}

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

用户输入所需信息的简单方法是使用Scanner类。使用以下内容实例化扫描仪对象:

Scanner kb = new Scanner(System.in);

然后获取您需要的信息。

System.out.println("enter the iphone year");
int year = kb.nextInt();
kb.nextLine(); // this is to consume the newline left from the previous call to nextInt();
System.out.println("enter the name of the phone");
String phonename = kb.nextLine();
System.out.println("enter minimum memory size");
String memory = kb.nextLine();

然后使用您的方法使用此信息实例化新的Iphone。