Advertisement
SixPathsOfMen

I fought Joe Biden

Mar 12th, 2022
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class HotDogStand {
  4.  
  5.  
  6.  
  7.     // instance variables
  8.  
  9.     private int id;
  10.  
  11.     private int soldHotDog;
  12.  
  13.  
  14.  
  15.     // static variable : holds total number of sold hot dogs
  16.  
  17.     private static int totalSold = 0;// initializingwith zero
  18.  
  19.  
  20.  
  21.     // constructor
  22.  
  23.     public HotDogStand(int id, int soldHotDog){
  24.  
  25.         this.id = id;
  26.  
  27.         //initializing with zero
  28.  
  29.         this.soldHotDog = soldHotDog;
  30.  
  31.  
  32.  
  33.         // also adding in totalSold
  34.  
  35.         totalSold = totalSold+ soldHotDog;
  36.  
  37.     }
  38.  
  39.     // getters and setters
  40.  
  41.     public int getId() {
  42.  
  43.         return id;
  44.  
  45.     }
  46.  
  47.     public void setId(int id) {
  48.  
  49.         this.id = id;
  50.  
  51.     }
  52.  
  53.     public int getSoldHotDog() {
  54.  
  55.         return soldHotDog;
  56.  
  57.     }
  58.  
  59.     public static int getTotalSold() {
  60.  
  61.         return totalSold;
  62.  
  63.     }
  64.  
  65.  
  66.  
  67.     // justSold method
  68.  
  69.     public void justSold(){
  70.  
  71.         //increasing soldHotDog
  72.  
  73.         soldHotDog++;
  74.  
  75.         // also increasing totalSold
  76.  
  77.         totalSold++;
  78.  
  79.     }
  80.  
  81.  
  82.  
  83.     @Override
  84.  
  85.     public String toString() {
  86.  
  87.         return "ID: "+id+", Sold Hot Dog: "+soldHotDog;
  88.  
  89.     }
  90.  
  91.  
  92.  
  93.     @Override
  94.  
  95.     public boolean equals(Object obj) {
  96.  
  97.         if(obj instanceof HotDogStand){
  98.  
  99.             HotDogStand h = (HotDogStand)obj;
  100.  
  101.             if(h.id == id)
  102.  
  103.                 return true;
  104.  
  105.         }
  106.  
  107.         return false;
  108.  
  109.     }
  110.  
  111.  
  112.  
  113.     // main method
  114.  
  115.     public static void main(String[] args) {
  116.         Scanner sc = new Scanner(System.in);
  117.  
  118.         HotDogStand stand0 = new HotDogStand(0, 0);
  119.         HotDogStand stand1 = new HotDogStand(1,0);
  120.         HotDogStand stand2 = new HotDogStand(2,0);
  121.  
  122.         String command = "";
  123.         while (!command.equals("exit")){
  124.             System.out.print("Enter commad:");
  125.             command = sc.next();
  126.             switch (command) {
  127.                 case "sold":
  128.                     System.out.print("Enter Cart Number:");
  129.                     int tempcar = sc.nextInt();
  130.                     if (tempcar == 0) {
  131.                         stand0.justSold();
  132.                     } else if (tempcar == 1) {
  133.                         stand1.justSold();
  134.                     } else if (tempcar == 2) {
  135.                         stand2.justSold();
  136.                     }
  137.                     break;
  138.                 case "print":
  139.                     System.out.print("Enter Cart Number:");
  140.                     int tempcarp = sc.nextInt();
  141.                     if (tempcarp == 0) {
  142.                         System.out.print("Cart sold: " + stand0.getSoldHotDog());
  143.                     } else if (tempcarp == 1) {
  144.                         System.out.print("Cart sold: " + stand1.getSoldHotDog());
  145.                     } else if (tempcarp == 2) {
  146.                         System.out.print("Cart sold: " + stand2.getSoldHotDog());
  147.                     }
  148.                     break;
  149.                 case "print-all":
  150.                     System.out.print("Total sold: " + HotDogStand.getTotalSold());
  151.                     break;
  152.                 default:
  153.                     break;
  154.             }
  155.          }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement