@创建对象属性、方法。
package xt;
import java.util.Scanner;
public class Website { //创建运营商类
static String WebsiteName; //运营商名称
private String name; //用户姓名
private String password ; //密码
private double balance ; //账户余额
private double turnover ; //交易额
private boolean authenticated; //布尔值验证
//欢迎语句
static void welcome() {
System.out.println ("----------" + "欢迎来到" +WebsiteName + "-----------");
}
public Website(String name,String password,double turnover) {
// TODO Auto-generated method stub
this.name = name;
this.password = password;
this.turnover = turnover;
this.balance = turnover - 10;
this.authenticated = false;
System.out.println (name+"开户成功,账户余额为"+balance) ;
}
public boolean getAuthenticated() {
return this.authenticated;
}
public boolean authenticate(String pwd) {
if (pwd.equals(this.password)) {
this.authenticated = true;
}
return this.authenticated;
}
public void deposit (double turnover) {
Scanner sc=new Scanner(System.in);
System.out.println("存款请输入:");
turnover=sc.nextDouble();
System.out.println(turnover);
balance = balance + turnover;
System.out.println(name+ "您好,您的账户 已存入"+turnover+"元," + "当前余额 "+balance+ " 元");
}
public void withdrawal (double turnover) {
if (!this.getAuthenticated()) {
// System.out.println("您输入的密码错误!");
System.out.println("Unauthorised operation!");
// Could throw an exception here
return;
}
//判断余额量
if (balance - turnover > 0 ) {
this.balance =balance- turnover;
System.out.println(name+"您好,您已成功办理半年宽带上网业务,感谢您的使用,通过您的账户 办理业务 已消费"+turnover+"元, "+ "当前余额 "+balance+" 元"+ "\n欢迎使用中国联通,感谢您的大力支持,我们将会更好的对您的服务!") ;
}else {
System.out.println( name+"您好,您已成功办理半年宽带上网业务,感谢您的使用,通过您的账户 办理业务 已消费"+turnover+"元, "+ "当前余额为 "+balance +" 元,您的话费即将不足,为了不影响您的通信,请及时交话费"+"\n欢迎使用中国联通,感谢您的大力支持,我们将会更好的对您的服务!") ;
}
}
static void welcomeNext() {
System.out.println("---------- "+ "请携带好随身财物,欢迎下次光临" +WebsiteName+" ------------");
}
}
@利用类名访问,创建对象并且赋值。
package xt;
import java.util.Scanner;
public class text01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Website.WebsiteName = "中国联通"; //定义一家运营商
Website.welcome(); //调用欢迎
Website website= new Website("陈意涵我女神","654321", 200.0); //构造方法进行用户操作
website.deposit(500.0); //进行存款操作
Scanner s = new Scanner(System.in);
int counter = 0;
while (counter < 3 && !website.getAuthenticated()) {
System.out.println("办理上网业务 请输入密码 Please input your password.");
if (website.authenticate(s.nextLine())) {
break;
}
++ counter;
System.out.println("\t密码不正确 \n Incorrect password.");
}
if (!website.getAuthenticated()) {
System.out.println("登录失败 "
+ "Failed to login!" + "\n关闭程序");
return;
}
//密码错误,进入失败
website.withdrawal(200.0); //余额不足,
// website.withdrawal(100.0); //密码正确,余额充足
website.welcomeNext();
}
}
实现效果
里面采用到this关键词调用,if…else判断句,while…if…密码循环。
第一次做系统项目,有很多的项目都被前辈做的千千万万,这次我想做一个不一样的。现在这个项目不够完整,有些模块没完善,用这次学习验证自己收获。还有很多语法需要进一步的学习。