Advertisement
Guest User

lab07 decomp

a guest
Mar 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2. import java.io.PrintStream;
  3. import java.util.Comparator;
  4. import java.util.Scanner;
  5. import javax.swing.JOptionPane;
  6.  
  7. public class Driver07
  8. {
  9.   public Driver07() {}
  10.  
  11.   public static void main(String[] args)
  12.   {
  13.     Salesperson[] array = input("data.txt");
  14.     menu(array);
  15.     System.exit(0);
  16.   }
  17.  
  18.   public static Salesperson[] input(String filename) {
  19.     Scanner infile = null;
  20.     try {
  21.       infile = new Scanner(new java.io.File(filename));
  22.     }
  23.     catch (FileNotFoundException e)
  24.     {
  25.       JOptionPane.showMessageDialog(null, "The file could not be found.");
  26.       System.exit(0);
  27.     }
  28.    
  29.     int numitems = infile.nextInt();
  30.     Salesperson[] array = new Salesperson[numitems];
  31.     for (int k = 0; k < numitems; k++)
  32.     {
  33.       String x = infile.next();
  34.       int y = infile.nextInt();
  35.       int z = infile.nextInt();
  36.       array[k] = new Salesperson(x, y, z);
  37.     }
  38.     infile.close();
  39.     return array;
  40.   }
  41.  
  42.   public static void menu(Salesperson[] array) {
  43.     int choice = 0;
  44.     while (choice != 7)
  45.     {
  46.       String message = "";
  47.       message = message + "\n1. List Alphabetically.";
  48.       message = message + "\n2. List by Cars Sold.";
  49.       message = message + "\n3. List by Trucks Sold.";
  50.       message = message + "\n4. List by Total Sales.";
  51.       message = message + "\n5. Add Sales.";
  52.       message = message + "\n6. Save data to file.";
  53.       message = message + "\n7. Quit.";
  54.       choice = Integer.parseInt(JOptionPane.showInputDialog(message));
  55.       switch (choice) {
  56.       case 1:
  57.         display(array, new ByName());
  58.         break;
  59.       case 2:  display(array, new ByCars());
  60.         break;
  61.       case 3:  display(array, new ByTrucks());
  62.         break;
  63.       case 4:  display(array, new ByTotalSales());
  64.         break;
  65.       case 5:  add(array);
  66.         break;
  67.       case 6:  save(array);
  68.         break;
  69.       case 7:
  70.         for (int k = 0; k < 25; k++)
  71.           System.out.println();
  72.         System.out.println("Bye-bye!");
  73.         break;
  74.       default:  System.out.println("Not a valid selection.");
  75.       }
  76.      
  77.       System.out.println();
  78.     }
  79.    
  80.     System.exit(0);
  81.   }
  82.  
  83.   public static void save(Salesperson[] array) {
  84.     PrintStream outfile = null;
  85.     try {
  86.       outfile = new PrintStream(new java.io.FileOutputStream("data.txt"));
  87.     }
  88.     catch (FileNotFoundException e)
  89.     {
  90.       JOptionPane.showMessageDialog(null, "The file could not be created.");
  91.     }
  92.    
  93.     outfile.println(array.length);
  94.     for (int k = 0; k < array.length; k++)
  95.     {
  96.       outfile.println(array[k].getName() + "\n" + array[k].getCars() + "\n" + array[k].getTrucks());
  97.     }
  98.    
  99.  
  100.  
  101.     outfile.close();
  102.    
  103.     for (int k = 0; k < 25; k++)
  104.       System.out.println();
  105.     System.out.println("Saved.");
  106.   }
  107.  
  108.   public static void add(Salesperson[] array)
  109.   {
  110.     int pos;
  111.     do
  112.     {
  113.       String name = JOptionPane.showInputDialog("What salesperson?");
  114.       pos = search(array, name);
  115.     } while (pos == -1);
  116.     String type;
  117.     do {
  118.       type = JOptionPane.showInputDialog("Cars or trucks?");
  119.       type = type.toLowerCase();
  120.     } while ((!type.equals("cars")) && (!type.equals("trucks")));
  121.     int amount;
  122.     do {
  123.       amount = Integer.parseInt(JOptionPane.showInputDialog("How many " + type + "?"));
  124.  
  125.     }
  126.     while (amount <= 0);
  127.     if (type.equals("cars")) {
  128.       array[pos].setCars(array[pos].getCars() + amount);
  129.     } else {
  130.       array[pos].setTrucks(array[pos].getTrucks() + amount);
  131.     }
  132.     for (int k = 0; k < 25; k++)
  133.       System.out.println();
  134.     System.out.println("Sales added.");
  135.   }
  136.  
  137.   public static int search(Salesperson[] array, String name) {
  138.     for (int k = 0; k < array.length; k++)
  139.       if (name.equals(array[k].getName()))
  140.         return k;
  141.     return -1;
  142.   }
  143.  
  144.   public static void display(Salesperson[] array, Comparator c) {
  145.     sort(array, c);
  146.     for (int k = 0; k < 25; k++)
  147.       System.out.println();
  148.     System.out.println("Name\tCars\tTrucks\tTotal");
  149.     System.out.println("-----------------------------");
  150.     for (int k = 0; k < array.length; k++) {
  151.       System.out.println(array[k].getName() + "\t" + array[k].getCars() + "\t" + array[k].getTrucks() + "\t" + (array[k].getCars() + array[k].getTrucks()));
  152.     }
  153.   }
  154.  
  155.   public static void sort(Object[] array, Comparator c)
  156.   {
  157.     for (int k = 0; k < array.length; k++)
  158.     {
  159.       int minPos = findMin(array, array.length - k, c);
  160.       swap(array, minPos, array.length - k - 1);
  161.     }
  162.   }
  163.  
  164.   private static int findMin(Object[] array, int upper, Comparator c) {
  165.     int minPos = 0;
  166.     for (int j = 1; j < upper; j++)
  167.       if (c.compare(array[j], array[minPos]) < 0)
  168.         minPos = j;
  169.     return minPos;
  170.   }
  171.  
  172.   private static void swap(Object[] array, int a, int b) {
  173.     Object temp = array[a];
  174.     array[a] = array[b];
  175.     array[b] = temp;
  176.   }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement