public static void main(String[] args) {
/*The following is the code to a Class containing two elements, name and boardFootage. I'll get the data from a Scanner, store it in an ArrayList in my Furniture object, then print out the items of the ArrayList in ascending order.
*/
class Furniture implements Comparable<Furniture> {
public String toString() {
return getName() + ": " + getBoardFootage() + "\n";
}
private String name;
private double boardFootage;
Furniture() {
name = "";
boardFootage = 0.0;
}
Furniture(String nameInput, double boardFootageInput) {
name = nameInput;
boardFootage = boardFootageInput;
}
public String getName()
{
Scanner keyboard = new Scanner(System.in);
String name = "";
while (!name.equalsIgnoreCase("quit"))
{
name = keyboard.next();
if (name == "quit") {
System.out.println("Project Summary");
// PRINT LIST SORTED BY PROJECT SIZE FROM SMALLEST
// TO LARGEST USING Collections.sort METHOD
}
}
return name;
}
public void setName(String name) {
Scanner keyboard = new Scanner(System.in);
this.name = keyboard.next();
}
public double getBoardFootage() {
Scanner keyboard = new Scanner(System.in);
double boardFootage = 0.0;
boardFootage = keyboard.nextDouble();
return boardFootage;
}
public void setBoardFootage(double boardFottage) {
Scanner keyboard = new Scanner(System.in);
this.boardFootage = keyboard.nextDouble();
}
public int compareTo(Furniture o) {
if (this.boardFootage < o.boardFootage) {
return -1;
} else if (this.boardFootage > o.boardFootage) {
return 1;
}
return this.name.compareTo(o.name);
}
}
Furniture furniture = new Furniture();
ArrayList<Furniture> furnitureList = new ArrayList<Furniture>();
Asked
Active
Viewed 117 times
-4
-
2Have you tried something yet? – Tim Biegeleisen Jul 17 '17 at 01:29
-
@TimBiegeleisen yes, asking SO :P – Tavo Jul 17 '17 at 01:29
-
[How do I compare strings in Java?](https://stackoverflow.com/q/513832/1553851) – shmosel Jul 17 '17 at 01:30
-
What's your real problem? Print the `List`? Tried `System.out.println()`? – MiguelKVidal Jul 17 '17 at 01:30
-
1Declaring a class within your main method is a.. strange practice. – DavidBittner Jul 17 '17 at 01:32
-
What is your problem here? Print the List or sort it? – MiguelKVidal Jul 17 '17 at 01:34
-
1@DavidBittner I've been checking out Java decompilers recently, and one thing I found is that none of them can handle method local class. So, the one and only use for that might be to mess with decompilers. :) – KC Wong Jul 17 '17 at 01:37
1 Answers
1
The way you would anytime?
//furnitureRef is a reference to a member of the list
for( Furniture furnitureRef : furnitureList ) {
//Do stuff with furnitureRef
}
This seems like a homework question, so I'm cautious to give more help without you at least trying to do some yourself.
Additionally, the generally accepted way is to create a toString method for every object you define. This toString function should return a string of your object, serialized, such as:
class Car {
String make;
String model;
public String toString() {
return "{ make: " + make + " model: "+model+" }";
}
}
This would then allow you to write:
//furnitureRef is a reference to a member of the list
for( Furniture furnitureRef : furnitureList ) {
System.out.println( furnitureRef.toString() );
}

DavidBittner
- 515
- 2
- 4
- 15
-
3
-
@shmosel eh, fair enough I suppose. Normally would, but I suppose if they really are a student it's good to set a proper example and not be lazy on examples :^) – DavidBittner Jul 17 '17 at 01:33
-
You're apparently still "doing stuff with i" despite the fact there *is* no `i` :-) – paxdiablo Jul 17 '17 at 01:56
-
@paxdiablo pfft, of course I'm not! Obviously you need to check the code again ;) – DavidBittner Jul 17 '17 at 01:57
-
1Your revisionism would be more effective if SO didn't have a history feature, and if you had also changed line 1 :-) – paxdiablo Jul 17 '17 at 02:00
-
Honestly, it's comical at that point. A sick programmer is a bad programmer apparently. – DavidBittner Jul 17 '17 at 02:01
-