Skip to content

Instantly share code, notes, and snippets.

@golenishchev
Last active February 15, 2016 23:40
Show Gist options
  • Save golenishchev/9a170a82caad15db3f80 to your computer and use it in GitHub Desktop.
Save golenishchev/9a170a82caad15db3f80 to your computer and use it in GitHub Desktop.
Lesson 17. Changed
package com.example.lesson17;
public class Computer {
private String processor;
private String ram;
private String hdd;
private String videoCard;
private String powerSupply;
public Computer() {
}
public Computer(String processor, String ram, String hdd, String videoCard, String powerSupply) {
this.processor = processor;
this.ram = ram;
this.hdd = hdd;
this.videoCard = videoCard;
this.powerSupply = powerSupply;
}
public String getProcessor() {
return processor;
}
public String getRam() {
return ram;
}
public String getHdd() {
return hdd;
}
public String getVideoCard() {
return videoCard;
}
public String getPowerSupply() {
return powerSupply;
}
@Override
public String toString() {
String result = "Processor: ";
result += getProcessor() + "\n";
result += "RAM: " + getRam() + "\n";
result += "HDD: " + getHdd() + "\n";
result += "Video Card: " + getVideoCard() + "\n";
result += "Power Supply: " + getPowerSupply() + "\n";
return result;
}
}
package com.example.lesson17;
import java.util.ArrayList;
import java.util.ListIterator;
public class ComputerStore { /* for */
public void printByIndex(ArrayList<Computer> catalogComputersList) {
for (int i = 0; i < catalogComputersList.size(); i++) {
System.out.println(catalogComputersList.get(i));
}
} /* ListIterator */
public void printIterator(ArrayList<Computer> catalogComputersList) {
ListIterator<Computer> computerIterator = catalogComputersList.listIterator();
while (computerIterator.hasNext()) {
System.out.println(computerIterator.next());
}
} /* for-each */
public void printForEach(ArrayList<Computer> catalogComputersList) {
for (Computer hardware : catalogComputersList) {
System.out.println(hardware);
}
}
}
package com.example.lesson17;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Computer myComp = new Computer("50 x 3.2 GHZ", "640 GB", "10 PetaBytes", "4 x Radeon Z0", "GigaWatt");
ComputerStore cs = new ComputerStore();
ArrayList<Computer> catalogComputersList = new ArrayList<Computer>();
catalogComputersList.add(new Computer("2 x 1.6 GHz", "4 GB", "512 GB", "GeForce 210", "350 Watt"));
catalogComputersList.add(new Computer("2 x 3.2 GHZ", "8 GB", "1 TB", "Radeon R7 370", "550 Watt"));
catalogComputersList.add(new Computer("2 x 3.7 GHz", "16 GB", "1 TB", "GeForce GTX 970", "550 Watt"));
System.out.println("-------- for -------");
cs.printByIndex(catalogComputersList);
System.out.println("--- ListIterator ---");
cs.printIterator(catalogComputersList);
System.out.println("----- for-each -----");
cs.printForEach(catalogComputersList);
System.out.println("----- Hardware -----");
System.out.println(myComp.getProcessor()); // null
System.out.println(myComp.toString()); // null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment