java课题–day2基础回顾
存钱和取钱
package account;
public class Account {
private long ID;
private String passWord;
private String name;
private String personld;
private String email;
private double blance;
public Account() {
this.ID = 0L;
this.passWord = null;
this.name = null;
this.personld = null;
this.email = null;
this.blance = 0.0;
}
public Account(long ID, String passWord, String name, String personld, String email, double blance) {
this.ID = ID;
this.passWord = passWord;
this.name = name;
this.personld = personld;
this.email = email;
this.blance = blance;
}
public long getID() {
return ID;
}
public void setID(long ID) {
this.ID = ID;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPersonld() {
return personld;
}
public void setPersonld(String personld) {
this.personld = personld;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public double getBlance() {
return blance;
}
public void setBlance(double blance) {
this.blance = blance;
}
@Override
public String toString() {
return "Account{" +
"ID=" + ID +
", passWord='" + passWord + '\'' +
", name='" + name + '\'' +
", personld='" + personld + '\'' +
", email='" + email + '\'' +
", blance=" + blance +
'}';
}
public double deposit(double blance){
if (blance<=0){
System.out.println("金额不等为0或负值!无法存款!");
}else {
this.blance = this.blance + blance;
}
return this.blance;
}
public double withdraw(double blance){
if (this.blance<blance || blance<0){
System.out.println("发生异常,您当前账户余额为"+this.blance+"元,不得进行违规取款(请检查您的取款金额)!");
}else{
this.blance=this.blance-blance;
}
return this.blance;
}
}
package account;
import java.util.Scanner;
public class AccountTest {
public static void main(String[] args) {
Account p1=new Account(001L,"001001","001","student","[email protected]",100.0);
System.out.println(p1);
System.out.println( "当前账户余额:"+p1.getBlance());
boolean flag=true;
while (flag) {
System.out.println("请输入您要进行的操作 0:取款 1:存款 \n请输入0或1进行选择,输入其他数值将会退出系统!");
Scanner input = new Scanner(System.in);
int ch = input.nextInt();
if (ch == 0) {
System.out.println("请输入您要取款的金额:");
double blance = input.nextDouble();
p1.withdraw(blance);
System.out.println("当前账户余额:" + p1.getBlance());
}else if (ch==1){
System.out.println("请输入您要存入的金额:");
double blance = input.nextDouble();
p1.deposit(blance);
System.out.println( "当前账户余额:"+p1.getBlance());
}else{
flag=false;
System.out.println("感谢您的使用!再见!");
}
}
System.out.println("正在退出系统...\n退出成功!");
}
}
圆:求面积的周长
package Circle;
public class CircleDemo {
double r;
static final double PI=3.14;
CircleDemo(){
this.r=1.0;
}
public CircleDemo(double r){
this.r=r;
}
public double mianji(){
return this.r*PI*PI;
}
public double zhouchang(){
return this.r*2*PI;
}
}
package Circle;
public class CircleDemoTest {
public static void main(String[] args) {
CircleDemo circle1=new CircleDemo(2);
double m=circle1.mianji();
System.out.println("圆的面积是"+m);
double z=circle1.zhouchang();
System.out.println("圆的周长是"+z);
System.out.println("无参构造生成的圆的半径是"+new CircleDemo().r);
}
}
公司发钱问题:生日是否和本月相同(继承)
package Company;
public class Employee {
private String name;
private int month;
public Employee(String name, int month) {
this.name = name;
this.month = month;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public double getSalary(int month){
if (this.month==month){
return 100;
}else{
return 0;
}
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", month=" + month +
'}';
}
}
package Company;
public class SalesEmployee extends Employee {
private double monthSale;
private double reyaltyRate;
public SalesEmployee(String name, int month,double monthSale,double reyaltyRate) {
super(name, month);
this.reyaltyRate=reyaltyRate;
this.monthSale=monthSale;
}
public double getMonthSale() {
return monthSale;
}
public void setMonthSale(double monthSale) {
this.monthSale = monthSale;
}
public double getReyaltyRate() {
return reyaltyRate;
}
public void setReyaltyRate(double reyaltyRate) {
this.reyaltyRate = reyaltyRate;
}
@Override
public double getSalary(int month){
return super.getSalary(month)+monthSale*reyaltyRate;
}
@Override
public String toString() {
return "SalesEmployee{" +
"monthSale=" + monthSale +
", reyaltyRate=" + reyaltyRate +
"} " + super.toString();
}
}
package Company;
public class SalariedEmployee extends Employee {
private double monthPay;
public SalariedEmployee(String name, int month,double monthPay) {
super(name, month);
this.monthPay=monthPay;
}
public double getMonthPay() {
return monthPay;
}
public void setMonthPay(double monthPay) {
this.monthPay = monthPay;
}
@Override
public double getSalary(int month){
return super.getSalary(month)+monthPay;
}
@Override
public String toString() {
return "SalariedEmployee{" +
"monthPay=" + monthPay +
"} " + super.toString();
}
}
package Company;
public class HourlyEmployee extends Employee {
private double hourPay;
private double hour;
public HourlyEmployee(String name, int month,double hourPay,double hour) {
super(name, month);
this.hour=hour;
this.hourPay=hourPay;
}
public double getHourPay() {
return hourPay;
}
public void setHourPay(double hourPay) {
this.hourPay = hourPay;
}
public double getHour() {
return hour;
}
public void setHour(double hour) {
this.hour = hour;
}
@Override
public double getSalary(int month){
if (hour<=160) {
return super.getSalary(month)+hour * hourPay;
}else {
return super.getSalary(month)+160*hourPay+(hour-160)*hourPay*1.5;
}
}
@Override
public String toString() {
return "HourlyEmployee{" +
"hourPay=" + hourPay +
", hour=" + hour +
"} " + super.toString();
}
}
package Company;
public class BasePlusSalesEmployee extends SalesEmployee{
private double basePay;
public BasePlusSalesEmployee(String name, int month,double monthSale,double reyaltyRate,double basePay) {
super(name,month,monthSale,reyaltyRate);
this.basePay=basePay;
}
public double getBasePay() {
return basePay;
}
public void setBasePay(double basePay) {
this.basePay = basePay;
}
@Override
public double getSalary(int month){
return super.getSalary(month)+basePay;
}
@Override
public String toString() {
return "BasePlusSalesEmployee{" +
"basePay=" + basePay +
"} " + super.toString();
}
}
package Company;
public class EmployeeDemoTest {
public static void main(String[] args) {
int thisMoth=12;
Employee employee=new Employee("e1",12);
System.out.println("职位:"+employee);
System.out.println("本月生日奖励金为:"+employee.getSalary(thisMoth));
SalariedEmployee salariedEmployee=new SalariedEmployee("s1",11,3500);
System.out.println("职位:"+salariedEmployee);
System.out.println("本月工资为:"+salariedEmployee.getSalary(thisMoth));
HourlyEmployee hourlyEmployee=new HourlyEmployee("h1",12,12,200);
System.out.println("职位:"+hourlyEmployee);
System.out.println("本月工资为:"+hourlyEmployee.getSalary(thisMoth));
BasePlusSalesEmployee basePlusSalesEmployee=new BasePlusSalesEmployee("b1",12,30000,0.1,3000);
System.out.println("职位:"+basePlusSalesEmployee);
System.out.println("本月工资为:"+basePlusSalesEmployee.getSalary(thisMoth));
}
}
图形求周长和面积(抽象类)
package ShapeDemo;
public class Circle extends Shape{
private double r;
private final static double PI=3.14;
public Circle(double r) {
this.r = r;
}
@Override
public double chouchang() {
return r*PI*2;
}
@Override
public double mianji(){
return r*PI*r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
package ShapeDemo;
import java.awt.*;
public class Rect extends Shape {
private double x;
private double y;
public Rect(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public double chouchang() {
return 2*(x+y);
}
@Override
public double mianji(){
return x*y;
}
}
package ShapeDemo;
public abstract class Shape {
public double chouchang(){
return 0.0;
}
public double mianji(){
return 0.0;
}
}
package ShapeDemo;
public class Square extends Rect {
private double l;
public Square(double l) {
super(l,l);
}
@Override
public double chouchang() {
return super.chouchang();
}
@Override
public double mianji(){
return super.mianji();
}
}
package ShapeDemo;
public class ShapeDemoTest {
public static void main(String[] args) {
Rect rect=new Rect(5.1,8.9);
System.out.println("矩形周长为"+rect.chouchang());
System.out.println("矩形面积为"+rect.mianji());
System.out.println("圆周长为"+new Circle(3.6).chouchang());
System.out.println("圆面积为"+new Circle(3.6).mianji());
System.out.println("正方形周长为"+new Square(4).chouchang());
System.out.println("正方形面积为"+new Square(4).mianji());
}
}
动物管理员喂食(接口和抽象类)
package Zoo;
public interface Animal {
public void eat(Food foods);
}
package Zoo;
public class Cat implements Animal{
public void eat(Food food){
System.out.println("Cat eat "+food.getFoodName());
}
}
package Zoo;
public class Fish extends Food {
public Fish(String name){
this.foodName= name;
}
}
package Zoo;
public abstract class Food {
protected String foodName;
public String getFoodName(){
return foodName;
}
public String setFoodNane(String foodName){
this.foodName=foodName;
return this.foodName;
}
}
package Zoo;
public class People {
public void feed(Animal animal,Food food){
animal.eat(food);
}
}
package Zoo;
public class ZooTest {
public static void main(String[] args) {
People people=new People();
Cat cat=new Cat();
Fish fish=new Fish(" 鲫鱼");
people.feed(cat,fish);
}
}