java-Set Programming Exercises

Source: Internet
Author: User

Demand:

Code:

/*** * Auto Class **/ Public classCar {PrivateString brand;//Automotive Brands    Private DoublePrice//Car Price    PrivateString color;//Car Color     PublicCar () {} PublicCar (String brand,DoublePrice , String color) {         This. Setbrand (brand);  This. Setprice (price);  This. SetColor (color); }     PublicString Getbrand () {returnbrand; }     Public voidSetbrand (String brand) { This. Brand =brand; }     Public DoubleGetPrice () {returnPrice ; }     Public voidSetprice (DoublePrice ) {         This. Price =Price ; }     PublicString GetColor () {returncolor; }     Public voidsetcolor (String color) { This. color =color; } @Override PublicString toString () {return"Brands:" + brand + "\ n Price:" + (int) Price + "\ n color:" + color+ "\ n"; }    }
/*** Automobile Management class*/ImportJava.util.HashSet;ImportJava.util.Iterator;ImportJava.util.Scanner;ImportJava.util.Set; Public classCarmanager {HashSet<Car> set =NewHashset<>(); Scanner SC=NewScanner (system.in); //ways to add cars:     Public voidAdd () {Car car=NewCar (); System.out.println ("Please enter the car's brand:");        Car.setbrand (Sc.next ()); System.out.println ("Please enter the price of the car:");        Car.setprice (Sc.nextdouble ()); System.out.println ("Please enter the color of the car:");        Car.setcolor (Sc.next ());        Set.add (car); System.out.println ("Car information added successfully!!"); }    //View information for all cars     Public voidShow () {if(Set.isempty ()) {System.out.println ("No car information, just add some!"); }Else{System.out.println ("Car Info:");  for(Car car:set) {System.out.println (car); }        }            }    //Modify car information according to car brand     Public voidChangbrand () {BooleanFlag =false; System.out.println ("Please enter the car brand you want to modify:"); Scanner SC=NewScanner (system.in); String Brand=Sc.next (); System.out.println ("Please enter the price you want to modify:"); Double Price=sc.nextdouble (); Iterator<Car> it =Set.iterator ();  while(It.hasnext ()) {Car C=It.next (); if(C.getbrand (). Equals (Brand)) {C.setprice (price); Flag=true; }        }        if(flag) {System.out.println ("Modified successfully!!"); }ElseSystem.out.println ("The staff was very friendly and the staff were very friendly."); }    //How to remove a car from a car brand     Public voidRemove () {BooleanFlag =false; System.out.println ("Please enter the car brand you want to delete:"); Scanner SC=NewScanner (system.in); String Brand=Sc.next (); Set<Car> Set1 =NewHashset<car>();  for(Car car:set) {if(Car.getbrand (). Equals (Brand)) {Set1.add (car); Flag=true; }        }        if(flag) {Set.removeall (SET1); System.out.println ("Delete success!!"); }ElseSystem.out.println ("The staff was very friendly and the staff were very friendly."); }}
/*** Test Class (Program startup Class)*/ImportJava.util.Scanner; Public classCartest {//Menu Method     Public voidmenu () {System.out.println ("**************************"); System.out.println ("\T1. Adding car Information"); System.out.println ("\t2. View all car Information"); System.out.println ("\t3. Change car prices According to car brands"); System.out.println ("\t4. Remove car according to car brand"); System.out.println ("\t0. Exit program"); System.out.println ("**************************"); System.out.println ("Please enter the action you have chosen:"); }     Public Static voidMain (string[] args) {Carmanager cm=NewCarmanager (); Cartest CT=Newcartest ();        Ct.menu (); Scanner SC=NewScanner (system.in);  while(true) {            intn =Sc.nextint (); Switch(n) { Case1: Cm.add ();                Ct.menu (); Continue;  Case2: Cm.show ();                Ct.menu (); Continue;  Case3: Cm.changbrand ();                Ct.menu (); Continue;  Case4: Cm.remove ();                Ct.menu (); Continue;  Case0: System.exit (1); default: System.out.println ("Select the error, please re-select:"); }        }    }}

java-Set Programming Exercises

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.