So i'm working on this project and i'm having trouble moving variables from one class to the other. The goal of this tiny software is to have the user input 2 pieces of information, one's a string and the other is an int. I also have 2 classes, one to retrieve the information and the other one to calculate it. This is the first class called Software:
import java.util.Scanner;
public class Software
{
public Scanner softwareName;
public Scanner devicesAmount;
public Software()
{
devicesAmount = new Scanner(System.in);
softwareName = new Scanner(System.in);
}
/**
*Gets the information from the user to later on process
*/
public void InfoGet()
{
Devices findDevices= new Devices();
Devices findNumber= new Devices();
String softwareName;
int devicesAmount;
Scanner sc= new Scanner(System.in);
System.out.println("Welcome to the Software License Calculator");
System.out.println("Please type in the name of the Software:");
softwareName = sc.nextLine();
System.out.println("");
System.out.println("Please type in the number of devices that have the software:");
devicesAmount=sc.nextInt();
findDevices.Calculations(devicesAmount);
findNumber.getNewDevices();
sc.close();
System.out.println(softwareName+ " & "+ findNumber);
}
}
And here's the second class called Devices:
public class Devices
{
public int NewDevices;
public String softwareName;
/**
* Gets number of Devices to preform the calculations of removing 1000 users
*/
public static void Devices()
{
Software getDevices= new Software();
getDevices.InfoGet();
}
public void Calculations(int devicesAmount){
if (devicesAmount>=1000){
NewDevices= devicesAmount - 1000;
}
else{
NewDevices=devicesAmount;
}
}
}
dunno why I post as user when I'm on my movil device, but to the point:
First of all in the class Software you have 2 objects "Device", following the MVC arquitecture you should only declare 1 controller object for the view class, to start I recommend you to only declare 1 Device for Software class, like this and we're gonna make the variables protected just cause they should be private or protected to respect the encapsulation:
public class Software {
protected Devices devices;
protected Scanner softwareName;
protected Scanner devicesAmount;
... }
I recommend you to do the same for the Devices class variables.
Then we're gonna build a proper constructor for Devices class so we can move the values through classes:
public Devices (String softwareName) {
this.softwareName = softwareName;
this.newDevices = 0;
}
So we can have this values at the Devices class. Then we need to add the getter for newDevices and adding the toString method to the Devices class so you can print the object.
public int getNewDevices() {
return newDevices;
}
public String toString() {
return softwareName + " & " + newDevices;
}
Then we're gonna move some stuff at the Software InfoGet method to build de devices correctly and printing it.
public void InfoGet() {
String softwareName;
int devicesAmount;
Scanner sc= new Scanner(System.in);
System.out.println("Welcome to the Software License Calculator");
System.out.println("Please type in the name of the Software:");
softwareName = sc.nextLine();
System.out.println("");
System.out.println("Please type in the number of devices that have the software:");
devicesAmount=sc.nextInt();
devices = new Devices(softwareName); // So we initialize the object.
devices.Calculations(devicesAmount);
sc.close();
System.out.println(devices.toString()); // Here we just call the toString() method we build to print the values.
}
We didn't use the getNewDevices() method, but building getters and setters for controller classes is a good practice in general ;).