How to set customers data to object. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to set customers data to object.

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String firstName = read.nextLine(); String secondName = read.nextLine(); int age = read.nextInt(); int roomNumber = read.nextInt(); Customer customer = new Customer(); //set customer's data to object here customer.saveCustomerInfo(); } } class Customer { String firstName ;String secondName ;int age;int roomNumber; public void saveCustomerInfo() { System.out.println("First name: " + firstName); System.out.println("Second name: " + secondName); System.out.println("Age: " +age); System.out.println("Room number: " + roomNumber); } }

27th May 2022, 7:29 AM
We Doru
We Doru - avatar
1 Answer
+ 2
There are multiple ways to do this. you can set each attribute one by one like this https://code.sololearn.com/cmdg21DZOm1m/?ref=app Or you can define a constructor for class and then set all data in one command. https://code.sololearn.com/c7JsTX9Oa36w/?ref=app Also regarding the method saveCustomerInfo, shouldn't it be named printCustomerInfo instead? Cuz, It's just printing the info.
27th May 2022, 7:58 AM
Vinit Sonawane
Vinit Sonawane - avatar