Advertisement
Guest User

HW4

a guest
Oct 14th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. //Christina Zimmer
  2.  
  3. import java.util.Scanner;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6.  
  7.  
  8. class Taco implements Comparable {
  9.      String name;
  10.      double price;
  11.      
  12.      public Taco(String name, double price) {
  13.          this.name = name;
  14.          this.price = price;
  15.      }
  16.      
  17.      public String toString() {
  18.          String formattedPrice = String.format("$%.2f", this.price);
  19.          return this.name + " - " + formattedPrice;
  20.      }
  21.  
  22.     @Override
  23.     public int compareTo(Object otherTaco) {
  24.          double otherPrice=((Taco)otherTaco).price;
  25.             /* For Ascending order of prices*/
  26.            
  27.             if(this.price < otherPrice) {
  28.                 return -1;
  29.             } else {
  30.                 return 0;
  31.             }
  32.     }
  33. }
  34.  
  35. public class HW4{
  36.    
  37.  
  38.      public static void main(String []args){
  39.          ArrayList<Taco> tacoList = new ArrayList<Taco>();
  40.          
  41.          int tacosNeeded = 10;  //needed is variadic for convenience
  42.          
  43.          //2 scanners instead of 1 so there's no carriage consumption manipulations
  44.          Scanner sNames = new Scanner(System.in);  //scans strings only
  45.          Scanner sPrices = new Scanner(System.in); //scans only doubles
  46.          
  47.          System.out.println("Welcome to the taco price sorter! Enter 10 taco names and prices and I’ll sort it!");
  48.          
  49.          //repeat 10 times to populate parallel arrays
  50.          for(int i = 0; i < tacosNeeded; i++) {
  51.              System.out.print("\nEnter name of taco " + (i+1) + ": ");
  52.              String tacoName = sNames.nextLine();
  53.              
  54.              System.out.print("Enter price of taco " + (i+1) + ": ");
  55.              double tacoPrice = sPrices.nextDouble();
  56.              
  57.              while(tacoPrice < 0) {
  58.                  //cheeky error message!
  59.                  System.out.print("\nError! Taco's cant be freeeee!\n(Enter a positive dollar amount this time): ");
  60.                  tacoPrice = sPrices.nextDouble();
  61.              }
  62.              
  63.              tacoList.add(new Taco(tacoName, tacoPrice));
  64.          }
  65.          
  66.          //sort backed by mergesort, overridden in compareTo in Taco
  67.          Collections.sort(tacoList);
  68.          
  69.          System.out.println();
  70.          
  71.          //iterate through and output all the Tacos, ordered by price
  72.          for(Taco t : tacoList) {
  73.              System.out.println(t.toString());
  74.          }
  75.      }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement