Skip to content

Instantly share code, notes, and snippets.

@OyeBenny
Last active July 25, 2016 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OyeBenny/d9c1a67705271254ff43225fe6074ebc to your computer and use it in GitHub Desktop.
Save OyeBenny/d9c1a67705271254ff43225fe6074ebc to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.io.*;
public class Elevator
{
public static void main (String[] args) throws IOException
{
Scanner scan = new Scanner(new File("elevatordata.txt"));
int s = scan.nextInt();
int i = 0;
int count = 0;
int countA;
int countB;
int countC;
PersonList persons = new PersonList(12);
while(scan.hasNext()){
persons.getPersons(0);
persons.getPersons(1);
persons.getPersons(2);
persons.getPersons(3);
persons.getPersons(4);
persons.getPersons(5);
persons.getPersons(6);
persons.getPersons(7);
persons.getPersons(8);
persons.getPersons(9);
persons.getPersons(10);
persons.getPersons(11);
person.setName(scan.next());
person.setWeight(scan.nextInt());
i++;
int total;
if(total > 1100)
System.out.println("You have exceeded the weight limit!");
countA = persons.howMany;
System.out.println("In the elevator" + countA + "people got on.");
System.out.println("The array by weight is");
countB = persons.sortWeights;
System.out.println("After the people were sorted by weight" + countB + "people got on.");
System.out.println("The array by name is");
countC = persons.sortNames;
System.out.println("After the people were sorted by name" + countC + "people got on.");
if(countA > countB && countA > countC)
{
System.out.println("The first method worked the best");
}
else if(countB > countA && countB > countC)
{
System.out.println("The second method worked the best");
}
else
{
System.out.println("The last method worked the best");
}
}
}
}
Bob 150
Ralph 305
Tim 225
Barbara 135
Jane 160
Steve 80
Tom 200
Mike 165
Shirley 90
Pam 100
Frank 120
import java.util.Scanner;
public class Person
{
private String name;
private int weight;
Scanner scan = new Scanner(System.in);
public Person(String n, int w)
{
name = n;
weight = w;
}
public Person()
{
name = "no name";
weight = 0;
}
public String toString()
{
return name + "\t" + weight;
}
public String getName()
{
return name;
}
public int getWeight()
{
return weight;
}
public void setName(String n)
{n = name;}
public void setWeight(int w)
{w = weight;}
}
public class PersonList
{
private Person[] persons;
private int count = 0;
public PersonList(int index)
{
persons = new Person[index];
persons[0] = new Person ("Anne", 30);
persons[1] = new Person ("Bob", 150);
persons[2] = new Person ("Ralph", 305);
persons[3] = new Person ("Tim", 225);
persons[4] = new Person ("Barbara", 135);
persons[5] = new Person ("Jane", 160);
persons[6] = new Person ("Steve", 80);
persons[7] = new Person ("Tom", 200);
persons[8] = new Person ("Mike", 165);
persons[9] = new Person ("Shirley", 90);
persons[10] = new Person ("Pam", 100);
persons[11] = new Person ("Frank", 120);
count = 0;
}
public getPersons(int index)
{
return persons.getpersons(0);
}
public void add(Person x)
{
persons[count] = x;
count++;
}
public int howMany()
{
int total = 0, i = 0;
do
{
total += persons[i].getWeight();
System.out.println("\n" + (i+1) + ":" + persons[i]);
i++;
}while (total <= 1100);
System.out.println("The total weight is " + (total- (persons[i-1].getWeight())) );
return i-1;
}
public int sortWeights()
{
int weightIndex;
for (int i=0; i < count; i++)
{
weightIndex = i;
for (int j = i+1; j < persons.length; j++)
if (persons[j].getWeight() < persons[weightIndex].getWeight())
weightIndex = j;
Person temp = persons[i];
persons[i] = persons[weightIndex];
persons[weightIndex] = temp;
}
return howMany();
}
public int sortNames()
{
int nameIndex;
for (int i=0; i < count; i++)
{
nameIndex = i;
for (int j = i+1; j < persons.length; j++)
if(persons[i].getName().compareTo(persons[nameIndex].getName())>0)
nameIndex = j;
Person temp = persons[i];
persons[i] = persons[nameIndex];
persons[nameIndex] = temp;
}
return howMany();
}
public String toString()
{
String out ="";
for (int i=0; i<count; i++)
out += ("\n"+(i+1) + ":\t" + persons[i]+"\n");
return out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment