Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class HotDogStand {
- // instance variables
- private int id;
- private int soldHotDog;
- // static variable : holds total number of sold hot dogs
- private static int totalSold = 0;// initializingwith zero
- // constructor
- public HotDogStand(int id, int soldHotDog){
- this.id = id;
- //initializing with zero
- this.soldHotDog = soldHotDog;
- // also adding in totalSold
- totalSold = totalSold+ soldHotDog;
- }
- // getters and setters
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getSoldHotDog() {
- return soldHotDog;
- }
- public static int getTotalSold() {
- return totalSold;
- }
- // justSold method
- public void justSold(){
- //increasing soldHotDog
- soldHotDog++;
- // also increasing totalSold
- totalSold++;
- }
- @Override
- public String toString() {
- return "ID: "+id+", Sold Hot Dog: "+soldHotDog;
- }
- @Override
- public boolean equals(Object obj) {
- if(obj instanceof HotDogStand){
- HotDogStand h = (HotDogStand)obj;
- if(h.id == id)
- return true;
- }
- return false;
- }
- // main method
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- HotDogStand stand0 = new HotDogStand(0, 0);
- HotDogStand stand1 = new HotDogStand(1,0);
- HotDogStand stand2 = new HotDogStand(2,0);
- String command = "";
- while (!command.equals("exit")){
- System.out.print("Enter commad:");
- command = sc.next();
- switch (command) {
- case "sold":
- System.out.print("Enter Cart Number:");
- int tempcar = sc.nextInt();
- if (tempcar == 0) {
- stand0.justSold();
- } else if (tempcar == 1) {
- stand1.justSold();
- } else if (tempcar == 2) {
- stand2.justSold();
- }
- break;
- case "print":
- System.out.print("Enter Cart Number:");
- int tempcarp = sc.nextInt();
- if (tempcarp == 0) {
- System.out.print("Cart sold: " + stand0.getSoldHotDog());
- } else if (tempcarp == 1) {
- System.out.print("Cart sold: " + stand1.getSoldHotDog());
- } else if (tempcarp == 2) {
- System.out.print("Cart sold: " + stand2.getSoldHotDog());
- }
- break;
- case "print-all":
- System.out.print("Total sold: " + HotDogStand.getTotalSold());
- break;
- default:
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement