0

i am new to java and i thought i want to experiment. i am making a program that is more of a timeline from 2007 to 2017 of iphones , each year has the iphone name, memory size, description and the profit made. i am able to display all that information but i now want users to be able to input a new event, like a new iphone, what the memory size is and the profit made. so far all the information just prints in the console like this:

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

but now i want the user to input a new phone like for example

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

this is what my first class looks like:

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;

}

and my second class:

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();
}
}
H.laksari
  • 3
  • 2
  • Hey, in order to better understand your code, make sure you follow Java naming conventions. Class names should start with uppercase, e.g: IphoneDescriptions. Variable names should start with lowercase, e.g: creator. Methods should start with lowercase, e.g: profitMade(). – Morgan Nov 20 '17 at 22:34
  • Thank you, I will bear that in mind :) – H.laksari Nov 20 '17 at 22:36

2 Answers2

0

Now, just like you print into console, you may want to read input from console. This may be helpful. How can I read input from the console using the Scanner class in Java?

And once you do that learn about delimiters and IO operations. See https://www.google.co.in/url?q=https://stackoverflow.com/questions/14723418/how-to-save-lines-of-console-input-as-a-txt-file&sa=U&ved=0ahUKEwjJv4yem87XAhXJvo8KHeWlCFAQFggLMAA&usg=AOvVaw2ZbNopNGOREh-WcoJm9_F4

Mahesh V S
  • 462
  • 1
  • 7
  • 21
0

A simple way for the user to input the required information is by using the Scanner class. Instantiate a scanner object with the following:

Scanner kb = new Scanner(System.in);

then get the information you need.

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

Then just use your methods to instantiate a new Iphone using this information.

IceManStan
  • 130
  • 2
  • 8
  • thanks, and how to i get it to save the results and print them back to the user? – H.laksari Nov 21 '17 at 20:53
  • After obtaining the information from the user save it in an instance of your phone class then just print out the info from your object. You should override the toString method in your phone class so you can just use System.out.println(iphoneDescriptions) – IceManStan Nov 23 '17 at 05:47