Skip to content
Snippets Groups Projects
Select Git revision
  • ff43402bd56330adb9792d68fca297a231147998
  • master default protected
  • revert-d81c9d6d
3 results

DrawerState.java

Blame
  • Forked from COUETOUX Basile / graphic-2020
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Backpack.java 1.20 KiB
    import java.util.Comparator;
    import java.util.List;
    
    public class Backpack {
        int capacity;
        List<Loot> loots;
        int sfValuee = 0;
        int sfWeight = 0;
    
        public Backpack(int capacity, List<Loot> loots) {
            this.capacity = capacity;
            this.loots = loots;
        }
    
        public String toString(){
            return capacity +"\n"+loots;
        }
    
        /**
        Méthode pour trier les objets par ordre décroissant selon leur ratio
        */
        public void sortByRatio(){
            loots.sort(Comparator.comparing(Loot::getRatio).reversed());
        }
    
        public void solutionFractionnelle(){
            this.sortByRatio();
    
            for(Loot loot : loots){
                if(sfWeight + loot.getWeight() <= this.capacity){
                    // Si l'objet entier peut entrer dans le sac
                    sfValuee += loot.getValue();
                    sfWeight += loot.getWeight();
                }
                else {
                    // Si l'objet ne peut pas entrer entièrement dans le sac
                    int remainingWeight = this.capacity - sfWeight;
                    sfValuee += (int) (loot.getRatio() * remainingWeight);
                    break;
                }
            }
        }
    
        public int getSfValuee() {
            return sfValuee;
        }
    
    }